nhn / socket.io-client-unity3d

socket.io-Client for Unity3D, which is compatible with socket.io v1.x
Other
167 stars 43 forks source link

Event body wrong parsed #31

Open SebastianVargas opened 6 years ago

SebastianVargas commented 6 years ago

When the event data contains ", " the parse fails. Because is looking for ", " to split it and get eventName and evenData.

If I set on Socket.cs in line 322

pkt.body = "[\"gameFinished\",{\"message\":\"Good job!, play in the new tournament\"}]";

Will cause Warning message: gameFinished",{"message":"Good job event doesn't have a handler, because is taking gameFinished",{"message":"Good job as the eventName

Most robust way to separate even name and data should be implemented.

sfranzyshen commented 5 years ago

Event body parsed poorly (say that 10 times fast) the way I figure ... testing for a comma followed by a space was a way to prevent the data string from having a leading space ... why not keep it simple and trim off any leading spaces and leave the data string properly intact (i mean comma space is commonly used!) here is a snippet;

case SocketPacketTypes.EVENT:
    if (!pkt.HasBody) {
        Debug.LogWarningFormat("{0} has no body(data)", pkt.ToString());
        return;
    }

    var seperateIndex = pkt.body.IndexOf(',');
    var seperatorLen = 1;

    var eventName = pkt.body.Substring(2, seperateIndex - 3);
    if (!_handlers.ContainsKey(eventName)) {
        Debug.LogWarningFormat("{0} event doesn't have a handler", eventName);
        break;
    }

    var data = pkt.body.Substring(seperateIndex + seperatorLen, pkt.body.Length - seperateIndex - seperatorLen - 1).TrimStart();
    _handlers[eventName](data);
    break;

additionally I'm running this on ... socket.io 2.1.1 and unity 2017.4.14f1 #2 Support socket.io v2.0.2