private async Task InitSocket()
 {
     _socket                = _client.NewSocket();
     _socket.Connected     += () => BoxTextController.WriteText("Socket Connected", Color.green);
     _socket.Closed        += () => BoxTextController.WriteText("Socket closed", Color.red);
     _socket.ReceivedError += Debug.LogError;
 }
        //PUBLIC METHODS

        public void SendDataToMatch(string data, DataCode code)
        {
            if (_currMatch == null)
            {
                return;
            }

            _socket.SendMatchStateAsync(_currMatch.Id, code.ToInt(), data);
            BoxTextController.WriteText("Sending data to players: " + data, Color.yellow);
        }
        private void SubscribeSocketsEvents()
        {
            connectedOpponents = new List <IUserPresence>();

            _socket.ReceivedMatchmakerMatched += async matched =>
            {
                Debug.LogFormat("Matched result: {0}", matched);
                _currMatch = await _socket.JoinMatchAsync(matched);

                _self = _currMatch.Self;
                Debug.LogFormat("Self: {0}", _self);
                connectedOpponents.AddRange(_currMatch.Presences);

                OnMatchJoins(_currMatch.Presences);
            };
            //
            _socket.ReceivedMatchPresence += presenceEvent =>
            {
                OnMatchLeaves(presenceEvent.Leaves);

                connectedOpponents.Remove(_self);

                OnMatchJoins(presenceEvent.Joins);

                UnityMainThread.wkr.AddJob(() => _player.SetName(_self.Username));
                connectedOpponents.AddRange(presenceEvent.Joins);
            };
            //
            var enc = System.Text.Encoding.UTF8;

            _socket.ReceivedMatchState += newState =>
            {
                var content = enc.GetString(newState.State);
                var code    = newState.OpCode.ToDataCode();
                switch (newState.OpCode.ToDataCode())
                {
                case DataCode.POSITION:
                    Debug.Log("A custom opcode -- > NEW POSITION.");
                    break;

                default:
                    Debug.LogFormat("User '{0}'' sent '{1}'", newState.UserPresence.Username, content);
                    break;
                }

                UnityMainThread.wkr.AddJob(() => { OnNewDataState(newState.UserPresence.UserId, code, content); });
                BoxTextController.WriteText("Receive data--> " + content, Color.yellow);
            };
        }
        private void OnMatchJoins(IEnumerable <IUserPresence> joins)
        {
            foreach (var j in joins)
            {
                if (j.UserId == _self.UserId)
                {
                    BoxTextController.WriteText("You've entered in a game", Color.green);
                    continue;
                }

                Debug.LogFormat("Connected opponents: [{0}]", string.Join(",\n  ", connectedOpponents));

                BoxTextController.WriteText("Connected Opponents " + j.Username, Color.yellow);
                UnityMainThread.wkr.AddJob(() => { OnMatchPrecense(true, j); });
            }
        }
        private void OnMatchLeaves(IEnumerable <IUserPresence> leaves)
        {
            foreach (var l in leaves)
            {
                if (l.UserId == _self.UserId)
                {
                    BoxTextController.WriteText("You've exited a game", Color.red);
                    continue;
                }

                connectedOpponents.Remove(l);
                Debug.LogFormat("Disconnected opponents: [{0}]",
                                string.Join(",\n  ", l));

                BoxTextController.WriteText("Disconnect Opponent " + l.Username, Color.yellow);
                UnityMainThread.wkr.AddJob(() => { OnMatchPrecense(false, l); });
            }
        }
示例#6
0
 // Start is called before the first frame update
 void Start()
 {
     BoxTextController.SetChatBox(this);
 }