Esempio n. 1
0
        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;
            }
        }