using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using WebSocketSharp.Server;

public class VideoChatWebSocketServer : MonoBehaviour
{
    private WebSocketServer wssv;
    private string serverIp;
    private int port = 8080;

    private void Awake()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                serverIp = ip.ToString();
                break;
            }
        }

        wssv = new WebSocketServer($"ws://{serverIp}:{port}");
        wssv.AddWebSocketService<VideoChatMediaStreamService>($"/{nameof(VideoChatMediaStreamService)}");

        wssv.Start();
        Debug.Log($"Server started on {serverIp}.");
    }

    private void OnDestroy()
    {
        wssv.Stop();    
    }
}
