private void ParseMessage(byte[] bytes) { if (previousCode == 0) { var code = bytes[0]; if (code == Protocol.USER_ID) { Id = System.Text.Encoding.UTF8.GetString(bytes, 2, bytes[1]); if (OnOpen != null) { OnOpen.Invoke(this, EventArgs.Empty); } } else if (code == Protocol.JOIN_REQUEST) { var requestId = bytes[1]; IRoom room; if (connectingRooms.TryGetValue(requestId, out room)) { room.Id = System.Text.Encoding.UTF8.GetString(bytes, 3, bytes[2]); endpoint.Path = "/" + room.Id; endpoint.Query = "colyseusid=" + this.Id; var processPath = ""; var nextIndex = 3 + room.Id.Length; if (bytes.Length > nextIndex) { processPath = System.Text.Encoding.UTF8.GetString(bytes, nextIndex + 1, bytes[nextIndex]) + "/"; } room.SetConnection(CreateConnection(processPath + room.Id, room.Options)); room.OnLeave += OnLeaveRoom; if (rooms.ContainsKey(room.Id)) { rooms.Remove(room.Id); } rooms.Add(room.Id, room); connectingRooms.Remove(requestId); } else { throw new Exception("can't join room using requestId " + requestId.ToString()); } } else if (code == Protocol.JOIN_ERROR) { string message = System.Text.Encoding.UTF8.GetString(bytes, 2, bytes[1]); if (OnError != null) { OnError.Invoke(this, new ErrorEventArgs(message)); } } else if (code == Protocol.ROOM_LIST) { previousCode = code; } } else { if (previousCode == Protocol.ROOM_LIST) { var message = MsgPack.Deserialize <List <object> >(new MemoryStream(bytes)); var requestId = Convert.ToInt32(message[0]); List <object> _rooms = (List <object>)message[1]; RoomAvailable[] availableRooms = new RoomAvailable[_rooms.Count]; for (int i = 0; i < _rooms.Count; i++) { IDictionary <string, object> room = (IDictionary <string, object>)_rooms[i]; RoomAvailable _room = ObjectExtensions.ToObject <RoomAvailable>(_rooms[i]); availableRooms[i] = _room; } roomsAvailableRequests[requestId].Invoke(availableRooms); roomsAvailableRequests.Remove(requestId); } previousCode = 0; } }
private void ParseMessage(byte[] recv) { var message = MsgPack.Deserialize <List <object> > (new MemoryStream(recv)); var code = (byte)message [0]; if (code == Protocol.USER_ID) { this.id = (string)message [1]; if (this.OnOpen != null) { this.OnOpen.Invoke(this, EventArgs.Empty); } } else if (code == Protocol.JOIN_ROOM) { var requestId = (byte)message [2]; Room room; if (this.connectingRooms.TryGetValue(requestId, out room)) { room.id = (string)message [1]; this.endpoint.Path = "/" + room.id; this.endpoint.Query = "colyseusid=" + this.id; room.SetConnection(CreateConnection(room.id, room.options)); room.OnLeave += OnLeaveRoom; this.rooms.Add(room.id, room); this.connectingRooms.Remove(requestId); } else { throw new Exception("can't join room using requestId " + requestId.ToString()); } } else if (code == Protocol.JOIN_ERROR) { if (this.OnError != null) { this.OnError.Invoke(this, new ErrorEventArgs((string)message [2])); } } else if (code == Protocol.ROOM_LIST) { var requestId = Convert.ToInt32(message[1]); List <object> _rooms = (List <object>)message[2]; RoomAvailable[] rooms = new RoomAvailable[_rooms.Count]; for (int i = 0; i < _rooms.Count; i++) { IDictionary <string, object> room = (IDictionary <string, object>)_rooms[i]; RoomAvailable _room = ObjectExtensions.ToObject <RoomAvailable>(_rooms[i]); rooms[i] = _room; } this.roomsAvailableRequests[requestId].Invoke(rooms); this.roomsAvailableRequests.Remove(requestId); } else { if (this.OnMessage != null) { this.OnMessage.Invoke(this, new MessageEventArgs(message)); } } }