Пример #1
0
        public async Task StartAsync()
        {
            // handle incoming data
            try {
                while (true)
                {
                    await OnMessage(await protocol.GetMessage());
                }

                // handle websock closing
            } catch (WebsockCloseException ex) {
                await OnClose(ex.Code, ex.Reason);

                // close client and destroy stream on exit
            } finally {
                if (server.GetClients().Contains(this))
                {
                    server.GetClients().Remove(this);
                }
                protocol.Dispose();
            }
        }
Пример #2
0
        // perform asynchronous close
        public async Task CloseAsync(ushort code, byte[] reason)
        {
            // TODO: implement close code checking for ranges

            byte[] closeCode = BitConverter.GetBytes(code);
            Array.Reverse(closeCode); // little to big endian

            byte[] closeFrame = new byte[closeCode.Length + reason.Length];
            closeCode.CopyTo(closeFrame, 0);
            reason.CopyTo(closeFrame, closeCode.Length);

            await SendFrame(OpCode.Close, closeFrame);

            client.Dispose(); // close the socket
            if (server.GetClients().Contains(this))
            {
                server.GetClients().Remove(this);
            }
        }