Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPS/WSS Client Side Setup #86

Open
NPPprojects opened this issue Apr 22, 2024 · 1 comment
Open

HTTPS/WSS Client Side Setup #86

NPPprojects opened this issue Apr 22, 2024 · 1 comment

Comments

@NPPprojects
Copy link

NPPprojects commented Apr 22, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant