<p>It seems that the client should send the Origin header in opening handshake to the MtGox server.</p>

<p>If you use current websocket-sharp, you should do like the following before connecting.</p>

<p><code>
ws.Origin = "<a href="http://websocket.mtgox.com" rel="nofollow">http://websocket.mtgox.com</a>";
</code></p>

<p>Could you try?</p>

<p>Update:</p>

<p>Could you try again the following code?</p>

<pre><code>static void Main(string[] args)
{
    var ws = new WebSocket("ws://websocket.mtgox.com/mtgox?Currency=USD");
    ws.Origin = "http://websocket.mtgox.com";
    ws.OnOpen += (sender, e) =&gt;
    {
      ws.Send(
@"{
    ""op"": ""unsubscribe"",
    ""channel"": ""d5f06780-30a8-4a48-a2f8-7ed181b4a13f""
}"
      );
    };
    ws.OnMessage += (sender, e) =&gt;
    {
        if (!String.IsNullOrEmpty(e.Data))
        {
            Console.WriteLine("{0}", e.Data);
        }
    };
    ws.Connect();
    Console.ReadLine();
    ws.Close();
}
</code></pre>

<p>It seems that an exception has occurred when the client is receiving ticker type data in a while.</p>

<p>I'll investigate a little further.</p>

<p>Update:</p>

<p>I've found out the cause of the exception, and fixed websocket-sharp.</p>

<p>Could you try your code with current websocket-sharp?</p>

CC BY-SA 3.0
2013-05-12T02:22:28.550Z
I just tried this and WebsocketSharp reports the connection to be closed: (added the code to the question)
digitalPhonix
CC BY-SA 3.0
2013-11-21T03:07:13.077Z
This using wss:// works!
digitalPhonix
CC BY-SA 3.0
2013-05-07T05:56:33.513Z
2013-05-13T17:24:50.597Z
2013-05-13T17:24:50.597Z
sta
<p>For one the url should be <code>websocket.mtgox.com/mtgox</code>. Here is a <a href="https://github.com/arthurpham/node-mtgox-socketio-client" rel="nofollow">sample project</a> which uses the node.js socket.io library. </p>

<p>I dont have much experience in C#, but here is an implementation in node.js</p>

<pre><code>var io = require('socket.io-client');
var socket = io.connect( 'https://socketio.mtgox.com/mtgox' );

var message = {
  "op": "subscribe",
  "channel": "24e67e0d-1cad-4cc0-9e7a-f8523ef460fe" // this is the market depth channel
};

socket.send(JSON.stringify(message));

socket.on('message', function(message) { console.log(message); });
</code></pre>

<p>This will log market depth changes to the console.</p>

CC BY-SA 3.0
2013-05-01T04:38:45.977Z
Don't have reputation to vote up :( Thanks though! I'm going to try and figure out how the Node.JS example handshakes differently...
Changing my code to connect to https://socketio.mtgox.com/mtgox instead of what I had results in the server immediately closing the connection (doesn't even send a bad request response)
digitalPhonix
CC BY-SA 3.0
2013-05-01T15:37:17.533Z
Glad it helped. You can except an answer as correct however ;-)
Loourr
CC BY-SA 3.0
2013-05-02T11:30:25.153Z
I'd like to use the raw websocket api. Socket.io creates bad abstractions.
ralphtheninja
CC BY-SA 3.0
2013-05-01T02:49:51.180Z
2013-05-01T02:49:51.180Z
Loourr
<p>I want to get started with the Mt. Gox websockets API, but I can't seem to get a good response from the server.</p>

<p>I'm sending the following (captured with Wireshark):</p>

<pre><code>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
</code></pre>

<p>and the server responds:</p>

<pre><code>HTTP/1.1 400 Bad Request
</code></pre>

<p>I've tried sending the handshake via telnet and with this C# code and I still get a 400 Bad Request error:</p>

<pre><code>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);
    }
}
</code></pre>

<p>(The last line printed is "Closed" and when I inspect the handshake with Wireshark I see the same 400 Bad Request)</p>

<p>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 :(</p>

<p>In response to sta's suggestion, I tried this (using WebsocketSharp):</p>

<pre><code>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) =&gt;
    {
        if (!String.IsNullOrEmpty(e.Data))
        {
            Console.WriteLine("{0}", e.Data);
        }
    };
    Console.WriteLine("{0}", ws.IsAlive);
    Console.ReadLine();
}
</code></pre>

