/// <summary> /// 向指定会话对象推送消息 /// </summary> /// <param name="session">当前会话</param> /// <param name="command">命令</param> /// <param name="msg">数据包</param> public static void Send(this CustomSession session, SocketCommand command, string msg) { if (session != null && session.Connected) { byte[] data = GetMsgBytes(msg, command); session.Send(data, 0, data.Length); } }
/// <summary> /// 推送消息 /// </summary> /// <param name="session"></param> /// <param name="action"></param> /// <param name="text"></param> public static void Push(this CustomSession session, ushort action, string text) { var response = BitConverter.GetBytes(action).Reverse().ToList(); var arr = System.Text.Encoding.UTF8.GetBytes(text); response.AddRange(BitConverter.GetBytes((ushort)arr.Length).Reverse().ToArray()); response.AddRange(arr); session.Send(response.ToArray(), 0, response.Count); }
/// <summary> /// 执行命令前调用 /// </summary> /// <param name="commandContext"></param> public override void OnCommandExecuting(CommandExecutingContext commandContext) { CustomSession session = (CustomSession)commandContext.Session; if (session != null) { session.Items["StartTime"] = DateTime.Now; if (commandContext.RequestInfo.Key.Equals("-1")) { CustomCommand comm = (CustomCommand)Enum.Parse(typeof(CustomCommand), commandContext.RequestInfo.Key); string json = "未知命令!".GetTransmitPackets(comm); session.Send(comm, json); //取消执行当前命令 commandContext.Cancel = true; } } }