Aimee
Aimee

Reputation: 143

Websockets sending garbage to server

I have created a WebSockets connection from my website to my home server running a python script listening on port 3000. I can connect to the server just fine as the server logs that a client has connected when I load my page. However each time I connect to the server, the server automatically fires my servers receivedMessage() function and prints out a bunch of garbage regarding the connection. The servers logs are below:

A new client has connected
Message Received:  GET / HTTP/1.1
Host: [Insert host name and port here]
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: null
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Sec-WebSocket-Key: 3DUmKD70X9p19IpLPjS2jg==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

The snippet of code to connect to the server is simple enough:

var theSocket = new WebSocket("ws://[insert host name]:3000");

    theSocket.onopen = function() {
        // Web Socket is connected, send data using send()
        theSocket.send("blah");
        alert("SENT");
     };

Also the alert box never shows up when the above script is run. What is causing all the connection details to print and how do I get it to send a simple string and successfully pop up the alert box?

Upvotes: 0

Views: 788

Answers (1)

Rob Cherry
Rob Cherry

Reputation: 56

Its likely that your WebSocket is never transitioning from CONNECTING to OPEN and thus never calling your onopen function. You need to make sure your python script is going through the Web Socket Handshake on the server side.

Upvotes: 1

Related Questions