Question

I am connecting CC3000 to a node.js server using socket.io. I have used the following library to create a websocket https://github.com/chadstachowicz/socket_io_arduino_cc3000

In SocketIOClient.cpp, it creates a TCP connection, gets a session-id(sid). It disconnects and creates another TCP connection and uses the sid to upgrade the connection to websocket. For this the client(here CC3000) sends the following header information:

client.print(F("GET /socket.io/1/websocket/"));
client.print(sid);
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.println(hostname);
client.println(F("Origin: ArduinoSocketIOClient"));
client.println(F("Upgrade: WebSocket"));    // must be camelcase ?!
client.println(F("Connection: Upgrade\r\n"));

After this request the client waits for HTTP 101 response from the server. But the server is not sending any response. It logs as warning "websocket connection invalid" and ends the connection.

Is the protocol for creating websocket fine or is there any missing information in the header?

Also, I want to know what should the value of 'Origin' be? During first handshake it is "Arduino" whereas in the second handshake it is "ArduinoSocketIOClient".

Was it helpful?

Solution

You are missing the Sec-WebSocket-Key and Sec-WebSocket-Version headers, key part of the WebSocket protocol handshake.

This is how an actual handshake looks:

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top