Example #1
0
        private void CloseClientSocket(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = e.UserToken as AsyncUserToken;

            // close the socket associated with the client
            try
            {
                token.AcceptSocket.Shutdown(SocketShutdown.Send);
            }
            // throws if client process has already closed
            catch (Exception) {}

            token.AcceptSocket.Close();
            if (m_clients.TryRemove(token.ID, out _))
            {
                // Free the SocketAsyncEventArg so they can be reused by another client
                m_maxNumberAcceptedClients.Release();

                Console.WriteLine("ID [ {0} ] client has been disconnected from the Server. There are {1} clients connected to the server", token.ID, m_clients.Count);

                // Free the SocketAsyncEventArg so they can be reused by another client
                m_readWritePool.Push(e);

                OnClientClosed?.Invoke(token.ID);
            }
        }
            /// <summary>
            /// Al ocurrir un error en la conexión WebSocket
            /// </summary>
            /// <param name="e"></param>
            protected override void OnError(ErrorEventArgs e)
            {
                if (OnClientClosed == null)
                {
                    return;
                }

                Debug.WriteLine(this, "Ocurrio un error en una conexión socket: " + e.Message, VerbosityLevel.Warning);

                OnClientClosed?.Invoke(this, Context.WebSocket);
            }
            /// <summary>
            /// Al cerrarse la conexión WebSocket
            /// </summary>
            /// <param name="e">Información del cierre</param>
            protected override void OnClose(CloseEventArgs e)
            {
                if (OnClientClosed == null)
                {
                    return;
                }

                Debug.WriteLine(this, "Se desconecto correctamente una conexión socket: " + e.Reason, VerbosityLevel.Default);

                OnClientClosed?.Invoke(this, Context.WebSocket);
            }