通过配置启动SuperSocket

关键字: 配置启动, Boostrap, 配置示例, 服务形式运行, Windows服务

为什么要通过配置启动?

  • 避免硬编码
  • SuperSocket提供了很多有用的配置选项
  • 可以充分利用SuperSocket提供的工具

如何使用Bootstrap来通过配置启动SuperSocket

  • SuperSocket配置sectionSuperSocket使用.NET自带的配置技术,SuperSocket有一个专门的配置Section:
  1. <configSections>
  2. <section name="superSocket"
  3. type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine" />
  4. </configSections>
  • Server实例的配置
  1. <superSocket>
  2. <servers>
  3. <server name="TelnetServer"
  4. serverType="SuperSocket.QuickStart.TelnetServer_StartByConfig.TelnetServer, SuperSocket.QuickStart.TelnetServer_StartByConfig"
  5. ip="Any" port="2020">
  6. </server>
  7. </servers>
  8. </superSocket>

现在,我在这里解释配置的服务器节点:

  1. name: 实例名称
  2. serverType: 实例运行的AppServer类型
  3. ip: 侦听ip
  4. port: 侦听端口

我们将在下一份文档中有关于配置的更加完整的介绍。

  • 使用BootStrap启动SuperSocket
  1. static void Main(string[] args)
  2. {
  3. Console.WriteLine("Press any key to start the server!");
  4. Console.ReadKey();
  5. Console.WriteLine();
  6. var bootstrap = BootstrapFactory.CreateBootstrap();
  7. if (!bootstrap.Initialize())
  8. {
  9. Console.WriteLine("Failed to initialize!");
  10. Console.ReadKey();
  11. return;
  12. }
  13. var result = bootstrap.Start();
  14. Console.WriteLine("Start result: {0}!", result);
  15. if (result == StartResult.Failed)
  16. {
  17. Console.WriteLine("Failed to start!");
  18. Console.ReadKey();
  19. return;
  20. }
  21. Console.WriteLine("Press key 'q' to stop it!");
  22. while (Console.ReadKey().KeyChar != 'q')
  23. {
  24. Console.WriteLine();
  25. continue;
  26. }
  27. Console.WriteLine();
  28. //Stop the appServer
  29. bootstrap.Stop();
  30. Console.WriteLine("The server was stopped!");
  31. Console.ReadKey();
  32. }
  • 一些配置示例

  • Server types 节点:

  1. <superSocket>
  2. <servers>
  3. <server name="TelnetServer"
  4. serverTypeName="TelnetServer"
  5. ip="Any" port="2020">
  6. </server>
  7. </servers>
  8. <serverTypes>
  9. <add name="TelnetServer" type="SuperSocket.QuickStart.TelnetServer_StartByConfig.TelnetServer, SuperSocket.QuickStart.TelnetServer_StartByConfig"/>
  10. </serverTypes>
  11. </superSocket>
  • 多服务器实例:
  1. <superSocket>
  2. <servers>
  3. <server name="TelnetServerA"
  4. serverTypeName="TelnetServer"
  5. ip="Any" port="2020">
  6. </server>
  7. <server name="TelnetServerB"
  8. serverTypeName="TelnetServer"
  9. ip="Any" port="2021">
  10. </server>
  11. </servers>
  12. <serverTypes>
  13. <add name="TelnetServer" type="SuperSocket.QuickStart.TelnetServer_StartByConfig.TelnetServer, SuperSocket.QuickStart.TelnetServer_StartByConfig"/>
  14. </serverTypes>
  15. </superSocket>

SuperSocket.SocketService.exe, SuperSocket提供的运行容器

  • 直接使用SuperSocket.SocketService.exe

  • 务必使你的server所需要的所有程序集都和SuperSocket.SocketService.exe在同一目录

  • 将你的SuperSocket配置放置于SuperSocket.SocketService.exe.config文件之中

  • 直接运行"SuperSocket.SocketService.exe",你定义的服务器将会运行

  • 安装SuperSocket.SocketService.exe为Windows服务

通过在命令行下加参数"-i"运行SuperSocket.SocketService.exe,你可以安装它成为一个Windows服务:

  1. SuperSocket.SocketSerrvice.exe -i

这个Windows服务的名字定义在配置文件之中,你可以根据你的需要修改它:

  1. <appSettings>
  2. <add key="ServiceName" value="SuperSocketService" />
  3. </appSettings>

你也可以通过参数"-u"来卸载该服务:

  1. SuperSocket.SocketService.exe -u