using Admin.Core.Common; using Admin.Core.Socket.TSocket; using Autofac.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TouchSocket.Core; using TouchSocket.Http; using TouchSocket.Http.WebSockets; using TouchSocket.Sockets; namespace Admin.Core.Socket { public class TouchSocketService : ITouchSocketService { public Task> AddTouchSocketAsync(IHttpService httpService) { MessageModel messageModel = new MessageModel(); HttpService service = new HttpService(); service.Connecting = (client, e) => { client.Logger.Info($"{client.ID}有客户端正在连接"); };//有客户端正在连接 service.Connected = (client, e) => { client.Logger.Info($"{client.ID}有客户端连接成功!!!!"); };//有客户端成功连接 service.Disconnected = (client, e) => { client.Logger.Info($"{client.ID}有客户端断开"); };//有客户端断开连接 service.Setup(new TouchSocketConfig()//载入配置 .SetListenIPHosts(new IPHost[] { new IPHost(Appsettings.app("Middleware", "TouchSocket", "Address").ObjToString()) })//可同时监听两个地址 .UsePlugin() .SetDataHandlingAdapter(() => new MyFixedHeaderDataHandlingAdapter()) .ConfigureContainer(a => { a.AddConsoleLogger(); }) .ConfigurePlugins(a => { a.UseWebSocket()//添加WebSocket功能 .SetWSUrl("/ws"); a.Add(); a.Add();//MyWebSocketPlugin是继承自WebSocketPluginBase的插件。 a.UseWebApi(); })) .Start();//启动 Console.WriteLine("服务器成功启动"); service.Logger.Info("服务器成功启动"); messageModel.success = true; messageModel.msg = "OK"; return Task.FromResult(messageModel); } } }