// 广播 public static void broadcast(netEventEnum type, string message) { foreach (var i in clients) { i.Send(type, message); } }
/// <summary> /// 事件退订 /// </summary> public static void DelEvent(Operation handler, netEventEnum e) { //Debug.Log("事件退订 "+(int)e); if ((int)e > operation.Length || operation[(int)e] == null) { return; } operation[(int)e].Remove(handler); }
/// <summary> /// 事件派发 /// </summary> public static void SendEvent(netEventEnum eventId, params object[] obj) { // Debug.Log("事件派发 "+(int)eventId); if (operation == null || operation[(int)eventId] == null) { return; } for (int i = 0; i < operation[(int)eventId].Count; i++) { operation[(int)eventId][i](obj); } }
/// <summary> /// 发送方法 /// </summary> /// <param name="type">消息类型</param> /// <param name="message">消息内容</param> public void Send(netEventEnum type, string message) { //消息构成 2byte总长,2byte消息类型,之后的都是消息内容 // 消息类型bytes byte[] typeBytes = new byte[2]; typeBytes[0] = (byte)(((int)type) % 256); typeBytes[1] = (byte)(((int)type) / 256); // 打印消息类 string strr = BitConverter.ToInt16(typeBytes, 0).ToString(); //UILog.log.Add("发送消息类型字符串:" + strr); //UILog.log.Add("发送消息类型:" + type.ToString()); // 消息类型字符串 string str = Encoding.UTF8.GetString(typeBytes); // 写入消息队列 lock (sendMgrQueue) { sendMgrQueue.Enqueue(str + message); } // 发送消息拼装(加入长度) if (client.Connected) { if (sendMgrQueue.Count < 0) { return; // 待发送消息队列中没有消息跳过 } string msg = sendMgrQueue.Peek(); // 取出待发送消息 // 消息长度 byte[] sendbytes = Encoding.UTF8.GetBytes(msg); Int16 mgrLen = (Int16)sendbytes.Length; byte[] lenBytes = new byte[2]; lenBytes[0] = (byte)(sendbytes.Length % 256); lenBytes[1] = (byte)(sendbytes.Length / 256); byte[] sendBytes = lenBytes.Concat(sendbytes).ToArray(); // 发送bytes //UILog.log.Add("发送:"+Encoding.UTF8.GetString(sendbytes)); client.BeginSend(sendBytes, 0, sendBytes.Length, 0, BeginSendCallback, client); } else { //UILog.log.Add("与服务器断开"); Close(); } }
/// <summary> /// 发送方法 /// </summary> /// <param name="type">消息类型</param> /// <param name="message">消息内容</param> public void Send(netEventEnum type, string message) { //消息构成 2byte总长,2byte消息类型,之后的都是消息内容 // 消息类型bytes byte[] typeBytes = new byte[2]; typeBytes[0] = (byte)(((int)type) % 256); typeBytes[1] = (byte)(((int)type) / 256); // 打印消息类 string strr = BitConverter.ToInt16(typeBytes, 0).ToString(); //Console.WriteLine("发送消息类型字符串:" + strr); if (type != netEventEnum.Pong) { Console.WriteLine(socket.RemoteEndPoint.ToString() + "发送消息类型:" + type.ToString()); } // 消息类型字符串 string str = Encoding.UTF8.GetString(typeBytes); // 写入消息队列 lock (sendMgrQueue) { sendMgrQueue.Enqueue(str + message); } }
public static byte[] EnMsgType(netEventEnum type) { string str = type.ToString(); return(Encoding.UTF8.GetBytes(str)); }