示例#1
0
        public async void Socket_AwaitedTasks_AreCanceled()
        {
            var id      = Guid.NewGuid().ToString();
            var session = await _client.AuthenticateCustomAsync(id);

            await _socket.ConnectAsync(session);

            var matchmakerTask1 = _socket.AddMatchmakerAsync("+label.foo:\"val\"", 15, 20);
            var matchmakerTask2 = _socket.AddMatchmakerAsync("+label.bar:\"val\"", 15, 20);
            await _socket.CloseAsync();

            await Assert.ThrowsAsync <TaskCanceledException>(() => Task.WhenAll(matchmakerTask1, matchmakerTask2));
        }
        public async Task ShouldCreateSocketAndConnect()
        {
            var session = await _client.AuthenticateCustomAsync($"{Guid.NewGuid()}");

            var completer = new TaskCompletionSource <bool>();

            _socket.Connected += () => completer.SetResult(true);

            await _socket.ConnectAsync(session);

            Assert.True(await completer.Task);
            await _socket.CloseAsync();
        }
示例#3
0
        public async Task ShouldThrowSocketExceptionWhenSendingMatchDataAfterClosingSocket()
        {
            var session = await _client.AuthenticateCustomAsync($"{Guid.NewGuid()}");

            await _socket.ConnectAsync(session);

            var match = await _socket.CreateMatchAsync();

            await _socket.CloseAsync();

            await Assert.ThrowsAsync <SocketException>(async() =>
            {
                await _socket.SendMatchStateAsync(match.Id, 1, new { hello = "world" }.ToJson(), null);
            });
        }
        /// <summary>
        /// Connects <see cref="Socket"/> to Nakama server to enable real-time communication.
        /// </summary>
        /// <returns>Returns true if socket has connected successfully.</returns>
        private async Task <bool> ConnectSocketAsync()
        {
            try
            {
                if (_socket != null)
                {
                    await _socket.CloseAsync();
                }
            }
            catch (Exception e)
            {
                Debug.LogWarning("Couldn't disconnect the socket: " + e);
            }

            try
            {
                await Socket.ConnectAsync(Session);

                return(true);
            }
            catch (Exception e)
            {
                Debug.LogError("An error has occured while connecting socket: " + e);
                return(false);
            }
        }
示例#5
0
 private async void OnApplicationQuit()
 {
     if (_socket != null)
     {
         await _socket.CloseAsync();
     }
 }
示例#6
0
        /// <summary>
        /// Connects <see cref="socket"/> to Nakama server to enable real-time communication.
        /// </summary>
        /// <returns>Returns true if socket has connected successfully.</returns>
        public async Task <bool> ConnectSocketAsync()
        {
            try
            {
                if (m_Socket != null)
                {
                    await m_Socket.CloseAsync();
                }
            }
            catch (Exception e)
            {
                Debug.Log($"Couldn't disconnect the socket: {e}");
            }

            try
            {
                await socket.ConnectAsync(session);

                return(true);
            }
            catch (Exception e)
            {
                Debug.Log("An error has occured while connecting socket: {e}");
                return(false);
            }
        }
        // -------------------------------------------

        /*
         * Disconnect
         */
        public async Task Disconnect(IMatch match)
        {
            await Socket.LeaveMatchAsync(match);

            await Socket.CloseAsync();

            Socket = null;
        }
        private async void Start()
        {
            var deviceId = SystemInfo.deviceUniqueIdentifier;
            var session  = await _client.AuthenticateDeviceAsync(deviceId);

            Debug.LogFormat("Session user id: '{0}'", session.UserId);

            var roomUsers = new List <IUserPresence>(10);

            _socket                = _client.NewSocket();
            _socket.Connected     += () => Debug.Log("Socket connected.");
            _socket.ReceivedError += Debug.LogError;
            _socket.Closed        += () =>
            {
                Debug.Log("Socket closed.");
                roomUsers.Clear();
            };
            _socket.ReceivedChannelPresence += presenceEvent =>
            {
                foreach (var presence in presenceEvent.Leaves)
                {
                    roomUsers.Remove(presence);
                }

                roomUsers.AddRange(presenceEvent.Joins);
                Debug.LogFormat("Room users: [{0}]", string.Join(",\n  ", roomUsers));
            };
            _socket.ReceivedChannelMessage += message => Debug.LogFormat("Received message: '{0}'", message);
            await _socket.ConnectAsync(session);

            // Join chat channel.
            var channel = await _socket.JoinChatAsync(RoomName, ChannelType.Room);

            roomUsers.AddRange(channel.Presences);

            // Simulate a disconnect.
            await Task.Delay(TimeSpan.FromSeconds(3));

            await _socket.CloseAsync();

            await Task.Delay(TimeSpan.FromSeconds(3));

            // Reconnect and rejoin chat channel(s).
            await _socket.ConnectAsync(session);

            var channel2 = await _socket.JoinChatAsync(RoomName, ChannelType.Room);

            roomUsers.AddRange(channel2.Presences);
            Debug.Log("Rejoined chat!");
        }
示例#9
0
 private void OnApplicationQuit()
 {
     _socket?.CloseAsync();
 }
 Task IAsyncLifetime.DisposeAsync()
 {
     return(_socket.CloseAsync());
 }
 public Task DisposeAsync() => _socket.CloseAsync();