itisnajim / SocketIOUnity

A Wrapper for socket.io-client-csharp to work with Unity.
MIT License
379 stars 65 forks source link

HTTPS/WSS Client Side Setup #86

Open NPPprojects opened 2 months ago

NPPprojects commented 2 months ago

I've made a self-signed certificate to make https requests to my WebSocket server. However, I can't find any options for setting certificates on the Unity client end. Is that possible? Here is my current client-side code:

 public class WebSockets : MonoBehaviour {

        public SocketIOUnity socket;

        private string serverUrlLink = "wss://192.168.1.116:3000";

        private void Awake()
        {
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {
                return true; // Supposed to accept all certificates, perhaps only works with UnityWebRequest?
            };
        }

        private void Start()
        {
            var uri = new Uri(serverUrlLink);

            socket = new SocketIOUnity(
                uri,
                new SocketIOOptions {
                    Query = new Dictionary<string, string> { { "token", "UNITY" } },
                    EIO = 4,
                    Transport = SocketIOClient.Transport.TransportProtocol.WebSocket,
                }
            );

            socket.JsonSerializer = new NewtonsoftJsonSerializer();

            ///// reserved socketio events
            socket.OnConnected += (sender, e) => { Debug.Log("socket.OnConnected"); };
            socket.OnDisconnected += (sender, e) => { Debug.Log("disconnect: " + e); };
            socket.OnReconnectAttempt += (sender, e) => { Debug.Log($"{DateTime.Now} Reconnecting: attempt = {e}"); };
            ////

            Debug.Log("Connecting...");
            socket.Connect();
            socket.On("message", OnMessageReceived);
            NativeInputController.s_Press += OnPress;
            NativeInputController.s_DownSwipe += Disconnect;

            Debug.Log(socket.ClientWebSocketProvider);
            Debug.Log(socket.HttpClient);
            Debug.Log(socket.Options.Auth);
        }

        void Disconnect()
        {
            socket.Disconnect();
            socket.Dispose();
        }

        void OnDestroy()
        {
            socket.Disconnect();
            socket.Dispose();
        }

        private void OnPress()
        {
            socket.EmitAsync("message", "Hello, server!");
            Debug.Log("press");
        }

        private void OnMessageReceived(SocketIOResponse evt)
        {
            Debug.Log("Response received from server: " + evt.ToString());
        }

    }

Whenever I run this code, the client keeps trying to connect to the server and the server responds by outputting "accepted" repeatedly. Not running with certificates, the code runs fine.

NPPprojects commented 2 months ago

To anyone stuck on this, found a solution on the following forum: https://forum.unity.com/threads/how-to-allow-self-signed-certificate.522183/

Modified my code to https instead of wss and added the following:

public class WebSockets : MonoBehaviour
    {

        public SocketIOUnity socket;

        private string serverUrlLink = "https://192.168.1.116:5000";
        class NoCheckCertificatePolicy : System.Net.ICertificatePolicy {

            public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
            {
                return true;
            }

        }

        private void Awake()
        {
            System.Net.ServicePointManager.CertificatePolicy = new NoCheckCertificatePolicy();
        }
        // rest of code