0

I'm trying to use SocketIO4Net for communication with my NodeJS server. As mentioned by the NodeJS SocketIO doc, we can do something like:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.on('ferret', function (name, fn) {
    fn('woot');
  });
});

Where there's a fn refers to a callback function. I need equivalent function in SocketIO4Net but I can't find it any where. Only 2 signatures I found here are:

public virtual void On(string eventName, Action<IMessage> action);
public virtual void On(string eventName, string endPoint, Action<IMessage> action);

So I'm wondering, is this function available in SocketIO4Net? Or do I have to emit another event to confirm with the server?

yaoxing
  • 4,003
  • 2
  • 21
  • 30

2 Answers2

0

So I found a solution myself. I'm using the source code from http://socketio4net.codeplex.com/, which mentioned a develop branch is located at: https://github.com/jstott/socketio4net/tree/develop It doesn't compile for me though. So I started from the codeplex package and try to implement this function myself.

It turns out that the SocketIo4Net package at codeplex is incomplete. I have to fix some bugs first. The patched code is now in my repository: https://git01.codeplex.com/forks/zhangyaoxing/socketio4net

By using this package you can manually send the AckMessage back to server by:

// Simulate a ack callback because SocketIO4Net doesn't provide one by default.
var msgText = JsonConvert.SerializeObject(new object[] {
    // object you want to send back
});
var ack = new AckMessage()
{
    AckId = msg.AckId,
    MessageText = msgText
};
this.socket.Send(ack);

I have to admit this is not an elegant way of doing the job at all. But it might be the only way to do it now since the original writer doesn't seem to be maintaining the project anymore. Hope someone can finish the unfinished part in future.

yaoxing
  • 4,003
  • 2
  • 21
  • 30
0

If you take a look at the sample project (on codeplex), you'll find the sample CallbackExample. The client can emit a message, and when processed by the server & executes the callback, that callback will be in turn executed back on the client.

Console.WriteLine("Emit [socket].[messageAck] - should recv callback [root].[messageAck]");
socket.Emit("messageAck", new { hello = "mama" }, null, (callback) =>
    {
        var jsonMsg = callback as JsonEncodedEventMessage; // callback will be of type JsonEncodedEventMessage, cast for intellisense
        Console.WriteLine(string.Format("callback [root].[messageAck]: {0} \r\n", jsonMsg.Args));
    });
Jim Stott
  • 810
  • 8
  • 10
  • I see. When I read the code, I thought `AckMessage` is the object I'm supposed to use for callback. But it seems incomplete because it doesn't resolve the server message correctly, I mean it doesn't get the AckId because of a regular expression issue. – yaoxing Jun 27 '14 at 09:32
  • I not sure what RE your referring to, but if you want an ack message back from the server, you'll need to use emite vs send. It's just about identical to the socket.io client code: socket.emit('ferret', 'tobi', function (data) { console.log(data); // data will be 'woot' }); – Jim Stott Jun 27 '14 at 23:17
  • Sorry for the late response (too late I know). hmm, yes the `Emit` has a callback. what I want is the `callback` for `On`. Like I described in my node example, there's a callback for `On`, too. – yaoxing Aug 04 '14 at 11:01