コード例 #1
0
 public AuthenticationProvider(string ip, int port, WebSocket webSocket) : this(
         "ws://" + (ip ?? GetIpAddress()) + $":{port}/",
         webSocket)
 {
     playerEventsReader = playerEvents.Reader;
     playerEventsWriter = playerEvents.Writer;
 }
コード例 #2
0
 public Listener(ServerHandle handle)
 {
     listenerSocket = new WebSocket()
     {
         NoDelay = false
     };
     globalChat  = new ChatChannel(this);
     this.handle = handle;
 }
コード例 #3
0
        public AuthenticationProvider(string url, WebSocket webSocket)
        {
            Task.Run(async() =>
            {
                while (await playerEventsReader.WaitToReadAsync())
                {
                    while (playerEventsReader.TryRead(out var playerEvent))
                    {
                        try
                        {
                            var player = playerEvent.Player;
                            if (playerEvent.Connected)
                            {
                                var client = AltNetworking.CreateClient();
                                lock (client)
                                {
                                    if (!client.Exists)
                                    {
                                        AltNetworking.Module.ClientPool.Remove(client.Token);
                                        continue;
                                    }

                                    playerTokens[client.Token] = player;
                                    playerTokenAccess[player]  = client.Token;
                                    player.SetNetworkingClient(client);

                                    lock (player)
                                    {
                                        if (player.Exists)
                                        {
                                            player.Emit("streamingToken", url, client.Token);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                player.RemoveNetworkingClient();
                                if (playerTokenAccess.Remove(player, out var token))
                                {
                                    playerTokens.Remove(token);
                                }
                                else
                                {
                                    continue;
                                }

                                if (!AltNetworking.Module.ClientPool.Remove(token, out var client))
                                {
                                    continue;
                                }
                                ManagedWebSocket clientWebSocket;
                                lock (client)
                                {
                                    clientWebSocket = client.WebSocket;
                                }

                                if (clientWebSocket == null)
                                {
                                    continue;
                                }
                                try
                                {
                                    await webSocket.CloseWebSocketAsync(clientWebSocket,
                                                                        WebSocketCloseStatus.NormalClosure,
                                                                        "disconnected");
                                }
                                catch (Exception exception)
                                {
                                    Console.WriteLine(exception);
                                }
                            }
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine(exception);
                        }
                    }
                }
            });
            Alt.OnPlayerConnect += (player, reason) =>
            {
                playerEventsWriter.WriteAsync(new PlayerEvent(player, true));
            };
            Alt.OnPlayerDisconnect += (player, reason) =>
            {
                playerEventsWriter.WriteAsync(new PlayerEvent(player, false));
            };
        }