Exemple #1
0
        public async void JoinOrCreateRoom(string psw = "123456", ColyseusClientResultEvent callback = null)
        {
            if (_JoinRoomLock)
            {
                return;
            }
            _JoinRoomLock = true;
            if (currentState == NetState.JoinARoom)
            {
                _JoinRoomLock = false;
                callback?.Invoke(ColyseusClientResult.Failed, ALREADY_INROOM_ERROCODE);
                return;
            }
            try
            {
                room = await client.JoinOrCreate <RoomState>(roomName, new Dictionary <string, object>() { { "psw", psw }, { "uuid", UUID } });

                _JoinRoomLock = false;
            }
            catch (Exception e)
            {
                _JoinRoomLock = false;
                Debug.Log(e.Message);
                callback?.Invoke(ColyseusClientResult.Failed, GetErrorCode(e));
                throw;
            }
            lastHeartBeat = GetTimeStamp();
            this.RegisRoomEvent(room);
            callback?.Invoke(ColyseusClientResult.Success, room);
            currentState = NetState.Connect;
        }
Exemple #2
0
        public async void CreateRoom(string name = "gameroom", string psw = "123456", ColyseusClientResultEvent callback = null)
        {
            if (_CreateRoomLock)
            {
                return;
            }
            _CreateRoomLock = true;
            if (currentState == NetState.JoinARoom)
            {
                _CreateRoomLock = false;
                Debug.LogError("Already in a room!!!!!!!!!!!");
                callback?.Invoke(ColyseusClientResult.Failed, ALREADY_INROOM_ERROCODE);
                return;
            }
            try
            {
                var option = new Dictionary <string, object>()
                {
                    { "roomName", name }, { "psw", psw }, { "ip", localIP }, { "uuid", UUID }
                };
                room = await client.Create <RoomState>(roomName, option);

                onCreateRoom    = callback;
                _CreateRoomLock = false;
            }
            catch (Exception e)
            {
                _CreateRoomLock = false;
                Debug.Log("join error :" + e.Message);
                callback?.Invoke(ColyseusClientResult.Failed, GetErrorCode(e));
            }
            lastHeartBeat = GetTimeStamp();
            this.RegisRoomEvent(room);
            currentState = NetState.Connect;
        }
Exemple #3
0
 void OnStateChangeHandler(RoomState state, bool isFirstState)
 {
     if (currentState == NetState.Connect)
     {
         currentState = NetState.JoinARoom;
         onCreateRoom?.Invoke(ColyseusClientResult.Success, room);
         onCreateRoom = null;
     }
     else
     {
         // update the state
         Trigger(MsgId.UpdateRoomInfo, state);
     }
 }
Exemple #4
0
 public async void LeaveRoom(ColyseusClientResultEvent callback = null)
 {
     if (room == null || currentState == NetState.Disconnect)
     {
         return;
     }
     try
     {
         await room.Leave(false);
     }
     catch (Exception)
     {
         Clear();
         callback?.Invoke(ColyseusClientResult.Failed, null);
         throw;
     }
     Clear();
     callback?.Invoke(ColyseusClientResult.Success, room);
 }
Exemple #5
0
        public async void ConnectToServer(string ip, string port, ColyseusClientResultEvent callback)
        {
            this.m_ServerIP = ip;
            string endpoint = string.Format("ws://{0}:{1}", m_ServerIP, port);

            Debug.Log("Connect to server:" + endpoint);
            // Connect to the colyeus Server
            client = ColyseusManager.Instance.CreateClient(endpoint);
            var result = await client.IsServerAvailable();

            if (result.result)
            {
                Debug.Log("Server is available version:" + result.version);
                callback?.Invoke(ColyseusClientResult.Success, null);
                ClientUpdator.Instance.StopCoroutine(HeartBeat());
                ClientUpdator.Instance.StartCoroutine(HeartBeat());
            }
            else
            {
                callback?.Invoke(ColyseusClientResult.Failed, null);
            }
        }
Exemple #6
0
        public async void GetAvailableRooms(ColyseusClientResultEvent callback)
        {
            CustomRoomAvailable[] roomsAvailable;
            try
            {
                roomsAvailable = await client.GetAvailableRooms <CustomRoomAvailable>(roomName);
            }
            catch (Exception)
            {
                callback?.Invoke(ColyseusClientResult.Failed, null);
                throw;
            }
            List <CustomRoomAvailable> validRooms = new List <CustomRoomAvailable>();

            foreach (var item in roomsAvailable)
            {
                if (item != null && item.clients > 0 && item.metadata != null)
                {
                    validRooms.Add(item);
                }
            }
            callback?.Invoke(ColyseusClientResult.Success, validRooms);
        }