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?

4

2 回答 2

0

所以我自己找到了解决方案。我正在使用来自http://socketio4net.codeplex.com/的源代码,其中提到了一个开发分支位于:https ://github.com/jstott/socketio4net/tree/develop它不适合我编译尽管。所以我从codeplex包开始,尝试自己实现这个功能。

事实证明,codeplex 的 SocketIo4Net 包是不完整的。我必须先修复一些错误。修补后的代码现在在我的存储库中:https ://git01.codeplex.com/forks/zhangyaoxing/socketio4net

通过使用此包,您可以通过以下方式手动将 AckMessage 发送回服务器:

// 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);

我不得不承认,这根本不是一种优雅的工作方式。但这可能是现在唯一的方法,因为原始作者似乎不再维护该项目。希望将来有人能完成未完成的部分。

于 2014-04-19T09:56:46.200 回答
0

如果您查看示例项目(在 codeplex 上),您会发现示例 CallbackExample。客户端可以发出一条消息,当服务器处理并执行回调时,该回调将依次在客户端上执行。

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));
    });
于 2014-06-27T02:44:11.287 回答