Пример #1
0
        public void StartServer()
        {
            var userFiter = new SocketHandshake();

            userFiter.Register(new SocketServerBLL.UserConnectFilter());
            UserListen = SocketServerFactory.SocketServerManager(
                new OfflineMsgMemory(),
                userFiter,
                heartFreq: this.HeartInterval,
                retryNumber: this.RetryNumber,
                retyInterval: this.RetryInterval
                );
            UserListen.MessageFormatting = (msg) => msg + ";";
            UserListen.NotifyComplete   += (userID, result) =>
            {
                Log(string.Format("send {0} {1}", userID, result?"success":"failed"));
            };
            UserListen.Listen(this.UserListenIp, this.UserListenPort);
            Log(string.Format("user listen address {0}:{1}", UserListenIp, UserListenPort));

            if (!String.IsNullOrEmpty(this.AdminListenIp))
            {
                var adminFiter = new SocketHandshake();
                adminFiter.Register(new SocketServerBLL.AdminConnectFilter());
                AdminListen = SocketServerFactory.SocketAdminManager(UserListen, adminFiter);
                AdminListen.Listen(this.AdminListenIp, this.AdminListenPort);
                Log(string.Format("admin notify listen address {0}:{1}", AdminListenIp, AdminListenPort));
            }
            this.IsRun = true;
        }
Пример #2
0
        static void Main(string[] args)
        {
            var userFiter = new SocketHandshake();

            userFiter.Register(new TestFilter());
            var manager = SocketServerFactory.SocketServerManager(
                new OfflineMsgMemory(),
                userFiter,
                heartFreq: 0,
                retryNumber: 3,
                retyInterval: 10000
                );

            manager.Listen("127.0.0.1", 3366);


            var adminFiter = new SocketHandshake();

            adminFiter.Register(new TestAdminFiter());
            SocketSpreader spreader = SocketServerFactory.SocketAdminManager(manager, adminFiter);

            spreader.Listen("127.0.0.1", 3365);


            while (true)
            {
                string msg = Console.ReadLine();
                if (msg.Equals("end"))
                {
                    manager.Dispose();
                    spreader.Dispose();
                    break;
                }
                else if (msg.StartsWith("send"))
                {
                    var temp = msg.Split(' ');
                    if (temp.Length < 3)
                    {
                        Console.WriteLine("参数错误");
                    }
                    else
                    {
                        manager.Notify(temp[2], temp[1]);
                    }
                }
                else
                {
                    manager.Notify(msg);
                }
            }
            Console.ReadKey();
            Main(null);
        }