public override void ChannelRead0(WebSocketClientImpl channel, byte[] msg) { string message = Encoding.UTF8.GetString(msg); IEvent @event = message.JsonToObject <Event>(); channel.HandleEvent(@event); Console.WriteLine(message); }
public override void ChannelStart(WebSocketClientImpl channel) { Console.WriteLine("正在连接服务器"); }
public override void ExceptionCaught(WebSocketClientImpl channel, Exception ex) { Console.WriteLine("Exception: " + ex.Message); }
public override void ChannelActive(WebSocketClientImpl channel) { Console.WriteLine("客户端已连接"); }
public override void ChannelInactive(WebSocketClientImpl channel) { Console.WriteLine("客户端已断开连接"); Task.Factory.StartNew(channel.StartAsync); }
public static async Task Main() { ConsoleHelper.AppName = "Client"; Version version = Assembly.Load("Demo.Client").GetName().Version; if (version != null) { Console.Title = $"Demo.Client [版本号:{version}]"; } if (TryRegisterService()) { try { var config = new ClientConfig { Url = "ws://192.168.0.101:8801/websocket", ReconnectionInterval = 1000, ReconnectionNumber = 0 }; var eventBus = ApplicationService.GetService <IEventBus>(); IWebSocketClient client = new WebSocketClientImpl(config, eventBus); client.OnException += ConsoleHelper.WriteError; client.OnConnectionSuccess += async() => { await client.SendCommandAsync(new SendMessageCommand { Message = "Hello World!" }); }; client.OnSubMessage += (message, subTitle) => ConsoleHelper.WriteLine(message, subTitle); client.OnConnectionFail += async() => { await client.ReconnectionAsync(); }; client.OnClose += async() => { await client.ReconnectionAsync(); }; await client.RunAsync(); ConsoleHelper.WriteLine("输入Stop停止服务"); var inputKey = string.Empty; while (!string.Equals(inputKey, "Stop", StringComparison.Ordinal)) { inputKey = Console.ReadLine(); if (string.Equals(inputKey, "Stop", StringComparison.Ordinal)) { break; } await client.SendCommandAsync(new SendMessageCommand { Message = inputKey }); } await client.StopAsync(); await Task.Delay(5000); } catch (Exception exception) { ConsoleHelper.WriteLine("发生致命错误", "错误", ConsoleColor.Red); ConsoleHelper.WriteError(exception); } } else { ConsoleHelper.WriteLine("注册服务失败", "失败", ConsoleColor.Red); } }
public abstract void ExceptionCaught(WebSocketClientImpl channel, Exception ex);
public abstract void ChannelRead0(WebSocketClientImpl channel, byte[] msg);
public abstract void ChannelInactive(WebSocketClientImpl channel);
public abstract void ChannelStart(WebSocketClientImpl channel);