Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static WebSocketHeader Parse(NetworkStream stream)
        {
            WebSocketHeader _retVal = null;

            if (stream.CanRead)
            {
                _retVal = new WebSocketHeader();

                int _byteRcvd  = Int32.MinValue;
                int _delimiter = 23;

                List <byte> _buffer = new List <byte>();

                while (stream.DataAvailable)
                {
                    _byteRcvd = stream.ReadByte();

                    if (_buffer.Count > 1)
                    {
                        if (_delimiter.Equals(_byteRcvd + _buffer[(_buffer.Count - 1)]))
                        {
                            string   _line   = UTF8Encoding.ASCII.GetString(_buffer.ToArray(), 0, (_buffer.Count - 1));
                            string[] _tokens = _line.Trim().Split(new char[] { ':' }, 2);

                            if (_tokens.Length > 1)
                            {
                                string _key = _tokens.GetValueOrDefault(0, string.Empty).Trim();
                                string _val = _tokens.GetValueOrDefault(1, string.Empty).Trim();

                                if (!string.IsNullOrEmpty(_key) &&
                                    !string.IsNullOrEmpty(_val))
                                {
                                    _retVal.SetValueWithKey(_key, _val);
                                }
                            }
                            else
                            {
                                if (_line.ContainsAny(new string[] { "HTTP", "GET" }))
                                {
                                    _retVal.Version = _line;
                                }
                            }

                            _buffer.Clear();

                            continue;
                        }
                    }

                    _buffer.Add((byte)_byteRcvd);
                }
            }

            return(_retVal);
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        protected override WebSocketHeader GetResponseHeader()
        {
            WebSocketHeader _response = new WebSocketHeader();

            _response.Version         = "HTTP/1.1 101 Switching Protocols";
            _response.Upgrade         = "websocket";
            _response.Connection      = "Upgrade";
            _response.WebSocketAccept = GetComputedBase64String();

            return(_response);
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="socket"></param>
        /// <returns></returns>
        public static WebSocketHeader Parse(Socket socket)
        {
            WebSocketHeader _retVal = null;

            using (NetworkStream _stream = new NetworkStream(socket))
            {
                _retVal = Parse(_stream);
            }

            return(_retVal);
        }
        /// <summary>
        ///
        /// </summary>
        public void Handshake()
        {
            WebSocketHeader _response = GetResponseHeader();

            if (_response != null)
            {
                using (NetworkStream _stream = new NetworkStream(CurrentConnection))
                {
                    if (_stream.CanWrite)
                    {
                        using (StreamWriter _writer = new StreamWriter(_stream))
                        {
                            _writer.Write(_response);
                        }
                    }
                }
            }
        }