I am attempting to consume a socket.io API using SocketIO4net however cannot seem to receive any messages. In the same VS2010 solution, I have a website with the following code:
<script src="https://api.icbit.se/socket.io/socket.io.js"></script>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script>
$(document).ready(function () {
var conn = io.connect('http://api-url');
conn.on('connect', function () {
alert('Connected');
conn.emit('message', { op: 'subscribe', channel: 'orderbook_BUM3' });
});
conn.on('message', function (data) {
console.log('Incoming message: ' + data.private);
});
});
</script>
This works fine and can connect to the API and receive messages. I then have the SocketIO4Net library and test project which I have put the following code into:
socket = new Client(http://api-url); // url to the nodejs / socket.io instance
socket.Opened += SocketOpened;
socket.Message += SocketMessage;
socket.SocketConnectionClosed += SocketConnectionClosed;
socket.Error += SocketError;
// register for 'connect' event with io server
socket.On("connect", (fn) =>
{
socket.Emit("message", new { op = "subscribe", channel = "orderbook_BUM3"
});
});
socket.On("message", (data) =>
{
Console.WriteLine("message received");
});
// make the socket.io connection
socket.Connect();
This connects and informs me of success however no messages are being received, or any error messages output. I'm fairly new to Socket.IO, is there anything I should be doing differently?
The issue is the default namespace SocketIO4Net is connecting & registering with. The sort answer is to make a small change in the on.connect event handler: Add a new variable for a IEndPointClient (say icbit), and on the socket connection, connect with the "icbit" namespace.
The SocketIO4Net default "On" handlers will handle namespace events as well, or you choose to register them directly with the given endpoint. (A single connection is made even with the additional namespace connection). You can read more about SO4N namespace
If you look at a websocket frame trace from a sample html page sample - you'll see it is using the "icbit" namespace:
Please see if this small workaround doesn't fix you up...I look further into why the querystring params / namespace connection and post that on the project site.
Jim