public override void ChannelActive(IChannelHandlerContext contex) { IChannelGroup g = Group; if (g == null) { lock (this) { g = Group ?? (Group = new DefaultChannelGroup(contex.Executor)); } } base.ChannelActive(contex); string msg = $"欢迎 to {0} secure chat server! { Dns.GetHostName()}\n"; ReplyContent <string> reply = new ReplyContent <string>() { ConnectionId = $"{contex.Channel.Id}", Cmd = 0, Scope = 0, Message = msg }; contex.WriteAndFlushAsync(reply.ToString()); g.Add(contex.Channel); }
protected override void ChannelRead0(IChannelHandlerContext contex, string msg) { if (!RequestCommand <string> .TryGetCommand(msg, out RequestCommand <string> cmd)) { contex.WriteAndFlushAsync("无法识别的JSON指令;" + msg); return; } ReplyContent <string> reply = new ReplyContent <string>() { ConnectionId = $"{contex.Channel}", Cmd = cmd.Cmd, Scope = cmd.Scope }; //OnMessageReceived // Group.WriteAndFlushAsync(reply, new AllChannelMatcher(contex.Channel.Id)); contex.WriteAndFlushAsync(reply.ToString()); OnMessageReceived(contex); if (string.Equals("bye", msg, StringComparison.OrdinalIgnoreCase)) { contex.CloseAsync(); } }