public override Dynamic.Net.Base.INetCommand Execute(Dynamic.Net.Base.INetSession session)
        {
            WebSocketSessionBase ws = session as WebSocketSessionBase;

            if (ws == null)
            {
                return(null);
            }

            MessageReceivedEventArgs args = new MessageReceivedEventArgs()
            {
                ContentType = MessageContentType.Binary,
                Data        = InnerData,
                Session     = session as WebSocketSessionBase,
                IsAync      = false
            };

            ws.OnMessageReceived(args);

            INetCommand responseCommand = null;

            if (!args.IsAync && args.ResponseData != null && args.ResponseData.Length > 0)
            {
                responseCommand = WebSocketCommandFactory.CreateCommand(args.ResponseData);
            }

            return(responseCommand);
        }
        public override Dynamic.Net.Base.INetCommand Execute(Dynamic.Net.Base.INetSession session)
        {
            if (InnerData != null && InnerData.Length > 0)
            {
                Content = session.Encoding.GetString(InnerData);
            }

            WebSocketSessionBase ws = session as WebSocketSessionBase;

            if (ws == null)
            {
                return(null);
            }

            MessageReceivedEventArgs args = new MessageReceivedEventArgs()
            {
                Content     = this.Content,
                ContentType = MessageContentType.Text,
                Data        = InnerData,
                Session     = ws,
                IsAync      = false
            };

            ws.OnMessageReceived(args);

            INetCommand responseCommand = null;

            if (!args.IsAync && !String.IsNullOrEmpty(args.ResponseContent))
            {
                responseCommand = WebSocketCommandFactory.CreateCommand(args.ResponseContent);
            }

            return(responseCommand);
        }
        protected virtual void HandshakeCompleted(object sender, EventArgs e)
        {
            WebSocketSessionBase ws = sender as WebSocketSessionBase;

            if (ws != null)
            {
                if (ClientConnected != null)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback((o) =>
                    {
                        CommandSession cs = null;
                        lock (lockerObj)
                        {
                            if (SessionList.ContainsKey(ws.SessionID))
                            {
                                cs = SessionList[ws.SessionID];
                            }
                        }
                        if (cs != null)
                        {
                            ClientConnected(this, cs);
                        }
                    }), null);
                }
            }
        }
        //public virtual void AttachSession(WebSocketSessionBase session)
        //{
        //    application.AttachSession(session);
        //    SessionStarted(session, EventArgs.Empty);
        //}

        //public virtual void DettachSession(WebSocketSessionBase session)
        //{
        //    lock (lockerObj)
        //    {
        //        SessionList.Remove(session.SessionID);
        //        application.DettachSession(session);
        //    }

        //}

        public virtual void AttachSession(CommandSession commandSession)
        {
            if (commandSession == null)
            {
                logger.Warn("AttachSession 传入Session为空");
                return;
            }
            application.AttachSession(commandSession.Session);
            WebSocketSessionBase ws = commandSession.Session as WebSocketSessionBase;

            if (ws != null)
            {
                ws.SessionIDChanged += new EventHandler <SessionIDChangedEventArgs>(ws_SessionIDChanged);

                lock (lockerObj)
                {
                    SessionList.Add(ws.SessionID, commandSession);
                }
            }
        }
Ejemplo n.º 5
0
        public Byte[] GetResponseData(INetSession session)
        {
            StringBuilder sbCommand = new StringBuilder();

            sbCommand.Append(GetHeader(WebSocketHeader.HttpVersion)).Append(" ");
            sbCommand.Append(GetHeader(WebSocketHeader.HttpCode)).Append(" ");
            sbCommand.Append(GetHeader(WebSocketHeader.SwitchingProtocols));
            sbCommand.Append(headerWrap);

            sbCommand.Append(WebSocketHeaderConverter.ConverterToString(WebSocketHeader.Upgrade));
            sbCommand.Append(": ");
            sbCommand.Append(GetHeader(WebSocketHeader.Upgrade));
            sbCommand.Append(headerWrap);

            sbCommand.Append(WebSocketHeaderConverter.ConverterToString(WebSocketHeader.Connection));
            sbCommand.Append(": ");
            sbCommand.Append(GetHeader(WebSocketHeader.Connection));
            sbCommand.Append(headerWrap);

            sbCommand.Append(WebSocketHeaderConverter.ConverterToString(WebSocketHeader.SecWebSocketAccept));
            sbCommand.Append(": ");
            sbCommand.Append(GetHeader(WebSocketHeader.SecWebSocketAccept));
            sbCommand.Append(headerWrap);
            WebSocketSessionBase s = session as WebSocketSessionBase;

            if (s != null && !String.IsNullOrEmpty(s.SubProtocol))
            {
                sbCommand.Append(WebSocketHeaderConverter.ConverterToString(WebSocketHeader.SecWebSocketProtocol));
                sbCommand.Append(": ");
                sbCommand.Append(s.SubProtocol);
                sbCommand.Append(headerWrap);
            }
            sbCommand.Append(headerWrap);

            string response = sbCommand.ToString();

            Debug.WriteLine(response);
            logger.Info("CommandName:{0}\r\nResponseContent:{1}", Name, response);

            return(Encoding.GetBytes(response));
        }
        protected virtual void SessionStarted(object sender, EventArgs e)
        {
            WebSocketSessionBase ws = sender as WebSocketSessionBase;

            ws.SessionIDChanged += new EventHandler <SessionIDChangedEventArgs>(ws_SessionIDChanged);

            CommandSession session = new CommandSession()
            {
                Session = ws
            };

            if (session.CommandParser == null)
            {
                session.CommandParser = DefaultCommandParser;
            }
            if (ws != null)
            {
                lock (lockerObj)
                {
                    SessionList.Add(ws.SessionID, session);
                }
            }
        }
        protected virtual void SessionClosed(object sender, EventArgs e)
        {
            WebSocketSessionBase ws     = sender as WebSocketSessionBase;
            CommandSession       client = null;

            lock (lockerObj)
            {
                if (SessionList.ContainsKey(ws.SessionID))
                {
                    client = SessionList[ws.SessionID];
                }
            }

            if (client == null)
            {
                return;
            }

            if (client != null)
            {
                if (ClientClosed != null)
                {
                    ClientClosed(this, client);
                }
            }
            if (ws != null)
            {
                lock (lockerObj)
                {
                    try
                    {
                        SessionList.Remove(ws.SessionID);
                    }
                    catch { }
                }
            }
        }