Ejemplo n.º 1
0
        private WebSocketHandShakeResponse ProcessHandShakeRequest(Request request)
        {
            int id = this.GetHashCode() % 100;

            webSocketApplication = new WebSocketChat(socket, id);
            return(webSocketApplication.Process(request));
        }
        /// <summary>
        /// 新建一个CommandServer
        /// </summary>
        /// <param name="ipAddress">监听的IP地址,如果传入空对应于IPAddress.Any</param>
        /// <param name="port">监听端口</param>
        /// <param name="appName">应用名称,表示此服务的名称,对功能无关紧要</param>
        /// <param name="serverName">服务名称,表示服务使用的内部TCP监听服务的名称,对功能无关紧要</param>
        /// <param name="useProtocol">所使用的协议</param>
        /// <param name="sessionFactory">创建内部Session的工厂, 注意:工厂所创建的Session必须是WebSocketSession的子类</param>
        public CommandServer(string ipAddress, int port, string appName, string serverName, string useProtocol, ISocketSessionFactory sessionFactory)
        {
            logger = LoggerManager.GetLogger("CommandServer." + (appName == "" ? "UnName" : appName));
            DefaultCommandParser = new WSBinaryCommandType();
            this.SessionFactory  = sessionFactory;

            IP             = ipAddress;
            Port           = port;
            RequestTimeout = TimeSpan.FromMinutes(2);
            SessionList    = new Dictionary <string, CommandSession>();
            Protocol       = useProtocol;
            IsAync         = true;
            CommandList    = new List <ICommand>();

            CommandList.Add(new SetCommandParserRequest());

            CommandParser = new List <ICommandParser>()
            {
                new WSCommandType(),
                new WSBinaryCommandType()
            };

            CommandAliveTime = TimeSpan.FromMinutes(2);

            IPAddress address = IPAddress.Any;

            if (!String.IsNullOrEmpty(ipAddress))
            {
                address = IPAddress.Parse(IP);
            }

            application = new WebSocketApplication();
            application.Setup(appName);
            AsyncTcpServer server = new AsyncTcpServer();

            ServerConfig = new SocketServerConfig()
            {
                Address             = address,
                AddressFamily       = System.Net.Sockets.AddressFamily.InterNetwork,
                MaxConnectionNumber = 1000,
                Name           = serverName,
                Port           = Port,
                SessionTimeout = (long)TimeSpan.FromMinutes(5).TotalMilliseconds,
                TimeoutType    = Dynamic.Net.Session.SessionTimeoutType.Unknown, //不过期
                ServerType     = NetServerType.ASyncTcp
            };
            server.Setup(ServerConfig, application, new WebSocketProtocol(), sessionFactory);


            application.AddServer(server);


            application.SwitchingProtocol += new EventHandler <SwitchingProtocolEventArgs>(SwitchingProtocol);

            application.SessionStarted += new EventHandler(SessionStarted);

            application.SessionClosed      += new EventHandler(SessionClosed);
            application.HandshakeCompleted += new EventHandler(HandshakeCompleted);

            application.MessageReceived += new EventHandler <MessageReceivedEventArgs>(MessageReceived);
        }