Search code examples
xsockets.net

XSockets WSS not connecting


I'm having great difficulty making a WSS Xsockets implementation. I've made a selfsigned certificate in IIS7. I've tried connecting several times via JS and it simply will not. What's more puzzling is that the non-secure implementation works absolutely fine.

My server code is as follows:

         public class SSLConfig : ConfigurationSetting
        {
            public SSLConfig()
                : base("wss://localhost:4509")
            {
                this.CertificateLocation = System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine;
                this.CertificateSubjectDistinguishedName = "cn=localhost";
            }
        }

    class Program
        {
            static void Main(string[] args)
            {
                var myCustomConfigs = new List<IConfigurationSetting>();
                myCustomConfigs.Add(new SSLConfig());

                using (var server = Composable.GetExport<IXSocketServerContainer>())
                {
                    Console.WriteLine("running");
                    server.StartServers(configurationSettings: myCustomConfigs);
                    foreach (var serv in server.Servers)
                    {
                        Console.WriteLine(serv.ConfigurationSetting.Endpoint);
                    }
                    Console.ReadLine();
                    server.StopServers();
                }
            }
        }

 public class TestSockets : XSocketController
    {      
        public TestSockets()
        {
        }

        public void Echo(string message)
        {
            this.Send(new
            {
                Message = message
            }.AsTextArgs("message"));
        }
}

Javascript:

<html>
<head>
  <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
  <script src="\scripts\XSockets.latest.js"></script>
  <script>
    var ws;
    $(function () {
      ws = new XSockets.WebSocket("wss://localhost:4509/TestSockets");

      ws.on(XSockets.Events.open, function (clientInfo) {
        alert(clientInfo);
        console.log('Open', clientInfo);
      });

      ws.on(XSockets.Events.onError, function (err) {
        alert(err);
        console.log('Error', err);
      });

      $("#btnSocket").on("click", function () {
        ws.publish("Echo", { message: XSockets.Utils.randomString(50) });
      });
    });
  </script>
</head>
<body>
<input type="button" value="click" id="btnSocket"/>
</body>
</html>

Solution

  • It turns out that due to the use of a self signed certificate, Firefox threw an error when trying to establish a connection (Chrome simply kept silent...).

    After visiting the URI with https:// instead of wss:// I was able to add exceptions on all browsers, and now I could connect to the wss server.