Aros
Aros

Reputation: 319

Websockets++ CORS settings

I am trying to run some websocket communication between C++ project using websocket++ with stand-alone asio and a client side created in angular running currently in a browser.

It seems I have an issue with the handshake, namely some CORS settings.

On the client side I get this: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3001/socket.io/?EIO=3&transport=polling&t=MV3LZsv. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

And at the same time I get this in the cpp side console: [2018-12-18 15:09:39] [error] Handshake ended with HTTP error: 426 [2018-12-18 15:09:39] [fail] WebSocket Connection [::ffff:127.0.0.1]:57762 v0 "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0" /socket.io/?EIO=3&transport=polling&t=MV1ffJg 426 websocketpp:28 Upgrade required

Well, I found this issue on the GitHub repo: https://github.com/zaphoyd/websocketpp/issues/679

So I tried adding the validate handler, but it doesn't work, it's not even called so the Access-Control-Allow-Origin is not even added to the request.

Any ideas?

//Edit: updated the client side error message

Upvotes: 1

Views: 1237

Answers (1)

alexm
alexm

Reputation: 510

Take a look at https://github.com/andrei-markeev/ddpserver/blob/master/examples/websocketpp.cpp#L44

In short, try use the connection_ptr to append you ‘Access-Control-Allow-Origin’.

websocket_server::connection_ptr con = wsServer->get_con_from_hdl(hdl);
con->append_header("access-control-allow-origin", "*");

Upvotes: 1

Related Questions