jaspervdj / websockets

A Haskell library for creating WebSocket-capable servers
http://jaspervdj.be/websockets
BSD 3-Clause "New" or "Revised" License
407 stars 113 forks source link

WebSocket connection to 'ws://localhost:9160/' failed: Connection closed before receiving a handshake response #64

Open adinapoli opened 10 years ago

adinapoli commented 10 years ago

Hi Jasper! :)

I have stumble upon a peculiar behaviour when hacking on the Tidal project: in a nutshell trying to establish a connection on port 9160 on my machine (Mac OS X 10.9.2 Maverick with GHC 7.8.2) fails in different flavours, whereas with any other ports (e.g 9161) seems to work. How I reproduced it:

WebSocket connection to 'ws://localhost:9160/' failed: Connection closed before receiving a handshake response 

If I change the port on both sides (server and client.js) the connection works. I though at first it might have been that port 9160 is already taken by some service, but netstat -a | grep 9160 shows nothing. It's interesting though, because this seems to be exactly the problem; I've got the same error trying to open a socket on a port already taken (.e.g 5432 is where my postgresql listen); the server.lhs didn't complain, but the client.js did. But in this last example, netstat -a was clearly indicanting that port 5432 was occupied.

On Tidal I had to submit this PR:

https://github.com/yaxu/Tidal/pull/21

because this line:

https://github.com/yaxu/Tidal/blob/master/Sound/Tidal/Tempo.hs#L167

was throwing an exception like "ghc: Parse exception: not enough input".

I think the two scenarios are somewhat related, but I do not have a clue about what's going on here and how come it seems to happen on just that port, even though according to netstat -a it should be available.

Any insight? Thanks, and see you at the ZuriHack :P

Alfredo

jaspervdj commented 10 years ago

Hey Alfredo! I think Mac only shows used port for your own processes. This thread seems to confirm that: http://stackoverflow.com/questions/4421633/who-is-listening-on-a-given-tcp-port-on-mac-os-x. Perhaps try with the sudo lsof solution described in the first answer?

adinapoli commented 10 years ago

Sure, I'll do that as soon as I get home and update the issue accordingly :)

Thanks Jasper!

adinapoli commented 10 years ago

@jaspervdj haha, bingo! I had a java process listening (probably Cassandra). Glad it was a simple cause which triggered the error, even though is a bit confusing in both its incarnations (aka the error reported in the title on this PR and the one in the PR I linked you). How difficult would be to fail with a more descriptive error message?

Cheers! Alfredo

jaspervdj commented 10 years ago

B On Apr 14, 2014 5:26 PM, "Alfredo Di Napoli" notifications@github.com wrote:

Sure, I'll do that as soon as I get home and update the issue accordingly :)

Thanks Jasper!

— Reply to this email directly or view it on GitHub.

adinapoli commented 10 years ago

@jaspervdj didn't you get the latest update? In a nutshell yes the problem was an occupied port of my machine, and I'm leaving this open just to discuss whether we can make WS gracefully fail with a more descriptive message :)

Feel free to close the issue if you think nothing can be done in this direction :)