Your code is:
service_echo(Conn, {recv, Data}) ->
error_logger:info_msg("Conn: ~p~nData: ~p", [Conn, Data]),
sockjs:send(Data, Conn);
service_echo(_Conn, _) ->
error_logger:warning_msg("Conn: ~p~nData: ~p", [Conn, Data]),
ok.
The latter won't work: undefined Data. I modified it to be:
service_echo(Conn, R = {recv, Data}) ->
error_logger:info_msg("Conn: ~p~nData: ~p", [Conn, R]),
sockjs:send(Data, Conn);
service_echo(Conn, R) ->
error_logger:warning_msg("Conn: ~p~nData: ~p", [Conn, R]),
ok.
And all works fine.
> I've tried to debug the code. All i've got is the fact, that
> sockjs_cowboy_handler:init/3 function returns {upgrade, protocol,
> cowboy_http_websocket} as expected (on "upgrade" request, of course). But
> cowboy throws out 400 Bad Request response.
> It's time to post here a 'whyyyyyyyy?!.jpg'.
> Anybody, please, explain me this behavior. What i did wrong?..
How did you get that 400? If using Curl - than this is a correct behaviour,
you should see:
$ curl localhost:8081/echo/a/b/websocket -v
> GET /echo/a/b/websocket HTTP/1.1
>
< HTTP/1.1 400 Bad Request
< Connection: keep-alive
< Content-Length: 34
< Date: Fri, 24 Feb 2012 10:04:01 GMT
< Server: Cowboy
<
Can "Upgrade" only to "WebSocket".
Take a careful look what the Chrome Inspector says,
and make sure that your app doesn't crash on the server
side.
> P.S. I build application with rebar. can it be some kind of versions
> mismatch issue?..
Unlikely.
Cheers,
Marek
Right. I can't reproduce that. What version of firefox? What firebug?
Maybe broken proxy settings?
> Sorry for pastebin link, that's the old version of code. Current version is
> here: http://pastebin.com/v49QYrwB. So there is no problem with undefined
> Data.
Ok.
> Here is debug output to console i have when loading page and sending exactly
> one message:
> init() // init of strata_sockjs to handle 'GET /'
> handle() // reply the static page
> terminate() // terminate
> GET /echo/info // part of sockjs protocol, here we're reply
> 'websocket=true'
> // here is 'Upgrade' query performed, which is not
> logged
> POST /echo/368/cohtapcb/xhr_streaming // switching to XHR
> MSG: Conn: {sockjs_session,{<<"z7tvoCSrOBECYNHu1KMbJg==">>,<0.170.0>}},
> Data: init // initialization
> POST /echo/368/cohtapcb/xhr_send // have incoming message
> MESG: Conn: {sockjs_session,{<<"z7tvoCSrOBECYNHu1KMbJg==">>,<0.170.0>}},
> Data: <<"type something here">> // and log it
>
> What i want to say is: my application works, but it rolls down to XHR
> transport, despite the fact that websockets are enabled in my browser.
Right.
> Just to illustrate the situation - look on requests in firefox:
> http://dl.dropbox.com/u/5858965/sockjs-requests.png.
Okay, can you send me websocket request and response headers?
You can get them via firebug or you can run tcpdump, like:
$ tcpdump -ni any port 8081 -s0 -A
Marek
GET /echo/368/cah8dfr1/websocket HTTP/1.1 Host: localhost:8081 User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Connection: keep-alive, Upgrade Sec-WebSocket-Version: 8 Sec-WebSocket-Origin: http://localhost:8081 Sec-WebSocket-Key: 6U7LZSDrrhhlrzREdSh78A== Cookie: JSESSIONID=dummy Pragma: no-cache Cache-Control: no-cache Upgrade: websocketResponse:
HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 0 Date: Fri, 24 Feb 2012 10:57:26 GMT Server: Cowboy
Still no diagnoze, sorry. Running your request against code from
http://pastebin.com/v49QYrwB
gives proper response for me:
$ nc localhost 8081
GET /echo/368/cah8dfr1/websocket HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:10.0.2)
Gecko/20100101 Firefox/10.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive, Upgrade
Sec-WebSocket-Version: 8
Sec-WebSocket-Origin: http://localhost:8081
Sec-WebSocket-Key: 6U7LZSDrrhhlrzREdSh78A==
Cookie: JSESSIONID=dummy
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Sec-Websocket-Accept: Qy/XzIWK0ypzAnHKy5WEGqdCUs8=
Connection: Upgrade
�o
I'll try to verify the same thing on firefox.
But I'm running out of ideas. Can you repeat the netcat trick
as I did? (ie: run nc localhost 8081 and paste the request + newline)
and see what it responds?
What erlang version? What cowboy version? What sockjs version?
Marek
Am I missing something?
https://github.com/extend/cowboy/tags
doesn't show 0.5.0
The app file, says 0.5.0 indeed:
https://github.com/extend/cowboy/blob/master/src/cowboy.app.src
But that isn't pinned to any particular release.
What release specifically are you using?
Marek
I still can't reproduce it.
I uploaded the code to gist: https://gist.github.com/1901073
Here's what I do, step by step. Please follow the steps carefully and
tell me if you get the same result:
git clone git://gist.github.com/1901073.git
cd 1901073
make
I'm using :
* erlang R14B01
* $ rebar -V --> rebar version: 2 date: 20120210_110111 vcs: git ebb2345
* Firefox 10.0.2 with no proxy settings and firebug 1.9.1
After pointing Firefox to localhost:8081 I see the message:
[*] Connected (using: websocket)
In right box and it all seems to be working.
Marek
My guess is that you might have been using old version of cowboy.
Whatever. I'm glad it's working for you now.
Cheers,
Marek
Now, that's interesting!
Marek
Well, without doubt you do need to start sockjs and cowboy apps
before calling cowboy:start_listener.
I'm not a rebar expert so I can't comment on your reltool.config.
I would usually suggest to mention the dependencies in .app file.
Cheers,
Marek