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.
根据以下答案,除了WebSocket-Protocol标头外,这是不可能的: Websockets客户端API中的HTTP标头
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.