简体   繁体   English

在Haskell中连接到WebSocket时如何添加标题

[英]How to add headers when connecting to a websocket in Haskell

I'm trying to create a WebSocket client to connect to an existing server ( mtgox api ). 我正在尝试创建一个WebSocket客户端以连接到现有服务器( 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 首先,要弄清楚连接到WebSocket的方法,我发现了以下示例代码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. 问题是Mtgox需要在连接时发送标头,但我不确定如何发送标头。

Update: To help work this out, I created a simple websocket server too. 更新:为了帮助解决这个问题,我也创建了一个简单的websocket服务器。 When I connect to it via a JavaScript WebSocket from my Chrome JavaScript console, I see the following headers: 当我从Chrome JavaScript控制台通过JavaScript WebSocket连接到它时,看到以下标头:

 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: 而当我通过Haskell客户端连接时,仅看到以下标头:

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. 我不知道MtGox实际需要哪些标头,但我的计划是只发送与Chrome发送的标头相同的标头,因为那样可以。

根据以下答案,除了WebSocket-Protocol标头外,这是不可能的: Websockets客户端API中的HTTP标头

Instead of using connect I used connectWith , which allows the "Origin" header to be set. 代替使用的connect我用connectWith ,这允许“起源”报头被设置。 Although I can't see how to add any other headers, this is the one that MtGox requires. 尽管我看不到如何添加其他标头,但这是MtGox所需的标头。 As long as I provide an origin, with some kind of http-based url, it connects successfully. 只要提供一个基于HTTP的url作为来源,它就可以成功连接。

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. 但是,查看源代码 ,可以通过复制粘贴connectWithconnectWithSocket的代码来更改添加到Request对象的标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM