예제 #1
0
        public static bool Accept(Stream stream)
        {
            string received = received = stream.Receive();

            //Check if client requests handshake
            if (Regex.IsMatch(received, "^GET"))
            {
                HandshakeRequest hr           = HandshakeRequest.Parse(received);
                string           acceptString = WebSocketsUtil.ComputeAcceptString(hr.SecWebSocketKey);

                try
                {
                    stream.Send(new HandshakeResponse(acceptString).Serialize());
                    return(true);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                return(false);
            }
        }
예제 #2
0
        //Receive data from client's stream
        private void BeginReceive(IAsyncResult ar)
        {
            WebSocketsClient wsclient = (WebSocketsClient)ar.AsyncState;
            DataFrame        frame    = wsclient.EndReceive(ar);

            if (frame == null)
            {
                DisconnectClient(wsclient);
                return;
            }

            try
            {
                switch (frame.OpCode)
                {
                case OpCode.Text:
                    try
                    {
                        string payload = Encoding.UTF8.GetString(WebSocketsUtil.DecodePayload(frame));
                        OnMessage?.Invoke(this, new WebSocketsMessageEventArgs(wsclient, payload));
                    }
                    catch (Exception e)
                    {
                        OnError?.Invoke(this, new WebSocketsErrorEventArgs(e));
                    }
                    break;

                case OpCode.Binary:

                    break;

                case OpCode.Continue:

                    break;

                case OpCode.Ping:

                    break;

                case OpCode.Pong:

                    break;

                case OpCode.ConnectionClose:
                    DisconnectClient(wsclient);
                    break;

                default:

                    break;
                }
            }
            catch (Exception) { }

            wsclient.BeginReceive(BeginReceive, wsclient);
        }