如何使用socketio4Net向socket.io命名空间注册事件?

时间:2013-06-24 19:29:13

标签: c# node.js sockets socket.io socketio4net

我正在使用SocketIO4Net在我的worker角色中创建一个.NET socket.io客户端,我可以使用它连接到我的socket.io服务器。在转移了大量代码之后,我已经能够连接到命名空间,而不是文档中提到的。但我仍然无法向事件发送和接收消息。下面是我的代码,请告诉我如何将事件注册到socket.io .net客户端。对我的项目来说,我能够从我的worker角色向socket.io服务器事件发送消息非常重要。

broadcastSocketClient = new Client(localSocketUrl);

                broadcastSocketClient.Opened += SocketOpened;
                broadcastSocketClient.Message += SocketMessage;
                broadcastSocketClient.SocketConnectionClosed += SocketConnectionClosed;
                broadcastSocketClient.Error += SocketError;

                while (!broadcastSocketClient.IsConnected)
                {
                    broadcastSocketClient.Connect();
                }

                // register for 'connect' event with io server
                broadcastSocketClient.On("connect", (cn) =>
                {                      

                    var namespaceConnect = broadcastSocketClient.Connect("/namespacename");

                    // register for 'connect' event with io server
                    namespaceConnect.On("connect", (data) =>
                    {
                        namespaceConnect.Emit("test", "CONNECTED");

                        namespaceConnect.On("first", (message) =>
                        {
                            Console.WriteLine(message);

                        });
                    });    
                });                    

1 个答案:

答案 0 :(得分:0)

这是一个与https://stackoverflow.com/a/16002007/1168541非常相似的问题,但是一个会给你带来麻烦的方面就是你的代码连接:

while (!broadcastSocketClient.IsConnected)
            {
                broadcastSocketClient.Connect();
            }

您应该等待事件消息“已连接”,而不是爆发多次连接尝试。你永远不会给客户端在while循环中连接的机会。

尝试以下几点:

public class SampleClient
{
    private Client socket;
    private IEndPointClient nsTarget;
    private string localSocketUrl = "http:your_url_to_socketioserver";

    public void Execute()
    {
        Console.WriteLine("Starting SocketIO4Net Client Events Example...");


        socket = new Client(localSocketUrl);
        socket.Opened += SocketOpened;
        socket.Message += SocketMessage;
        socket.SocketConnectionClosed += SocketConnectionClosed;
        socket.Error += SocketError;

        // register for 'connect' event with io server
        socket.On("connect", (fn) =>
        {       // connect to namespace
            nsTarget = socket.Connect("/namespacename");
            nsTarget.On("connect", (cn) => nsTarget.Emit("test", new { data = "CONNECTED" }));
            nsTarget.On("first", (message) =>
            {
                Console.WriteLine("recv [socket].[update] event");
                Console.WriteLine("  raw message:      {0}", message.RawMessage);
                Console.WriteLine("  string message:   {0}", message.MessageText);
                Console.WriteLine("  json data string: {0}", message.Json.ToJsonString());
            });

        });

        // make the socket.io connection
        socket.Connect();
    }

    void SocketOpened(object sender, EventArgs e)
    {
        Console.WriteLine("SocketOpened\r\n");
        Console.WriteLine("Connected to ICBIT API server!\r\n");
    }

    void SocketError(object sender, ErrorEventArgs e)
    {
        Console.WriteLine("socket client error:");
        Console.WriteLine(e.Message);
    }

    void SocketConnectionClosed(object sender, EventArgs e)
    {
        Console.WriteLine("WebSocketConnection was terminated!");
    }

    void SocketMessage(object sender, MessageEventArgs e)
    {
        // uncomment to show any non-registered messages
        if (string.IsNullOrEmpty(e.Message.Event))
            Console.WriteLine("Generic SocketMessage: {0}", e.Message.MessageText);
        else
            Console.WriteLine("Generic SocketMessage: {0} : {1}", e.Message.Event, e.Message.Json.ToJsonString());
    }
    public void Close()
    {
        if (this.socket != null)
        {
            socket.Opened -= SocketOpened;
            socket.Message -= SocketMessage;
            socket.SocketConnectionClosed -= SocketConnectionClosed;
            socket.Error -= SocketError;
            this.socket.Dispose(); // close & dispose of socket client
        }
    }
}