protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); appServer = new CustomServer(); var SocketPort = System.Web.Configuration.WebConfigurationManager.AppSettings["SocketPort"].ToString(); int port = Convert.ToInt32(SocketPort); if (!appServer.Setup(2000)) { return; } //从配置中获取端口号 if (!appServer.Start()) { return; } //客户端连接 appServer.NewSessionConnected += AppServer_NewSessionConnected; //客户端接收消息 appServer.NewMessageReceived += AppServer_NewMessageReceived; //客户端关闭 appServer.SessionClosed += AppServer_SessionClosed; }
static void Main(string[] args) { Console.WriteLine("Press any key to start the server!"); Console.ReadKey(); Console.WriteLine(); var bootstrap = BootstrapFactory.CreateBootstrap(); if (!bootstrap.Initialize()) { Console.WriteLine("Failed to initialize!"); Console.ReadKey(); return; } var result = bootstrap.Start(); Console.WriteLine("Start result: {0}!", result); if (result == StartResult.Failed) { Console.WriteLine("Failed to start!"); Console.ReadKey(); return; } foreach (var s in bootstrap.AppServers) { if (s is TelnetServer) { TelnetServer x = s as TelnetServer; x.NewSessionConnected += new SessionHandler <TelnetSession>(telnetServer_NewSessionConnected); x.SessionClosed += new SessionHandler <TelnetSession, CloseReason>(telnetServer_SessionClosed); } else if (s is CustomServer) { CustomServer x = s as CustomServer; x.NewSessionConnected += new SessionHandler <CustomSession>(customServer_NewSessionConnected); x.SessionClosed += new SessionHandler <CustomSession, CloseReason>(customServer_SessionClosed); } } Console.WriteLine("Press key 'q' to stop it!"); while (Console.ReadKey().KeyChar != 'q') { Console.WriteLine(); continue; } Console.WriteLine(); //Stop the appServer bootstrap.Stop(); Console.WriteLine("The server was stopped!"); Console.ReadKey(); }