Пример #1
0
        public void CreateHandler(IEnumerable <byte> data)
        {
            byte[] bytes   = data.ToArray();
            var    request = RequestParser.Parse(bytes, owner.Schema);

            if (request == null)
            {
                return;
            }
            handler = WebSocketHandlerFactory.BuildHandler(request, SockectConnection.OnMessage, SockectConnection.OnClose, SockectConnection.OnBinary);
            if (handler == null)
            {
                return;
            }
            var subProtocol = SubProtocolNegotiator.Negotiate(owner.SupportedSubProtocols, request.SubProtocols);

            ConnectionInfo = WebSocketConnectionInfo.Create(request, SockectConnection.Socket.RemoteIpAddress, SockectConnection.Socket.RemotePort, subProtocol);

            if (!string.IsNullOrEmpty(ConnectionInfo.Path))
            {
                SockectConnection.Session.Add("WebSocket_Path", ConnectionInfo.Path);
            }

            foreach (string item in ConnectionInfo.Cookies.Keys)
            {
                SockectConnection.Session.Add(item, ConnectionInfo.Cookies[item]);
            }

            var handshake = handler.CreateHandshake(subProtocol);

            SockectConnection.RawSend(handshake);
        }
Пример #2
0
        private void Write(string content)
        {
            if (content == null)
            {
                content = string.Empty;
            }

            var contentBytes = Encoding.UTF8.GetBytes(content);
            var headerByes   = this.GetHeaderBytes(contentBytes.Length);

            var all = new byte[contentBytes.Length + headerByes.Length];

            Buffer.BlockCopy(headerByes, 0, all, 0, headerByes.Length);
            Buffer.BlockCopy(contentBytes, 0, all, headerByes.Length * sizeof(byte), contentBytes.Length);

            SockectConnection.Send(all);
        }