public void TestECHO() { IServerConfig config = new ServerConfig { Name = "My Custom Server", Ip = "Any", Port = 100, Mode = SocketMode.Async, MaxConnectionNumber = 1 }; var rootConfig = new RootConfig { LoggingMode = LoggingMode.Console }; YourServer server = new YourServer(); server.Setup(rootConfig, config, SocketServerFactory.Instance); server.Start(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), config.Port); using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); Stream socketStream = new NetworkStream(socket); using (StreamReader reader = new StreamReader(socketStream, Encoding.Default, true)) using (StreamWriter writer = new StreamWriter(socketStream, Encoding.Default, 1024 * 8)) { //ignore welcome message reader.ReadLine(); string command = "CMD:ECHO "; string[] parameters = new string[] { "Kerry", "Jiang", "China", "Shanghai" }; string parameter = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Join(" ", parameters))); writer.WriteLine(command + parameter); writer.Flush(); foreach (var p in parameters) { string param = reader.ReadLine(); Console.WriteLine(param); Assert.AreEqual(p, param); } } } server.Stop(); }
protected void Setup(WebSocketServer websocketServer, Action<ServerConfig> configurator) { var rootConfig = new RootConfig { DisablePerformanceDataCollector = true }; websocketServer.NewSessionConnected += new SessionHandler<WebSocketSession>(m_WebSocketServer_NewSessionConnected); websocketServer.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(m_WebSocketServer_SessionClosed); var config = new ServerConfig(); configurator(config); var ret = websocketServer.Setup(rootConfig, config, null, null, new ConsoleLogFactory(), null, null); Assert.IsTrue(ret); m_WebSocketServer = websocketServer; }
public virtual void Setup() { var rootConfig = new RootConfig { DisablePerformanceDataCollector = true }; m_WebSocketServer = new WebSocketServer(new BasicSubProtocol("Basic", new List<Assembly> { this.GetType().Assembly })); m_WebSocketServer.NewDataReceived += new SessionEventHandler<WebSocketSession, byte[]>(m_WebSocketServer_NewDataReceived); m_WebSocketServer.Setup(rootConfig, new ServerConfig { Port = 2012, Ip = "Any", MaxConnectionNumber = 100, Mode = SocketMode.Tcp, Name = "SuperWebSocket Server" }); m_Bootstrap = new DefaultBootstrap(rootConfig, new IWorkItem[] { m_WebSocketServer }, new ConsoleLogFactory()); }
public override void Setup() { var rootConfig = new RootConfig { DisablePerformanceDataCollector = true }; m_Encoding = new UTF8Encoding(); m_WebSocketServer = new WebSocketServer(new BasicSubProtocol("Basic", new List<Assembly>{ this.GetType().Assembly } )); m_WebSocketServer.Setup(rootConfig, new ServerConfig { Port = 2012, Ip = "Any", MaxConnectionNumber = 100, Mode = SocketMode.Tcp, Name = "SuperWebSocket Server" }); m_Bootstrap = new DefaultBootstrap(rootConfig, new IWorkItem[] { m_WebSocketServer }, new ConsoleLogFactory()); }
public virtual void Setup() { var rootConfig = new RootConfig { DisablePerformanceDataCollector = true }; m_WebSocketServer = new WebSocketServer(); m_WebSocketServer.NewMessageReceived += new SessionEventHandler<WebSocketSession, string>(m_WebSocketServer_NewMessageReceived); m_WebSocketServer.NewSessionConnected += m_WebSocketServer_NewSessionConnected; m_WebSocketServer.SessionClosed += m_WebSocketServer_SessionClosed; m_WebSocketServer.Setup(rootConfig, new ServerConfig { Port = 2012, Ip = "Any", MaxConnectionNumber = 100, Mode = SocketMode.Tcp, Name = "SuperWebSocket Server" }); m_Bootstrap = new DefaultBootstrap(rootConfig, new IWorkItem[] { m_WebSocketServer }, new ConsoleLogFactory()); }
protected override void OnStart(string[] args) { string value = System.Configuration.ConfigurationManager.AppSettings["Port"]; int port = SysUtils.ToInt(value); if (port <= 0) port = 2012; m_WebSocketServer = new WebSocketServer(); var rootConfig = new RootConfig { DisablePerformanceDataCollector = true }; m_WebSocketServer.NewDataReceived += m_WebSocketServer_NewDataReceived; m_WebSocketServer.NewMessageReceived += m_WebSocketServer_NewMessageReceived; m_WebSocketServer.NewSessionConnected += m_WebSocketServer_NewSessionConnected; m_WebSocketServer.SessionClosed += m_WebSocketServer_SessionClosed; var config = new ServerConfig(); config.Port = port; config.Ip = "Any"; config.MaxConnectionNumber = 100; config.MaxRequestLength = 100000; config.Name = "WebSocket Proxy Server"; var ret = m_WebSocketServer.Setup(rootConfig, config, null, null, new ConsoleLogFactory(), null, null); m_WebSocketServer.Start(); _container = ComponentManager.GetInstance(); IHandler[] handlers = _container.Kernel.GetHandlers(typeof(IDeviceService)); devicelist = new List<IDeviceService>(); IDeviceService deviceservice; foreach (IHandler handler in handlers) { deviceservice = (IDeviceService)_container[handler.ComponentModel.Name]; deviceservice.webSocketServer = m_WebSocketServer; devicelist.Add(deviceservice); } _logger.Info(string.Format("启动成功,port={0}", port)); }
void StartSuperWebSocketByProgramming() { var rootConfig = new RootConfig(); var socketServer = new WebSocketServer(); socketServer.NewMessageReceived += new SessionHandler<WebSocketSession, string>(socketServer_NewMessageReceived); socketServer.NewSessionConnected += socketServer_NewSessionConnected; socketServer.SessionClosed += socketServer_SessionClosed; socketServer.Setup(rootConfig, new ServerConfig { Name = "SuperWebSocket", Ip = "Any", Port = 2011, Mode = SocketMode.Tcp }); var secureSocketServer = new WebSocketServer(); secureSocketServer.NewSessionConnected += secureSocketServer_NewSessionConnected; secureSocketServer.SessionClosed += secureSocketServer_SessionClosed; secureSocketServer.Setup(rootConfig, new ServerConfig { Name = "SecureSuperWebSocket", Ip = "Any", Port = 2012, Mode = SocketMode.Tcp, Security = "tls", Certificate = new SuperSocket.SocketBase.Config.CertificateConfig { FilePath = Server.MapPath("~/localhost.pfx"), Password = "******" } }); m_Bootstrap = new DefaultBootstrap(new RootConfig(), new IWorkItem[] { socketServer, secureSocketServer }); Application["WebSocketPort"] = socketServer.Config.Port; Application["SecureWebSocketPort"] = secureSocketServer.Config.Port; m_Bootstrap.Start(); }