No event dispatched when args are missing

Aug 22, 2013 at 2:41 PM
Edited Aug 22, 2013 at 2:42 PM
Hello,

As I experienced it, it may happen that no args are submitted when triggering an event on the nodejs side. For instance :
socket.emit("my event");
Doing so, the transmitted packet will just look like that :
5:::{"name":"my event"}
Without the "args" property.

But on the c# side, this message will by fully deserialized only if "name" AND "args" are defined :
EventMessage.cs: line 64 :

if (!string.IsNullOrEmpty(evtMsg.MessageText) &&
                    evtMsg.MessageText.Contains("name") &&
                    evtMsg.MessageText.Contains("args"))
                {
                    evtMsg.Json = JsonEncodedEventMessage.Deserialize(evtMsg.MessageText);
                    evtMsg.Event = evtMsg.Json.Name;
                }
                else
                    evtMsg.Json = new JsonEncodedEventMessage();
So no args means no event name on the c# side, and means my "socket.On("my event" ...)" callback is never called.

What do you think ? Am i missing something ?

Benjamin
Coordinator
Aug 22, 2013 at 4:25 PM
Hello Benjamin,

I don't think you're missing something...it stems from the Socket.io specs calling out for name and args to be mandatory:
an event is like a json message, but has mandatory name and args fields.
That said, line 64 as you point out looks for the args parameter. If not found, a 'message' event will be called with the 'event' name rather an the event itself.
Since the server sends events w/o any args or issue, it's probably reasonable to remove the args check and/or just force to a empty object for empty args.

Couple of thoughts to get around the situation below. As such, are those options workable and/or how big of an overall issue do you think this represents?
  • 1) On the server side, emit an empty object if you your payload could be null: socket.emit('my event', payload || {});
  • 2) On the client side, register to the message event along these lines will be called:
socket.Message += SocketMessage;
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());
}
Generic SocketMessage: {"name":"my event"}
Aug 23, 2013 at 8:28 AM
Hello,

Thanks for the answer.

Indeed the best workaround in my case is to systematically add a parameter, even empty. That should do the work for awhile :)

However, there might be an issue with those lines anyway :
                    EventMessage.cs: line 64 :

                    if (!string.IsNullOrEmpty(evtMsg.MessageText) &&
                                        evtMsg.MessageText.Contains("name") &&
                                        evtMsg.MessageText.Contains("args"))
                                    {
                                        evtMsg.Json = JsonEncodedEventMessage.Deserialize(evtMsg.MessageText);
                                        evtMsg.Event = evtMsg.Json.Name;
                                    }
                                    else
                                        evtMsg.Json = new JsonEncodedEventMessage();
Indeed, the "if" only checks if the message string contains "name" and "args". So a message like that :
5:::{"fake":"name and args"} 
Will be considered as valid event though it shouldn't.

Thanks for the headsup anyway,

Best regards,

Benjamin