0

I am using SocketIO4Net to create a .NET socket.io client in my worker role with which i can connect to my socket.io server. I have been able to connect to the namespace after shifting around a lot of code than what was mentioned in the documentation. But I am still not able to send and receive messages to events. Below is my code, please let me know how I can register events to the socket.io .net client. Its really important for my project that i am able to send messages to socket.io server events from my worker role.

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

                        });
                    });    
                });                    
Bitsian
  • 2,238
  • 5
  • 37
  • 72

1 Answers1

0

This is a very similar question to https://stackoverflow.com/a/16002007/1168541, but one area that's going to give you trouble is in your code to connect:

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

You should wait for the event message 'connected', rather than blast multiple connection attempts. You'll never give the client the chance to connect in the while loop.

Try something along these lines:

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
        }
    }
}
Community
  • 1
  • 1
Jim Stott
  • 810
  • 8
  • 10
  • Thank you. You answer works but i found out that the issue in my case was this http://stackoverflow.com/questions/17236342/newtonsoft-json-assembly-package-version-mismatch because of newtonsoft.json not loading properly , the event messages were not being sent or received properly. I am still facing this issue. Let me know if you know any fix – Bitsian Jun 25 '13 at 12:25
  • For now i have moved my code to my web role instead of the worker role, and there is no version mismatch there as there is no other library using newtonsoft.json – Bitsian Jun 25 '13 at 12:26
  • The bindingRedirect ref'd in the link you posted should get you loading the newer version of newtonsoft.json - no breaking changes that I'm aware of. You could always recompile socketio4net with a newer version of newtonsoft, then use that in your project as a workaround too. – Jim Stott Jun 25 '13 at 20:53