Question

I'm trying to create a WebSocket client to connect to an existing server (mtgox api).

As a starting point, to figure out connecting to WebSockets, I found this example code https://github.com/jaspervdj/websockets/blob/master/example/client.hs

The problem is that Mtgox requires headers to be sent along when it connects, I'm just not sure how to send them.

Update: To help work this out, I created a simple websocket server too. When I connect to it via a JavaScript WebSocket from my Chrome JavaScript console, I see the following headers:

 requestHeaders = [("Upgrade","websocket"),("Connection","Upgrade"),
("Host","127.0.0.1:8001"),("Origin","chrome://newtab"),("Pragma","no-cache"),
("Cache-Control","no-cache"),("Sec-WebSocket-Key","yOsPEMHx9AyT9u3ssNma/Q=="),
("Sec-WebSocket-Version","13"),("Sec-WebSocket-Extensions","x-webkit-deflate-frame"),
("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36")]

Whereas, when I connect through via the Haskell client, I see only the following headers:

requestHeaders = [("Host","127.0.0.1"),("Connection","Upgrade"),
("Upgrade","websocket"),("Sec-WebSocket-Key","X3hMDW4fAau53dbz7w4MTw=="),
("Sec-WebSocket-Version","13")]

I don't know which of the headers are actually required by MtGox, but my plan was to just send the same ones that Chrome sends, since that works.

Was it helpful?

Solution 2

Instead of using connect I used connectWith, which allows the "Origin" header to be set. Although I can't see how to add any other headers, this is the one that MtGox requires. As long as I provide an origin, with some kind of http-based url, it connects successfully.

WS.connectWith "websocket.mtgox.com" 80 "/mtgox" (Just "http://anything") Nothing app

It would seem that it is mostly unnecessary to add any further headers, and presumably that is why no mechanism is provided. However, looking at the source, it's possible by copy-pasting the code of connectWith and connectWithSocket to change the headers that are added to the Request object.

OTHER TIPS

According to the below answer it's not possible except for the WebSocket-Protocol header: HTTP headers in Websockets client API

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top