Connect Websocket with Poco libraries

Moonlit picture Moonlit · Aug 30, 2013 · Viewed 7.3k times · Source

I am trying to connect to the Echo Test Websocket using the Poco C++ libraries. In order to do so here is my code which should set up the Websocket:

HTTPClientSession cs("echo.websocket.org");
HTTPRequest request(HTTPRequest::HTTP_GET, "/ws");
HTTPResponse response;

WebSocket* m_psock = new WebSocket(cs, request, response);
m_psock->close(); //close immidiately

However it does not work: I am getting an error message like this:

Poco::Exception: WebSocket Exception: Cannot upgrade to WebSocket connection: Not Found

Can anybody help?

Answer

Kevin H. Patterson picture Kevin H. Patterson · Nov 14, 2013

The 'Not Found' error is the standard HTTP 404 Not Found returned by the HTTP server. It generally means the resource you are requesting does not exist.

I got your code to work by changing the resource from "/ws" to "/":

HTTPRequest request(HTTPRequest::HTTP_GET, "/");

and adding the following line

request.set("origin", "http://www.websocket.org");

before creating the new WebSocket. I think it's a header pair that many (or all?) WebSocket servers expect.