socket.io-client-unity3d icon indicating copy to clipboard operation
socket.io-client-unity3d copied to clipboard

Event body wrong parsed

Open SebastianVargas opened this issue 7 years ago • 1 comments

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.

SebastianVargas avatar Jun 07 '18 01:06 SebastianVargas

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

sfranzyshen avatar Nov 06 '18 06:11 sfranzyshen