Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Connection closed before receiving a handshake response #12

Closed
mkkhedawat opened this issue Jul 22, 2015 · 14 comments
Closed

Connection closed before receiving a handshake response #12

mkkhedawat opened this issue Jul 22, 2015 · 14 comments

Comments

@mkkhedawat
Copy link

I simply did

git clone https://github.com/eidheim/Simple-WebSocket-Server.git

Then compiled in linux

g++ -O3 -std=c++1y ws_examples.cpp -lboost_system -lcrypto -lpthread -o ws_examples

Wrote a sample JS client as specified in documentation

<!Doctype html>
<html>
    <head>
        <title>
            Web Socket 
        </title>
   </head>
<body>
    <script>

var ws=new WebSocket("ws://localhost:8080/echo");
ws.onmessage=function(evt){console.log(evt.data);};
ws.send("test");


    </script>
</body>
</html>

But when I run client and server, It gives error

Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
WebSocket connection to 'ws://localhost:8080/echo' failed: Connection closed before receiving a handshake response

Can you please direct where is things going wrong ?

Ubuntu 14
Boost 0.9.8
No trace was received on server side

@eidheim
Copy link
Owner

eidheim commented Jul 22, 2015

You need to wait for the connection to establish, so instead of the line ws.send("test") do something like this:

ws.onopen=function(evt){
  ws.send("test");
}

@mkkhedawat
Copy link
Author

No, That is resolving only

Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.

That was understood.

But Handshake Error is still there

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

@eidheim
Copy link
Owner

eidheim commented Jul 22, 2015

What browser do you use? This happens when you reload (and maybe leave) the page while having an open websocket connection. Just means that you have not closed it. To remove the message I guess you can do a ws.close() when the page unloads, but I do not know the JavaScript code for this.

@mkkhedawat
Copy link
Author

I am using chrome.
On Firefox it says

Firefox can't establish a connection to the server at ws://localhost:8080/echo.

I am not reloading or navigating away.
Web-socket connection is not being established I believe.
If it did , shouldn't I get trace in serve side ?

@eidheim
Copy link
Owner

eidheim commented Jul 22, 2015

Try this html file from a similar issue posted on this project:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WebSocket Test</title>
</head>
<body>
<script>
window.onload=function(){
  var ws=new WebSocket("ws://localhost:8080/echo");
  ws.onmessage=function(evt){console.log(evt.data);};
  ws.onopen=function(evt){
    ws.send("test");
    setTimeout(function(){ws.close();}, 1000);
  }
}
</script>
</body>
</html>

This one closes the connection after 1 sec, also the charset is set and its using the window.onload function. And yes you should see output on the terminal running ws_examples.

@mkkhedawat
Copy link
Author

Nah ! Same error. Is it still working at your system ? Please use the same steps as me.
If yes tell me the configs ?

@eidheim
Copy link
Owner

eidheim commented Jul 23, 2015

Yes this is working on Debian stable, testing and OS X. Do you get output when you start ./ws_examples?

@eidheim
Copy link
Owner

eidheim commented Jul 23, 2015

Also please try cmake .;make;make test and see if the tests are passed.

@eidheim
Copy link
Owner

eidheim commented Jul 23, 2015

You can also open the console in Chrome, and type in the JavaScript commands yourself:

var ws=new WebSocket("ws://localhost:8080/echo");
ws.onmessage=function(evt){console.log(evt.data);};
ws.send("test"); //try this line again if the connection is not up yet.

@mkkhedawat
Copy link
Author

I do not get anything on running ./ws_examples

When I do cmake, It ask for compiler 4.9.
I have 4.8.2 installed on Ubuntu 14. It was able to get compiled via traditional method.

@eidheim
Copy link
Owner

eidheim commented Jul 23, 2015

Strange you managed to compile it on g++ 4.8, I think regex was not supported before 4.9, and that is why you need g++ 4.9. I think it should be possible to install this on Ubuntu. Maybe do an upgrade on your system? g++ 4.9 is available in debian stable now, and should thus be installable on all its derivatives like Ubuntu (hopefully).

@ZX-Diablo
Copy link

I've made g++4.8 compilable fork but haven't updated code for a long time thou

@mkkhedawat
Copy link
Author

Okay , It seems the issue here then.

It was compiling with g++ 4.8.2 and running but not giving any traces.
But when compiled with 4.9 , it is running fine.

Thanks for your time.

Closing Issue :)

@taimur38
Copy link

taimur38 commented Jun 11, 2017

I'm having this issue currently, and am running on windows.
Visual Studio 2015, I'm just pointing to Simple-WebSocket-Server in AdditionalIncludeDirectories.

Any ideas on how I can get around this? Looks like I might just need to update visual studio

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants