private void ReceivedHanlder(SegmentToken session)
        {
            if (IsConnected == false)
            {
                string msg = encoding.GetString(session.Data.buffer, session.Data.offset, session.Data.size);
                acceptInfo = new WebsocketFrame().ParseAcceptedFrame(msg);

                if ((IsConnected = acceptInfo.IsHandShaked()))
                {
                    resetEvent.Set();
                    if (OnConnected != null)
                    {
                        OnConnected(session.sToken, IsConnected);
                    }
                }
                else
                {
                    clientProvider.Disconnect();
                }
            }
            else
            {
                WebsocketFrame packet = new WebsocketFrame();
                bool           isOk   = packet.DecodingFromBytes(session.Data, true);
                if (isOk == false)
                {
                    return;
                }

                if (packet.OpCode == 0x01)
                {
                    if (OnReceived != null)
                    {
                        OnReceived(session.sToken, encoding.GetString(packet.Payload.buffer,
                                                                      packet.Payload.offset, packet.Payload.size));
                    }

                    return;
                }
                else if (packet.OpCode == 0x08)//close
                {
                    IsConnected = false;
                    clientProvider.Disconnect();
                }
                else if (packet.OpCode == 0x09)//ping
                {
                    SendPong(session.Data);
                }
                else if (packet.OpCode == 0x0A)//pong
                {
                    SendPing();
                }

                if (OnReceivedBytes != null && packet.Payload.size > 0)
                {
                    OnReceivedBytes(new SegmentToken(session.sToken, packet.Payload));
                }
            }
        }
Esempio n. 2
0
        public SegmentOffset RspAcceptedFrame(AccessInfo access)
        {
            var accept = new AcceptInfo()
            {
                Connection           = access.Connection,
                Upgrade              = access.Upgrade,
                SecWebSocketLocation = access.Host,
                SecWebSocketOrigin   = access.Origin,
                SecWebSocketAccept   = (access.SecWebSocketKey + acceptMask).ToSha1Base64(encoding)
            };

            return(new SegmentOffset(encoding.GetBytes(accept.ToString())));
        }
Esempio n. 3
0
        public AcceptInfo ParseAcceptedFrame(string msg)
        {
            string[] msgs       = msg.Split(BaseInfo.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            var      acceptInfo = new AcceptInfo
            {
                HttpProto = msgs[0]
            };

            foreach (var item in msgs)
            {
                string[] kv = item.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
                switch (kv[0])
                {
                case "Upgrade":
                    acceptInfo.Upgrade = kv[1];
                    break;

                case "Connection":
                    acceptInfo.Connection = kv[1];
                    break;

                case "Sec-WebSocket-Accept":
                    acceptInfo.SecWebSocketAccept = kv[1];
                    break;

                case "Sec-WebSocket-Location":
                    acceptInfo.SecWebSocketLocation = kv[1];
                    break;

                case "Sec-WebSocket-Origin":
                    acceptInfo.SecWebSocketOrigin = kv[1];
                    break;
                }
            }
            return(acceptInfo);
        }