private async void InitTcpConnectAndReceive() { var options = new ChannelOptions { Logger = NullLogger.Instance, ReadAsDemand = true }; _sendTcpClient = new EasyClient <TextPackageInfo>(new LinePipelineFilter(), options).AsClient(); _connected = await _sendTcpClient.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 8888)); var connectPackage = new TcpPackage() { OpCode = OpCode.Connect, LocalName = TbUserName.Text, RemoteName = "Server", MessageType = MessageType.Text }; await _sendTcpClient.SendAsync( new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(connectPackage.ToString()))); while (true) { var receivePackage = TcpPackage.JsonToPackage((await _sendTcpClient.ReceiveAsync()).Text); if (string.IsNullOrEmpty(receivePackage.Message)) { continue; } switch (receivePackage.OpCode) { case OpCode.Connect: SpOnlineList.Children.Clear(); var allItem = new UserItemsControl("All"); allItem.setRoteName = SetRoteName; SpOnlineList.Children.Add(allItem); foreach (var onlineClient in receivePackage.Clients) { var childItem = new UserItemsControl(onlineClient.Username); childItem.setRoteName = SetRoteName; SpOnlineList.Children.Add(childItem); } TbUserName.IsEnabled = false; BtnConnectServer.IsEnabled = false; break; case OpCode.DisConnect: MessageBox.Show(receivePackage.Message, receivePackage.LocalName); TbUserName.IsEnabled = true; break; case OpCode.All: case OpCode.Single: LbChatWith.Content = receivePackage.OpCode == OpCode.All ? receivePackage.RemoteName : receivePackage.LocalName; switch (receivePackage.MessageType) { case MessageType.Text: ChatArea.Children.Add(new ReceiveControl(receivePackage, null)); break; case MessageType.Image: // 建立UDP客户端 直接接收消息 ReceiveImage(receivePackage: receivePackage); break; case MessageType.File: var res = MessageBox.Show("是否接收文件?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Information); if (res == MessageBoxResult.Yes) { // 发送TCP确认连接消息 // 创建UDP客户端 } break; } break; case OpCode.Confirm: switch (receivePackage.MessageType) { case MessageType.Image: var fileName = await SendImage(); await Task.Delay(1000); await SendImage(fileName, receivePackage); break; case MessageType.File: break; } break; } Scr.ScrollToEnd(); if (_connected) { BdConnectState.Background = new SolidColorBrush(Colors.LimeGreen); TbUserName.IsEnabled = false; } else { BdConnectState.Background = new SolidColorBrush(Colors.OrangeRed); TbUserName.IsEnabled = true; break; } //await Task.Delay(1000); } }
static async Task Main(string[] args) { var host = SuperSocketHostBuilder.Create <TextPackageInfo, LinePipelineFilter>(args) .ConfigureSuperSocket(options => { options.AddListener(new ListenOptions { Ip = "Any", Port = 4040 }) .AddListener(new ListenOptions() { Ip = "Any", Port = 8888 }); }) .UseSession <MySession>() .UseSessionHandler(async s => { var data = new TcpPackage() { OpCode = OpCode.Connect, LocalName = "Server", RemoteName = "All", MessageType = MessageType.Text, Message = String.Empty, Clients = _clients }; var sessions = _sessionContainer.GetSessions(); foreach (var session in sessions) { string da = data.ToString(); await session.SendAsync(new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(da))); } }, async(s, e) => { _clients = _clients.Where(x => x.SessionId != s.SessionID).ToList(); var sessions = _sessionContainer.GetSessions().Where(x => x.SessionID != s.SessionID); var data = new TcpPackage() { OpCode = OpCode.Connect, LocalName = "Server", RemoteName = "All", MessageType = MessageType.Text, Message = "Connect Success.", Clients = _clients.Count() == 1 ? null : _clients }; foreach (var session in sessions) { string val = data.ToString(); await session.SendAsync(new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(val))); } }) .UsePackageHandler(async(s, p) => { var package = TcpPackage.JsonToPackage(p.Text); //Console.WriteLine($"\n{p.Text}\n"); switch (package.OpCode) { case OpCode.Connect: _clients.Add(new ClientInfo() { Username = package.LocalName, SessionId = s.SessionID }); var sessions = _sessionContainer.GetSessions(); foreach (var session in sessions) { var connectData = new TcpPackage() { OpCode = OpCode.Connect, MessageType = MessageType.Text, LocalName = "Server", RemoteName = _clients.Where(x => s.SessionID == x.SessionId)?.FirstOrDefault() != null ? _clients.FirstOrDefault(x => s.SessionID == x.SessionId)?.Username : null, Message = "Connect Success.", Clients = _clients.Where(x => x.SessionId != session.SessionID).ToList() }; string conn = connectData.ToString(); await session.SendAsync(new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(conn))); } break; case OpCode.All: TcpPackage allData = null; switch (package.MessageType) { case MessageType.Text: allData = new TcpPackage() { OpCode = OpCode.All, MessageType = MessageType.Text, LocalName = package.LocalName, RemoteName = "All", Message = package.Message }; break; case MessageType.Image: //var imgConfig = new UdpConfigPackage() //{ // SendEndPoint = $"{((IPEndPoint)s.LocalEndPoint).Address}|{11000}", // ReceiveEndPoint = $"{((IPEndPoint)s.RemoteEndPoint).Address}|{12000}" //}; allData = new TcpPackage() { OpCode = OpCode.All, MessageType = MessageType.Image, LocalName = "Server", RemoteName = package.LocalName, Config = null }; break; case MessageType.File: allData = new TcpPackage() { OpCode = OpCode.All, MessageType = MessageType.File, LocalName = "Server", RemoteName = package.LocalName, Message = s.LocalEndPoint.ToString() }; break; } var aSessionClients = _sessionContainer.GetSessions().Where(x => x.SessionID != s.SessionID); foreach (var sClient in aSessionClients) { string val = allData.ToString(); await sClient.SendAsync(new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(val))); } break; case OpCode.Single: TcpPackage singleData = null; switch (package.MessageType) { case MessageType.Text: singleData = new TcpPackage() { OpCode = OpCode.Single, MessageType = MessageType.Text, LocalName = package.LocalName, RemoteName = package.RemoteName, Message = package.Message }; break; case MessageType.Image: //var imgConfig = new UdpConfigPackage() //{ // SendEndPoint = $"{((IPEndPoint)s.LocalEndPoint).Address}.{11000}", // ReceiveEndPoint = $"{((IPEndPoint)s.RemoteEndPoint).Address}.{12000}" //}; singleData = new TcpPackage() { OpCode = OpCode.Single, MessageType = MessageType.Image, LocalName = "Server", RemoteName = package.LocalName, Message = null }; break; case MessageType.File: singleData = new TcpPackage() { OpCode = OpCode.Single, MessageType = MessageType.File, LocalName = package.LocalName, RemoteName = package.RemoteName, Message = s.LocalEndPoint.ToString() }; break; } var remoteSession = _clients.Where(y => y.Username == package.RemoteName); foreach (var rSession in remoteSession) { var sSessionClients = _sessionContainer.GetSessions() .Where(x => x.SessionID.Equals(rSession.SessionId)); foreach (var sClient in sSessionClients) { var sing = singleData.ToString(); await sClient.SendAsync( new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(sing))); } } break; case OpCode.DisConnect: break; case OpCode.Confirm: var imgConfig = new UdpConfigPackage() { SendEndPoint = $"{((IPEndPoint)s.LocalEndPoint).Address}|{11000}", ReceiveEndPoint = $"{((IPEndPoint)s.RemoteEndPoint).Address}|{12000}" }; TcpPackage confirmPackage = new TcpPackage() { OpCode = OpCode.Confirm, MessageType = package.MessageType, LocalName = package.LocalName, RemoteName = package.RemoteName, Message = "image", Config = imgConfig }; #region 测试数据 //var testleData = new TcpPackage() //{ // OpCode = OpCode.Confirm, // MessageType = MessageType.Text, // LocalName = package.LocalName, // RemoteName = package.RemoteName, // Message = JsonConvert.SerializeObject(imgConfig), // Config = imgConfig //}; //var testString = testleData.ToString(); #endregion var confirmMsg = confirmPackage.ToString(); await s.SendAsync(new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(confirmMsg))); if (package.RemoteName.Equals("All")) { confirmPackage.OpCode = OpCode.All; } else { confirmPackage.OpCode = OpCode.Single; } var cRemoteSession = _sessionContainer.GetSessions() .Where(x => x.SessionID != s.SessionID); var confirm = confirmPackage.ToString(); foreach (var session in cRemoteSession) { await session.SendAsync(new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(confirm))); } break; default: throw new ArgumentException(message: "op code error"); } }) .ConfigureErrorHandler((s, v) => { Console.WriteLine($"\n[{DateTime.Now}] [TCP] Error信息:" + s.SessionID.ToString() + Environment.NewLine); return(default);