[Libwebsockets] Unable to communicate with node server over Websocket

Anders Brander anders at brander.dk
Wed Aug 20 13:51:07 CEST 2014


Hi,

On Wed, 2014-08-20 at 16:59 +0530, sthustfo wrote:
> I was of the opinion that in order to connect to the server, you need
> to have a path like -  "/socket.io/1/websocket/RJmiyNAyu1PZFRp5WiLn".
> Where "RJmiyNAyu1PZFRp5WiLn" is an id that is returned by the nodejs
> (socket.io) server in response to GET request sent over HTTP (xhr).

I used another library to do the initial HTTP GET request, extract token
from body and then initiate websocket request using said token.

Something like this for an OLD version of libwebsockets and libsoup:
	...

	/* Connect to socket.io server */
	SoupSession *session = soup_session_sync_new();

	url = g_strdup_printf("http://%s/socket.io/1", host);
	SoupMessage *msg = soup_message_new("GET", url);
	g_free(url);

	soup_session_send_message(session, msg);

	/* We should have a response body in the form of
	 *
"session_id:heartbeat_timeout:connection_closing_timeout:supported_transports"
	 */

	content = g_strsplit(msg->response_body->data, ":", 4);
	session_id = content[0];
	heartbeat_timeout = atoi(content[1]);

	path = g_strdup_printf("/socket.io/1/websocket/%s", session_id);
	g_strfreev(content);

	remote->priv->client = libwebsocket_client_connect(
		remote->priv->context,
		host,
		80,
		0,
		path,
		"",
		"",
		NULL,
		0);

	/* Setup heartbeat */

	...

Maybe this can help somebody...

/Anders



More information about the Libwebsockets mailing list