public void StartServer(string address, int port) { host = WebSocketHostBuilder.Create() .UseWebSocketMessageHandler( async(session, message) => { await session.SendAsync(message.Message); } ) .ConfigureAppConfiguration((hostCtx, configApp) => { configApp.AddInMemoryCollection(new Dictionary <string, string> { { "serverOptions:name", "EchoWebSocketServer" }, { "serverOptions:listeners:0:ip", address }, { "serverOptions:listeners:0:port", port.ToString() } }); }) .ConfigureLogging((hostCtx, loggingBuilder) => { loggingBuilder.AddConsole(); }) .Build(); host.Start(); }
static async System.Threading.Tasks.Task Main(string[] args) { var host = WebSocketHostBuilder.Create() .UseWebSocketMessageHandler( async(session, message) => { Console.WriteLine(message.Message); await session.SendAsync(message.Message); } ) .ConfigureAppConfiguration((hostCtx, configApp) => { configApp.AddInMemoryCollection(new Dictionary <string, string> { { "serverOptions:name", "JT808.Server" }, { "serverOptions:listeners:0:ip", "Any" }, { "serverOptions:listeners:0:port", "4040" } }); }) .ConfigureLogging((hostCtx, loggingBuilder) => { loggingBuilder.AddConsole(); }) .Build(); await host.RunAsync(); }
public async Task TestStartByHost() { var hostBuilder = WebSocketHostBuilder.Create() .ConfigureLogging((hostCtx, loggingBuilder) => { loggingBuilder.AddConsole(); loggingBuilder.AddDebug(); }); using (var host = hostBuilder.Build()) { await host.StartAsync(); var websocket = new ClientWebSocket(); await websocket.ConnectAsync(new Uri($"ws://localhost:{DefaultServerPort}"), CancellationToken.None); Assert.Equal(WebSocketState.Open, websocket.State); await Task.Delay(1000 * 5); await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); Assert.Equal(WebSocketState.Closed, websocket.State); await host.StopAsync(); } }
static async Task Main(string[] args) { var host = WebSocketHostBuilder.Create() .UseWebSocketMessageHandler(async(session, message) => { if (message.Message.Equals("StartPush", StringComparison.OrdinalIgnoreCase)) { var server = session.Server as IServer; var sessionCount = server.SessionCount; var serverPush = server.ServiceProvider.GetServices <IMiddleware>().OfType <ServerPushMiddleware>() .FirstOrDefault(); serverPush.StartPush(sessionCount); session.GetDefaultLogger().LogInformation($"Start pushing to {sessionCount} clients..."); return; } // echo message back to the client await session.SendAsync(message.Message); }) .UseSession <PushSession>() .UseInProcSessionContainer() .UseMiddleware <ServerPushMiddleware>() .ConfigureLogging((hostCtx, loggingBuilder) => { // register your logging library here loggingBuilder.AddConsole(); }) .Build(); await host.RunAsync(); }
static async Task Main(string[] args) { var host = WebSocketHostBuilder.Create(args) .UseWebSocketMessageHandler(async(session, message) => { var s = session as PushSession; if (message.Message.Equals("ACK", StringComparison.OrdinalIgnoreCase)) { s.Ack(); } await Task.CompletedTask; }) .UseSession <PushSession>() .UseInProcSessionContainer() .UseMiddleware <ServerPushMiddleware>() .ConfigureLogging((hostCtx, loggingBuilder) => { // register your logging library here loggingBuilder.AddConsole(); }) .Build(); await host.RunAsync(); }
public async Task RunAsync() { var host = WebSocketHostBuilder.Create() .UseWebSocketMessageHandler( async(session, message) => { await session.SendAsync(message.Message); } ).UseCommand <StringPackageInfo, StringPackageConverter>(commandOptions => { // register commands one by one commandOptions.AddCommand <ADD>(); }) .ConfigureAppConfiguration((hostCtx, configApp) => { configApp.AddInMemoryCollection(new Dictionary <string, string> { { "serverOptions:name", "WebsockServer" }, { "serverOptions:listeners:0:ip", "Any" }, { "serverOptions:listeners:0:port", "4040" } }); }) .ConfigureLogging((hostCtx, loggingBuilder) => { loggingBuilder.AddConsole(); }) .Build(); await host.RunAsync(); }
public async static Task Main(string[] args) { var svcProvider = RegisterServices(); var stateManager = svcProvider.GetService <IStateManager>(); var mainHandler = svcProvider.GetService <IMainMessageHandler>(); var socketManager = svcProvider.GetService <ISessionHandler>(); socketManager.RegisterStateManager(stateManager); await stateManager.ChangeState("disconnected"); var host = WebSocketHostBuilder.Create() .UseWebSocketMessageHandler(mainHandler.HandleIncoming) .UseSessionHandler(socketManager.OnConnected, socketManager.OnDisconnected) .ConfigureAppConfiguration((_, conf) => { conf.AddInMemoryCollection(new Dictionary <string, string> { ["serverOptions:name"] = "MaterialFader", ["serverOptions:listeners:0:ip"] = "Any", ["serverOptions:listeners:0:port"] = "2021" }); }) .Build(); host.Run(); }
protected IHostBuilder CreateWebSocketServerBuilder(Func <IWebSocketHostBuilder, IWebSocketHostBuilder> configurator = null, IHostConfigurator hostConfigurator = null) { var builder = WebSocketHostBuilder.Create(); if (configurator != null) { builder = configurator(builder); } return(Configure(builder, hostConfigurator)); }
protected ISuperSocketHostBuilder <WebSocketPackage> CreateWebSocketSocketServerBuilder(Func <ISuperSocketHostBuilder <WebSocketPackage>, ISuperSocketHostBuilder <WebSocketPackage> > configurator = null, IHostConfigurator hostConfigurator = null) { var hostBuilder = WebSocketHostBuilder.Create() as ISuperSocketHostBuilder <WebSocketPackage>; if (configurator != null) { hostBuilder = configurator(hostBuilder); } return(Configure(hostBuilder, hostConfigurator) as ISuperSocketHostBuilder <WebSocketPackage>); }
static async Task Main(string[] args) { var host = WebSocketHostBuilder.Create() .UseWebSocketMessageHandler(async(session, message) => { // echo message back to the client await session.SendAsync(message.Message); }) .ConfigureLogging((hostCtx, loggingBuilder) => { // register your logging library here loggingBuilder.AddConsole(); }).Build(); await host.RunAsync(); }
public async Task Build() { var host = WebSocketHostBuilder.Create() .UseWebSocketMessageHandler( async(session, message) => { yzbcore.Bussiness.LogHelper.Info(JsonConvert.SerializeObject(message)); ConnectionManager.AddSocket(session); var socketId = ConnectionManager.GetId(session); var uid = JsonConvert.DeserializeObject <wsmessgae>(message.Message); ConnectionManager.AddSerial(uid.uid, socketId); yzbcore.Bussiness.LogHelper.Info(uid.uid); //await session.SendAsync(uid.uid); LogHelper.Error("收到ws连接uid" + uid + "socketId" + socketId); } ) .ConfigureAppConfiguration((hostCtx, configApp) => { configApp.AddInMemoryCollection(new Dictionary <string, string> { //{ "handshakeoptions:checkinginterval", "1800" }, { "serverOptions:name", "YZBServer" }, { "serverOptions:listeners:0:ip", "Any" }, { "serverOptions:listeners:0:port", "2347" } }); }).ConfigureServices((ctx, services) => { services.Configure <HandshakeOptions>(options => { options.CheckingInterval = 1800; options.OpenHandshakeTimeOut = 1; options.CloseHandshakeTimeOut = 1800; }); }) .ConfigureLogging((hostCtx, loggingBuilder) => { loggingBuilder.AddConsole(); }) .Build(); await host.RunAsync(); }
public Sensor(DeviceConfiguration configuration, DeviceStatusReport status) { Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); StatusReport = status ?? throw new ArgumentNullException(nameof(status)); _marsClients = new Dictionary <string, MrsClient>(); _pendingIndications = new List <IndicationType>(400); _socketHost = WebSocketHostBuilder.Create() .ConfigureAppConfiguration((context, builder) => { builder.AddInMemoryCollection(new Dictionary <string, string> { { "serverOptions:name", "MrsSensorServer" }, { "serverOptions:listeners:0:ip", $"{IP}" }, { "serverOptions:listeners:0:port", $"{Port}" } }); }) .UseWebSocketMessageHandler(WebSocketMessageHandler) .Build(); _timer = new Timer(); _timer.Elapsed += Timer_Elapsed; }
protected IHostBuilder CreateWebSocketServerBuilder() { return(Configure(WebSocketHostBuilder.Create())); }