static void Main(string[] args) { Console.Title = "SAEA.Sockets.UdpTest"; //udpserver UDPServer server = new UDPServer(); server.OnReceive += Server_OnReceive; server.Start(); //udpclient var bContext = new BaseContext(); UDPClient client = new UDPClient(); client.OnReceive += Client_OnReceive; client.Connect(); //send msg for (int i = 0; i < 10; i++) { client.SendAsync(BaseSocketProtocal.Parse(Encoding.UTF8.GetBytes($"hello udpserver {i}"), SocketProtocalType.ChatMessage)); } Console.ReadLine(); }
void ReplyGroupMessage(MessageUserToken userToken, ChatMessage cm) { ReplyBase(userToken, new ChatMessage(ChatMessageType.GroupMessageAnswer, "")); var groupMsg = cm.GetIMessage <GroupMessage>(); if (groupMsg != null && !string.IsNullOrEmpty(groupMsg.Name)) { groupMsg.Sender = userToken.ID; groupMsg.Sended = DateTimeHelper.ToString(); var group = _groupList.Get(groupMsg.Name); if (group != null && group.Members != null) { lock (_groupList.SyncLocker) { var ccm = new ChatMessage(ChatMessageType.GroupMessage, groupMsg); var data = SerializeHelper.PBSerialize(cm); var sp = BaseSocketProtocal.Parse(data, SocketProtocalType.ChatMessage).ToBytes(); Parallel.ForEach(group.Members, (m) => { if (m.ID != userToken.ID) { _server.SendAsync(m.ID, sp); } }); } } } }
void ReplyChannelMessage(MessageUserToken userToken, ChatMessage cm) { var channelMsg = SerializeHelper.Deserialize <ChannelMessage>(cm.Content); if (channelMsg != null && !string.IsNullOrEmpty(channelMsg.Name)) { channelMsg.Sender = userToken.ID; channelMsg.Sended = DateTimeHelper.ToString(); var channel = _channelList.Get(channelMsg.Name); if (channel != null && channel.Members != null) { var members = channel.Members.ToArray(); var ccm = new ChatMessage(ChatMessageType.ChannelMessage, SerializeHelper.Serialize(channelMsg)); var data = SerializeHelper.PBSerialize(ccm); var sp = BaseSocketProtocal.Parse(data, SocketProtocalType.ChatMessage).ToBytes(); Parallel.ForEach(members, (m) => { if (m.ID != userToken.ID) { _server.SendAsync(m.ID, sp); } }); } } }
private void SendBase(ChatMessage cm) { var data = SerializeHelper.PBSerialize(cm); var content = BaseSocketProtocal.Parse(data, SocketProtocalType.ChatMessage).ToBytes(); _batcher.Insert(content); }
void ReplyBase(IUserToken userToken, ChatMessage cm) { var data = SerializeHelper.PBSerialize(cm); var sp = BaseSocketProtocal.Parse(data, SocketProtocalType.ChatMessage).ToBytes(); _classificationBatcher.Insert(userToken.ID, sp); }
void ReplyBase(IUserToken userToken, ChatMessage cm) { var data = SerializeHelper.PBSerialize(cm); var sp = BaseSocketProtocal.Parse(data, SocketProtocalType.ChatMessage).ToBytes(); _server.SendAsync(userToken.ID, sp); }
private void SendBase(ChatMessage cm) { var data = SerializeHelper.PBSerialize(cm); var content = BaseSocketProtocal.Parse(data, SocketProtocalType.ChatMessage).ToBytes(); _client.SendAsync(content); Actived = DateTimeHelper.Now; }
private static void Server_OnReceive(UDPServer arg1, string arg2, ISocketProtocal arg3) { lock (_locker) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"udp服务器收到消息:{Encoding.UTF8.GetString(arg3.Content)}"); } var msg2 = BaseSocketProtocal.Parse(Encoding.UTF8.GetBytes($"udpserver reply:{Encoding.UTF8.GetString(arg3.Content)}"), SocketProtocalType.ChatMessage); arg1.SendAsync(arg2, msg2); }
/// <summary> /// SendAsync /// </summary> /// <param name="id"></param> /// <param name="data"></param> /// <param name="socketProtocalType"></param> public void SendAsync(string id, byte[] data, SocketProtocalType socketProtocalType = SocketProtocalType.ChatMessage) { SendAsync(id, BaseSocketProtocal.Parse(data, socketProtocalType)); }