This project is read-only.

Websocket + tls example

Nov 20, 2012 at 8:49 AM
Edited Nov 20, 2012 at 8:50 AM

Hi, I can't get an example of wss:// websocket client working.

I have created and successfully tested a wss example with

1. Server: SuperWebSocket

2. Client: Firefox / Chrome

Now I was trying to develop my own client with WebSocket4Net but I can't get the connection to run.


           m_WebSocketServer = new WebSocketServer();
            m_WebSocketServer.Setup(new RootConfig(), new ServerConfig
                Port = 8086,
                Ip = "Any",
                MaxConnectionNumber = 100,
                Mode = SocketMode.Sync,
                Name = "SuperWebSocket Server",
                Security = "tls",
                Certificate = new SuperSocket.SocketBase.Config.CertificateConfig
                    FilePath = @"C:\\Certificate.pfx",
                    Password = "CertificatePassword",
                    IsEnabled = true
            }, SocketServerFactory.Instance);
            m_WebSocketServer.NewMessageReceived  += new SessionEventHandler<WebSocketSession, string>(m_WebSocketServer_NewMessageReceived);
            m_WebSocketServer.NewSessionConnected += new SessionEventHandler<WebSocketSession>(m_WebSocketServer_NewSessionConnected);
            m_WebSocketServer.SessionClosed += new SessionEventHandler<WebSocketSession,CloseReason>(m_WebSocketServer_SessionDisonnected); 



            m_WebSocket = new WebSocket("wss://localhost:8086");
            m_WebSocket.Closed += new EventHandler(m_WebSocket_Closed);
            m_WebSocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(m_WebSocket_MessageReceived);
            m_WebSocket.Opened += new EventHandler(m_WebSocket_Opened);
            m_WebSocket.EnableAutoSendPing = true;


Both client and server are 'silent', i.e. the callbacks do not log any activity.

If I close the client, however, the server acknowledges a disconnection, with a 'SocketError' in the CloseReason.


Any thoughts?

Nov 20, 2012 at 8:53 AM

Did you catch something in Error event of the client?

Nov 20, 2012 at 9:03 AM

Whoa, thanks for the swift answer!

Exception : "RemoteCertificateNameMismatch"


How can I grant access to any certificate? Again, the certificate works just fine in browsers.

Nov 20, 2012 at 9:23 AM

The certificate's CN should be same the URI's host.

If the uri is "wss://localhost:8086", the CN of the certificate should be "localhost".

Nov 20, 2012 at 9:53 AM

Ok, I see.

Is there anyway to bypass the common name security check instead of creating the certificate, run selfSSL, add the binding...?

Nov 20, 2012 at 10:13 AM
Edited Nov 20, 2012 at 10:15 AM

According the source code, if the dll is DEBUG version, this error would be ignored. 

But you need set AllowUnstrustedCertificate to be true.

Nov 20, 2012 at 10:30 AM

The AllowUnstrustedCertificate did the trick.


Thanks for the support!