javascriptc#websocket-sharp

Client using websocket to not connecting


My client just shows the status as closed. My C# application is using websocket-sharp:

WebSocketServer mWebSocketServer;

try {
  // Creation of server, ipWebSocketAddr = "ws://192.168.5.73/posttest.html
  // mcintWebSocketPort = 443
  mWebSocketServer = new WebSocketServer(ipWebSocketAddr, mcintWebSocketPort);
  mWebSocketServer.AddWebSocketService<clsQuintexMsg>("/clsQuintexMsg");
} 
catch (Exception ex)  
{
  FrmMain.UpdateTaskbar(ex.ToString());
}

In my JavaScript:

try {
  webSocket = new WebSocket(cstrServerSocketIP);
  
  //Setup timeout timer to timeout if webSocket does not connect
  tmrCheckWebSocket = setInterval(function() {
    var strMsg = "Checking webSocket (" + cstrServerSocketIP + "), ";
    if (typeof webSocket == "object" &&
      typeof webSocket.readyState == "number") {
      if (webSocket.readyState == WebSocket.CONNECTING) {
        strMsg += "connecting";
      } else if (webSocket.readyState == WebSocket.OPEN) {
        strMsg += "open (connected)";
      } else if (webSocket.readyState == WebSocket.CLOSING) {
        strMsg += "closing";
      } else if (webSocket.readyState == WebSocket.CLOSED) {
        strMsg += "closed";
      }
    } else {
      strMsg += "not a valid websocket!";
    }
    DebugMsg(strMsg);
  }, cintCheckWebSocketInterval);
  
  webSocket.onclose = (event) => {
    var strReason = "";
    if (typeof event == "object") {
      if (typeof event.code == "number") {
        strReason += "code[" + event.code + "]";
        var strError = cobjWebSocketErrors[event.code];
        if (typeof strError == "string") {
          strReason += ":" + strError;
        }
      }
      if (typeof event.reason == "string" && event.reason.length > 0) {
        if (strReason.length > 0) {
          strReason += ", ";
        }
        strReason += "reason:\"" + event.reason + "\"";
      }
    }
    DebugMsg("webSocket.onclose " + strReason);
  };
  
  webSocket.onerror = (event) => {
    DebugMsg("webSocket.onerror" + ((typeof event == "object" && typeof event.data != "undefined") ? ":" + String(event.data) : ""));
  };
  
  webSocket.onmessage = (event) => {
    DebugMsg("webSocket.onmessage");
    if (event.data instanceof ArrayBuffer) {
      console.lob("Received ArrayBuffer");
      console.lob(event.data);
    }
    webSocket.close();
  };
  
  webSocket.onopen = () => {
    DebugMsg("webSocket.onopen");
  };
  
  //Set-up timer to send requests for data updates
  InstallServiceTimer(cintServiceRequestInterval);
} catch (e) {
  DebugMsg(e);
}

Just found an exception is being thrown in c# application when calling start:

System.InvalidOperationException
  HResult=0x80131509
  Message=There is no server certificate for secure connection.
  Source=websocket-sharp
  StackTrace:
   at WebSocketSharp.Server.WebSocketServer.start()
   at WebSocketSharp.Server.WebSocketServer.Start()
   at CheetahCS.clsServer..ctor(String strRoot, String strDefault, Int32 intWWWPort, Int32 intWebSocketPort) in clsServer.cs:line 131

  This exception was originally thrown at this call stack:
    WebSocketSharp.Server.WebSocketServer.start()
    WebSocketSharp.Server.WebSocketServer.Start()
    clsServer.clsServer(string, string, int, int) in clsServer.cs

Why does it think this is a secure connection?

It seems there is a bug in the WebSocketSharp code when passing the IPAddress and port seperately it defaults to a secure connection. I changed the above to use a string with the prefix ws://address:443, now no exception.


Solution

  • Normally 443 is the port for secure wss while ws should default to 80 (or 8080), not quite sure why it manifests like that though, but def shouldn't use 443 for this setup