lipp / lua-websockets

Websockets for Lua.
http://lipp.github.com/lua-websockets/
MIT License
399 stars 114 forks source link

"bad protocol" Error #48

Closed drasko closed 9 years ago

drasko commented 9 years ago

I tried with several WebSocket clients and I am constantly getting "bad protocol" error.

Is the code functional?

I am using an example "copas echo server" with freshly installed lua5.1 on Debian Linux:

drasko@Lenin:~/lua$ lua5.1 server.lua 
bad protocol

I tried test with both this code from the browser (both Chrome and Firefox):

<!doctype html>
<html><head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <script src="bootstrap/js/bootstrap.min.js"></script>

    </head>

    <body lang="en">
    <h1>WebSocket Echo example</h1>
    <button id="btnTest" type="button" class="btn btn-primary btn-lg">Primary</button>

    <script>
        var port = "8080";
        var uri = "/";
        var host = "localhost";
        ws = new WebSocket("ws://" + host + ":" + port + uri);
        ws.onopen    = function()  {console.log('[*] open', ws.protocol);};
        ws.onclose   = function()  {console.log('[*] close');};
        $(document).ready(function(){
            $("#btnTest").click(function(){
                ws.send("TEST");
            });
        });
    </script>

</body></html>

And also this code from Python: https://github.com/liris/websocket-client

Same result - "bad protocol".

Best regards, Drasko

lipp commented 9 years ago

thanks for reporting. strange, the code is used on daily basis... how did you install it?

drasko commented 9 years ago

I installed it via luarocks. I just said:

sudo luarocks install lua-websockets

I installed luarocks via apt-get.

drasko commented 9 years ago

Reinstalled now from git - built with

luarocks make rockspecs/lua-websockets-scm-1.rockspec

following the instructions.

Still same thing - "bad protocol"

How can I debug?

lipp commented 9 years ago

could you try to add the 'echo' protocol, please:

ws = new WebSocket("ws://" + host + ":" + port + uri, "echo");

lipp commented 9 years ago

i just tried to connect from browser to the "echo-server-copas.lua" manually and it worked fine. (besides the automated tests are passing).

lipp commented 9 years ago

you could debug with a "real" debugger (like zerobrane studio) or by adding prints to the sources. the interesting spot should be "server_copas.lua" lines 100 to 111 (this is where the "bad protocol" error is generated.

lipp commented 9 years ago

ahhh... i just compared the README.md echo server example and there is no default protocol specified, that means that u MUST define the protocol:

ws = new WebSocket(..., "echo");

the second param the the websocket ctor ("echo") is the important thing here!

drasko commented 9 years ago

Yes, indeed! Thanks, it is working now.

But why are we obliged to define the protocol like this? Can you please elaborate on this, it would be very helpful!

I have been used this JavaScript code before with other WebSocket servers (like Tornado, etc..), and I have never defined protocol before.

lipp commented 9 years ago

you can define a "default" handler, which will be chosen if no protocol has been provided by the client:

https://github.com/lipp/lua-websockets/blob/master/API.md#configdefault

the copas echo server (https://github.com/lipp/lua-websockets/blob/master/examples/echo-server-copas.lua) does both, as it provides a "explicit" echo protocol handler and a "default" handler (which - in this case- is the same.

@drasko is http://we-io.net/index.html your project? maybe this could be of use for you: http://jetbus.io