This project is read-only.

Problem with Client constructor

Jun 8, 2014 at 5:51 PM
Edited Jun 8, 2014 at 5:53 PM
Hi all,
I am developing a chat application and I have decided to make the client in C#, using the Socketio4Net.client library.

I have a issue when I construct the client. Here is my code:
  try
        {
            socket = new Client("localhost:8000");
            socket.On("txt", (data) =>
            {
                AddMessage(data.ToString());
            });
            socket.Connect();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
        if(socket.ReadyState.ToString() == "Connecting")
            socket.Emit("newuser", email);
        else
        {
            MessageBox.Show("Fallito nel connettersi al server.", "Errore");
            return;
        }
When I debug the code, it says that Client.Cs class can't be found; instead, if I do not debug the code, I don't get any errors (only one time I got a URI error) and when I get the socket.ReadyState if condition, the ReadyState is always None, while it should be "Connecting". I correctly inserted the SocketIo4Net.Client library in my project, and when I write the data type "Client", it is shown in the suggestion list. The nodejs server is listening on port 8000 and in the router configuration I have correctly configured the ports (external: 8000 and internal:8000).
Coordinator
Jun 8, 2014 at 7:31 PM
I'm not sure what you might have going on in your debug environment - so I'll leave that aside for the moment...

Try to use the event nature of the library as you would with socket.io in node.js (e.g. use : socket.On("connect" vs socket.ReadyState test
yourMethodHere()
{

    socket = new Client("http://127.0.0.1:8000/"); // url to nodejs 
    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("newuser", email);
    });

    

    // make the socket.io connection AFTER all other event handlers have been setup
    socket.Connect();
}

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.JsonEncodedMessage.ToJsonString());
        }

void SocketOpened(object sender, EventArgs e)
        {

        }
Jun 8, 2014 at 7:42 PM
could you explain me the purpose of these:
socket.Opened += SocketOpened;
socket.Message += SocketMessage;
socket.SocketConnectionClosed += SocketConnectionClosed;
socket.Error += SocketError;
Thank you ;)
Jun 8, 2014 at 7:46 PM
cl3v3r wrote:
could you explain me the purpose of these:
socket.Opened += SocketOpened;
socket.Message += SocketMessage;
socket.SocketConnectionClosed += SocketConnectionClosed;
socket.Error += SocketError;
Thank you ;)
I've followed the tutorial given here:
http://aravindvs.com/blog/?p=181
http://www.codeproject.com/Articles/598281/SocketIO-Programming-in-Csharp-using-SocketIO-Net
And there weren't instruction like these.
Coordinator
Jun 8, 2014 at 8:53 PM
To be fair - none of the examples you point to use "if(socket.ReadyState.ToString() == "Connecting") " either.

The documentation at https://socketio4net.codeplex.com/documentation does show them, as as the sample project.

So what doesn't make sense about and Opened, Message, SocketConnectionClosed and Error events to you - are you trolling me, or do you not understand event delegates? I'm not sure these event names could be named much more self-descriptive, but I'll definitely take suggestions for some different name's if have any...

Jim


Jun 8, 2014 at 9:02 PM
jstott wrote:
To be fair - none of the examples you point to use "if(socket.ReadyState.ToString() == "Connecting") " either. The documentation at https://socketio4net.codeplex.com/documentation does show them, as as the sample project. So what doesn't make sense about and Opened, Message, SocketConnectionClosed and Error events to you - are you trolling me, or do you not understand event delegates? I'm not sure these event names could be named much more self-descriptive, but I'll definitely take suggestions for some different name's if have any... Jim
If you download the source code of the project, you can see that there is that line of code.
Yes the problem is that I don't understand event delegates. I think I will have to study them.
Jun 9, 2014 at 11:49 AM
Edited Jun 9, 2014 at 1:29 PM
OK I've understood how the delegates work. Thanks for your attention.
Jun 9, 2014 at 1:36 PM
Edited Jun 9, 2014 at 5:59 PM
Ok I've solved the original problem, all I did was to integrate the Socket4IO project in my project.

But now I'm getting a bad request error when I connect the client to the server (error 400) when I debug.