Exemple #1
0
        private void listenData(string recievePacket)
        {
            string[] messages = recievePacket.Substring(0, recievePacket.Length - 1).Split(';');
            foreach (string message in messages)
            {
                Packet packet = Packet.unPackPacket(message);
                _serverTs = packet.ts;
                UnityMainThreadDispatcher.Instance().Enqueue(() => InvokeRepeating("timerTick", 0, 0.001f));
                switch (packet.issue)
                {
                case "authOK2":
                    JObject joo = JObject.Parse(packet.body);
                    isTurn = joo["isTurn"].ToObject <bool>();
                    _netId = joo["netId"].ToObject <string>();
                    UnityMainThreadDispatcher.Instance().Enqueue(() =>
                    {
                        _clients = new Dictionary <string, List <StreambleObjectTag> >();

                        if (isTurn == false)
                        {
                            gameObject.AddComponent <StreamObjects>();
                            StreamObjects.instance.streamer = streamer;
                            StreamObjects.instance.SetNetId(netId);
                        }

                        gameObject.AddComponent <PingCalculator>();
                        PingCalculator.instance.serverConnected = true;
                        ConnectedToServer();
                    });
                    break;

                case "call-event":
                    PlayerModel  player   = this.findPlayerInRoom(packet.sender);
                    eventRecieve eve_recv = JsonConvert.DeserializeObject <eventRecieve>(packet.body);
                    this.CallEvent(eve_recv.eventName, player);
                    break;

                case "call-event-with-body":
                    PlayerModel  player2   = this.findPlayerInRoom(packet.sender);
                    eventRecieve eve_recv2 = JsonConvert.DeserializeObject <eventRecieve>(packet.body);
                    this.CallEvent(eve_recv2.eventName, player2, eve_recv2.body);
                    break;

                case "updateQeue":
                    if (isTurn == false)
                    {
                        UnityMainThreadDispatcher.Instance().Enqueue(() =>
                        {
                            NetWorkTransform transform = Packet.convertTotransform(packet.body);
                            StreamObjects.instance.updateQeue(packet.sender, transform);
                        });
                    }
                    else
                    {
                        Debug.LogWarning("[GamingCloud] Your game type is turnbased!");
                    }
                    break;

                case "join":
                    OnjoinPlayer joinPlayerStatus = JsonConvert.DeserializeObject <OnjoinPlayer>(packet.body);
                    PlayerModel  playerModel      = new PlayerModel(joinPlayerStatus.player.netId, joinPlayerStatus.player.name);

                    this._isMasterPlayer = joinPlayerStatus.room.roomCreator.netId == _netId;
                    this._room           = joinPlayerStatus.room;

                    UnityMainThreadDispatcher.Instance().Enqueue(() =>
                    {
                        // اگر جدید است
                        if (!joinPlayerStatus.pre)
                        {
                            // اگر فرستنده با بازیکن الان یکی باشد
                            if (packet.sender == _netId)
                            {
                                _PlayerInfo = playerModel;
                                this.OnJoined();     // جوین شدی!
                                if (isTurn == false)
                                {
                                    StreamObjects.instance.StartStream();
                                }
                            }
                            else
                            {
                                // بازیکن جدید اومده
                                this.OnJoinNewPlayer(playerModel);
                            }
                        }
                        else
                        {
                            // این بازیکن قبلا توی اتاق بوده
                            this.PreviousPlayerInRoom(playerModel);
                        }

                        if (instatiateClientsOnJoin && _netId != packet.sender)
                        {
                            this.InstatiateStreambleObjects(packet.sender);
                        }
                    });
                    break;

                case "removeRoom":
                    OnCloseRoom(_room, _isMasterPlayer);
                    _room           = null;
                    _isMasterPlayer = false;
                    break;

                case "update-room-data":
                    JObject jo  = JObject.Parse(packet.body);
                    string  key = jo["key"].ToObject <string>();

                    if (jo["dl"].ToObject <bool>())
                    {
                        _room.data.Remove(key);
                        UnityMainThreadDispatcher.Instance().Enqueue(() =>
                                                                     DeleteDataFromRoomTrigger(findPlayerInRoom(packet.sender), key));
                    }
                    else
                    {
                        _room.data = jo["data"].ToObject <JObject>();
                        UnityMainThreadDispatcher.Instance().Enqueue(() =>
                                                                     NewDataOnRoomTrigger(findPlayerInRoom(packet.sender), key));
                    }

                    break;

                case "leave-player":
                case "dissconnect":
                    UnityMainThreadDispatcher.Instance().Enqueue(() =>
                    {
                        disconnectPlayer dc = JsonConvert.DeserializeObject <disconnectPlayer>(packet.body);

                        if (this.removeClientsAfterDC)
                        {
                            DestroyAllClients(dc.player.netId);
                        }

                        _clients.Remove(dc.player.netId);

                        _room           = dc.room;
                        _isMasterPlayer = _room.roomCreator.netId == _netId;

                        if (dc.isLeave)
                        {
                            OnLeavePlayer(dc.player);
                        }
                        else
                        {
                            OnDisconnetPlayer(dc.player);
                        }
                    });

                    break;

                case "error":
                    try
                    {
                        int status = int.Parse(packet.body);
                        multiplayerErrors error = (multiplayerErrors)status;
                        UnityMainThreadDispatcher.Instance().Enqueue(() =>
                        {
                            OnError(error);
                        });
                    }
                    catch (System.Exception) { }
                    break;
                }
            }
        }
Exemple #2
0
 ///<summary>
 ///when has room closed
 ///</summary>
 public virtual void OnCloseRoom(RoomModel removedRoom, bool isYouMaster)
 {
 }