I want to get started with the Mt. Gox websockets API, but I can't seem to get a good response from the server.
I'm sending the following (captured with Wireshark):
GET /mtgox HTTP/1.1
User-Agent: websocket-sharp/1.0
Upgrade: websocket
Connection: Upgrade
Host: websocket.mtgox.com
Sec-WebSocket-Key: LtH+YFgnW1N8KDLDbocsUQ==
Sec-WebSocket-Version: 13
and the server responds:
HTTP/1.1 400 Bad Request
I've tried sending the handshake via telnet and with this C# code and I still get a 400 Bad Request error:
class Program
{
static void Main(string[] args)
{
ClientWebSocket ws = new ClientWebSocket();
Task connectionTask = ws.ConnectAsync(new Uri("ws://websocket.mtgox.com/mtgox"), CancellationToken.None);
Console.Write("Connecting");
while (!connectionTask.IsCompleted)
{
Console.Write(".");
Thread.Sleep(100);
}
Console.WriteLine(ws.State);
}
}
(The last line printed is "Closed" and when I inspect the handshake with Wireshark I see the same 400 Bad Request)
Any ideas what I'm doing wrong? I can't see much documentation or samples using the websockets API so I don't really have anything to compare to :(
In response to sta's suggestion, I tried this (using WebsocketSharp):
static void Main(string[] args)
{
WebSocket ws = new WebSocket("ws://websocket.mtgox.com/mtgox");
ws.Origin = "http://websocket.mtgox.com";
ws.Connect();
ws.OnMessage += (sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
Console.WriteLine("{0}", e.Data);
}
};
Console.WriteLine("{0}", ws.IsAlive);
Console.ReadLine();
}
This displays FALSE
so there's still something I'm doing wrong :/ I haven't had a chance to dig into WebSocketSharp yet though
Update: With WebsocketSharp setting the Origin header correctly, Wireshark shows the mtgox server accepting the websocket upgrade:
HTTP/1.1 101 Switching Protocols\r\n
Upgrade: websocket\r\n
Connection: Upgrade\r\n
Sec-WebSocket-Accept: 2+1ghzJ/Itt6w1aBdUteghcsJHk=\r\n
\r\n
But WebSocketSharp still reports the connection as closed :(