LiveOrDevTrying / WebsocketsSimple

WebsocketsSimple provides an easy-to-use and customizable Websocket Server and Websocket Client. The server is created using a TcpListener and upgrades a successful connection to a WebSocket. The server and client can be used for non-SSL or SSL connections and authentication (including client and server SSL certification validation) is provided for identifying the clients connected to your server. Both client and server are created in .NET Standard and use async await functionality.
Apache License 2.0
21 stars 2 forks source link

Error Event: Length cannot be less than zero. (Parameter 'length') #6

Closed tomvdb closed 1 year ago

tomvdb commented 1 year ago

I'm trying to to a simple websocket server and have some javascript from a website connect to it. Server starts fine, but when the client tries to connect then I get a WsServer_ErrorEvent triggered with the message:

Error Event: Length cannot be less than zero. (Parameter 'length')

This is my ws server setup:

wsServer = new WebsocketServer(new ParamsWSServer(port : 8089, pingIntervalSec : 30 ));
wsServer.MessageEvent += WsServer_MessageEvent;
wsServer.ConnectionEvent += WsServer_ConnectionEvent;
wsServer.ErrorEvent += WsServer_ErrorEvent;
wsServer.ServerEvent += WsServer_ServerEvent;
wsServer.Start();

This is my javascript code for connection:

function connectWebsocket()
{
    console.log("Connecting to WS Server");
    sockurl = "ws://" + "127.0.0.1:8089";
    websocket = new WebSocket(sockurl);

    websocket.onopen = function () {
        console.log("Websocket Connected");
    };

    websocket.onerror = function () {
        console.log("Websocket Error");
    };

    websocket.onclose = function () {
        console.log("Websocket Closed");
    };

    websocket.onmessage = function (message) 
    {
        console.log(message);
    }

}

Server starts fine, and I get the error event when my client tries to connect. Am I missing an important settings?

Thanks!

cameronwmckay commented 1 year ago

I had the same problem, the solution I used was to use a "channel" on the web socket address. So in your example above, if you can, change the address in this code and the javascript to be:

sockurl = "ws://" + "127.0.0.1:8089/ws";

LiveOrDevTrying commented 1 year ago

Hello and thank you for using WebsocketsSimple! I have fixed this bug and tested successfully with your client. The code is commit and new packages are available. Please give it a try and let me know if you have any other problems, questions, or suggestions for improvements. Thanks again for using WebsocketsSimple!