public static bool Start() { try { var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, LogServiceConfigFileName); IBootstrap = BootstrapFactory.CreateBootstrapFromConfigFile(configFilePath); if (!IBootstrap.Initialize()) { Console.WriteLine("日志服务初始化失败"); Logger.Error("日志服务启动失败!"); return(false); } var result = IBootstrap.Start(); if (result == StartResult.Failed) { Console.WriteLine("日志服务启动失败!"); Logger.Error("日志服务启动失败!"); return(false); } return(true); } catch (Exception ex) { Logger.Error($"启动日志Socket服务失败:{ex}"); return(false); } }
public void Setup() { m_Bootstrap = BootstrapFactory.CreateBootstrapFromConfigFile("SuperSocket.config"); Assert.IsTrue(m_Bootstrap.Initialize()); m_Port = ((IAppServer)m_Bootstrap.AppServers.FirstOrDefault()).Config.Port; Assert.AreEqual(StartResult.Success, m_Bootstrap.Start()); }
private void button_StartListen_Click(object sender, EventArgs e) { try { //方法一、采用当前应用程序中的【App.config】文件。 //var bootstrap = BootstrapFactory.CreateBootstrap(); //=>方法二、采用自定义独立【SuperSocket.config】配置文件 var bootstrap = BootstrapFactory.CreateBootstrapFromConfigFile("SuperSocket.config"); if (!bootstrap.Initialize()) { ShowMessage("Failed to initialize!"); return; } StartResult startResult = bootstrap.Start(); if (startResult == StartResult.Success) { this.ShowMessage("服务启动成功!"); tcpServerEngine = bootstrap.AppServers.Cast <MyAppServer>().FirstOrDefault(); if (tcpServerEngine != null) { tcpServerEngine.NewSessionConnected += tcpServerEngine_NewSessionConnected; /* 同时你要移除请求处理方法的注册,因为它和命令不能同时被支持: * http://docs.supersocket.net/v1-6/zh-CN/A-Telnet-Example * 如果你的服务器端包含有很多复杂的业务逻辑,这样的switch/case代码将会很长而且非常难看,并且没有遵循OOD的原则。 * 在这种情况下,SuperSocket提供了一个让你在多个独立的类中处理各自不同的请求的命令框架。 */ //tcpServerEngine.NewRequestReceived += tcpServerEngine_NewRequestReceived; ECHO.ECHOMessageRecevied += ECHO_ECHOMessageRecevied; tcpServerEngine.SessionClosed += tcpServerEngine_SessionClosed; this.ShowListenStatus(); } else { this.ShowMessage("请检查配置文件中是否又可用的服务信息!"); } } else { this.ShowMessage("服务启动失败!"); } } catch (Exception ex) { ShowMessage(ex.Message); } }
private void StartWebSocketServer() { var appFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var configPath = Path.Combine(Path.Combine(appFolder, "config"), "supersocket.config"); _bootstrapFactory = BootstrapFactory.CreateBootstrapFromConfigFile(configPath); if (!_bootstrapFactory.Initialize()) { throw new Exception("Failed to initialize"); } (_bootstrapFactory.AppServers.First() as IotWebSocketServer).SetConnectionRegistry(_connectionRegistry, () => _serviceProvider.GetService <CommandExecutor>()); var result = _bootstrapFactory.Start(); if (result == StartResult.Failed) { throw new Exception("Failed to start"); } }
private void button_StartListen_Click(object sender, EventArgs e) { try { //方法一、采用当前应用程序中的【App.config】文件。 //var bootstrap = BootstrapFactory.CreateBootstrap(); //=>方法二、采用自定义独立【SuperSocket.config】配置文件 var bootstrap = BootstrapFactory.CreateBootstrapFromConfigFile("SuperSocket.config"); if (!bootstrap.Initialize()) { ShowMessage("Failed to initialize!"); return; } StartResult startResult = bootstrap.Start(); if (startResult == StartResult.Success) { this.ShowMessage("服务启动成功!"); tcpServerEngine = bootstrap.AppServers.Cast <MyAppServer>().FirstOrDefault(); if (tcpServerEngine != null) { tcpServerEngine.NewSessionConnected += tcpServerEngine_NewSessionConnected; tcpServerEngine.NewRequestReceived += tcpServerEngine_NewRequestReceived; tcpServerEngine.SessionClosed += tcpServerEngine_SessionClosed; this.ShowListenStatus(); } else { this.ShowMessage("请检查配置文件中是否有可用的服务信息!"); } } else { this.ShowMessage("服务启动失败!"); } } catch (Exception ex) { ShowMessage(ex.Message); } }
static void Main(string[] args) { IBootstrap bootstrap = BootstrapFactory.CreateBootstrapFromConfigFile("SuperSocket.config"); 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) //{ // Console.WriteLine($"{s.Config.Ip}:{s.Config.Port}"); //} //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!"); }
/// <summary> /// 启动AppServer /// </summary> private ErrorMessage InitAppServer() { ShowMessage(ColorHelper.MsgGray, "正在加载通讯服务..."); Thread.Sleep(interval); try { //=>方法一、采用当前应用程序中的【App.config】文件BootstrapFactory.CreateBootstrap()。方法二、采用自定义独立【SuperSocket.config】配置文件 var bootstrap = BootstrapFactory.CreateBootstrapFromConfigFile("SuperSocket.config"); if (!bootstrap.Initialize()) { return new ErrorMessage() { ErrorCode = "Init Error", ErrorInfo = "Failed to initialize!" } } ; StartResult startResult = bootstrap.Start(); if (startResult == StartResult.Success) { this.ShowMessage(ColorHelper.MsgGreen, "服务启动成功,等待设备连接 =>"); } else { return new ErrorMessage() { ErrorCode = "StartError", ErrorInfo = "服务启动失败!" } }; } catch (Exception ex) { return(new ErrorMessage() { ErrorCode = "StartError", ErrorInfo = ex.Message }); } return(null); }
static void Main(string[] args) { Console.WriteLine("Start the server!"); var bootstrap = BootstrapFactory.CreateBootstrapFromConfigFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Config\Basic.config")); 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; } Console.WriteLine("Press key 'q' to stop it!"); while (Console.ReadKey().KeyChar != 'q') { Console.WriteLine(); continue; } Console.WriteLine(); //Stop the appServer bootstrap.Stop(); Console.ReadKey(); }
private void InitAppServer() { try { Indicator.Text = "初始化MES服务器..."; //方法一、采用当前应用程序中的【App.config】文件。 //var bootstrap = BootstrapFactory.CreateBootstrap(); //=>方法二、采用自定义独立【SuperSocket.config】配置文件 var bootstrap = BootstrapFactory.CreateBootstrapFromConfigFile("SuperSocket.config"); #region [=>自定义服务配置] //IServerConfig serverConfig = new ServerConfig //{ // Name = "MesServer",// "AgileServer",//服务器实例的名称 // ServerType = "EsayCare.MES.MesServer, EsayCare.MES", // Ip = "Any",//Any - 所有的IPv4地址 IPv6Any - 所有的IPv6地址 // Mode = SocketMode.Tcp,//服务器运行的模式, Tcp (默认) 或者 Udp // Port = int.Parse("6543"),//服务器监听的端口 // SendingQueueSize = 5000,//发送队列最大长度, 默认值为5 // MaxConnectionNumber = 5000,//可允许连接的最大连接数 // LogCommand = false,//是否记录命令执行的记录 // LogBasicSessionActivity = false,//是否记录session的基本活动,如连接和断开 // LogAllSocketException = false,//是否记录所有Socket异常和错误 // //Security = "tls",//Empty, Tls, Ssl3. Socket服务器所采用的传输层加密协议 // MaxRequestLength = 5000,//最大允许的请求长度,默认值为1024 // TextEncoding = "UTF-8",//文本的默认编码,默认值是 ASCII,(###改成UTF-8,否则的话中文会出现乱码) // KeepAliveTime = 60,//网络连接正常情况下的keep alive数据的发送间隔, 默认值为 600, 单位为秒 // KeepAliveInterval = 60,//Keep alive失败之后, keep alive探测包的发送间隔,默认值为 60, 单位为秒 // ClearIdleSession = true, // 是否定时清空空闲会话,默认值是 false;(###如果开启定时60秒钟情况闲置的连接,为了保证客户端正常不掉线连接到服务器,故我们需要设置10秒的心跳数据包检查。也就是说清除闲置的时间必须大于心跳数据包的间隔时间,否则就会出现服务端主动踢掉闲置的TCP客户端连接。) // ClearIdleSessionInterval = 60,//: 清空空闲会话的时间间隔, 默认值是120, 单位为秒; // SyncSend = true,//:是否启用同步发送模式, 默认值: false; //}; //var rootConfig = new RootConfig() //{ // MaxWorkingThreads = 5000,//线程池最大工作线程数量 // MinWorkingThreads = 10,// 线程池最小工作线程数量; // MaxCompletionPortThreads = 5000,//线程池最大完成端口线程数量; // MinCompletionPortThreads = 10,// 线程池最小完成端口线程数量; // DisablePerformanceDataCollector = true,// 是否禁用性能数据采集; // PerformanceDataCollectInterval = 60,// 性能数据采集频率 (单位为秒, 默认值: 60); // LogFactory = "ConsoleLogFactory",//默认logFactory的名字 // Isolation = IsolationMode.AppDomain// 服务器实例隔离级别 //}; #endregion if (!bootstrap.Initialize()) { ShowMessage(ColorHelper.MsgRed, "Failed to initialize!"); return; } StartResult startResult = bootstrap.Start(); if (startResult == StartResult.Success) { Indicator.Visible = false; this.ShowMessage(ColorHelper.MsgGreen, "服务启动成功,等待设备连接 =>"); mesServer = bootstrap.AppServers.Cast <MesServer>().FirstOrDefault(); if (mesServer != null) { mesServer.NewSessionConnected += MesServer_NewSessionConnected; mesServer.NewRequestReceived += MesServer_NewRequestReceived; mesServer.SessionClosed += MesServer_SessionClosed; } else { this.ShowMessage(ColorHelper.MsgRed, "请检查配置文件中是否有可用的服务信息!"); } } else { this.ShowMessage(ColorHelper.MsgRed, "服务启动失败!"); } } catch (Exception ex) { ShowMessage(ex.Message); } }
private IBootstrap GetBootstrap() { var configFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Asserts", "Config", ConfigFile); return(BootstrapFactory.CreateBootstrapFromConfigFile(configFile)); }