<p>This displays <code>FALSE</code> so there's still something I'm doing wrong :/ I haven't had a chance to dig into WebSocketSharp yet though</p>

<p>Update:
With WebsocketSharp setting the Origin header correctly, Wireshark shows the mtgox server accepting the websocket upgrade:</p>

<pre><code>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
</code></pre>

<p>But WebSocketSharp still reports the connection as closed :(</p>

CC BY-SA 3.0
2013-04-30T18:37:53.330Z
Funny, I just noticed the same thing, but I'm writing a websocket client for node.js.
ralphtheninja
CC BY-SA 3.0
2013-05-01T13:54:59.773Z
There seems to be a big difference between websocket.mtgox.com/mtgox server and the socketio.mtgox.com/mtgox... The sample Loourr posted below works for socketio (slightly different handshake) but I'm still not sure why I can't connect to the websocket one (with C# WebSockets or this websockets implementation for node: https://github.com/Worlize/WebSocket-Node )
digitalPhonix
<mtgox><api><c#>
MtGox websockets api returning "400 Bad Request"
digitalPhonix
# MtGox websockets api returning "400 Bad Request"

(Asked by: username_0 on 2013-04-30)

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 :(

**Tags:** mtgox, api, c#

> **Comments:**
> **username_1**: Funny, I just noticed the same thing, but I'm writing a websocket client for node.js.  
> **username_0**: There seems to be a big difference between websocket.mtgox.com/mtgox server and the socketio.mtgox.com/mtgox... The sample username_3 posted below works for socketio (slightly different handshake) but I'm still not sure why I can't connect to the websocket one (with C# WebSockets or this websockets implementation for node: https://github.com/Worlize/WebSocket-Node )  

---

## 2 answers

### Answer by username_2 on 2013-05-07. Score: 1 | Accepted answer

It seems that the client should send the Origin header in opening handshake to the MtGox server.

If you use current websocket-sharp, you should do like the following before connecting.

`ws.Origin = "http://websocket.mtgox.com";`

Could you try?

Update:

Could you try again the following code?

```
static void Main(string[] args)
{
    var ws = new WebSocket("ws://websocket.mtgox.com/mtgox?Currency=USD");
    ws.Origin = "http://websocket.mtgox.com";
    ws.OnOpen += (sender, e) =>
    {
      ws.Send(
@"{
    ""op"": ""unsubscribe"",
    ""channel"": ""d5f06780-30a8-4a48-a2f8-7ed181b4a13f""
}"
      );
    };
    ws.OnMessage += (sender, e) =>
    {
        if (!String.IsNullOrEmpty(e.Data))
        {
            Console.WriteLine("{0}", e.Data);
        }
    };
    ws.Connect();
    Console.ReadLine();
    ws.Close();
}

```

It seems that an exception has occurred when the client is receiving ticker type data in a while.

I'll investigate a little further.

Update:

I've found out the cause of the exception, and fixed websocket-sharp.

Could you try your code with current websocket-sharp?

> **Comments:**
> **username_0**: I just tried this and WebsocketSharp reports the connection to be closed: (added the code to the question)  
> **username_0**: This using wss:// works!  

---

### Answer by username_3 on 2013-05-01. Score: 0

For one the url should be `websocket.mtgox.com/mtgox`. Here is a [sample project](https://github.com/arthurpham/node-mtgox-socketio-client) which uses the node.js socket.io library.

I dont have much experience in C#, but here is an implementation in node.js

```
var io = require('socket.io-client');
var socket = io.connect( 'https://socketio.mtgox.com/mtgox' );

var message = {
  "op": "subscribe",
  "channel": "24e67e0d-1cad-4cc0-9e7a-f8523ef460fe" // this is the market depth channel
};

socket.send(JSON.stringify(message));

socket.on('message', function(message) { console.log(message); });

```

This will log market depth changes to the console.

> **Comments:**
> **username_0**: Don't have reputation to vote up :( Thanks though! I'm going to try and figure out how the Node.JS example handshakes differently... Changing my code to connect to https://socketio.mtgox.com/mtgox instead of what I had results in the server immediately closing the connection (doesn't even send a bad request response)  
> **username_3**: Glad it helped. You can except an answer as correct however ;-)  
> **username_1**: I'd like to use the raw websocket api. Socket.io creates bad abstractions.  

---

