You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PLT-PDA/Admin.Core.Socket/TouchSocketService.cs

56 lines
2.2 KiB
C#

1 year ago
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<MessageModel<string>> AddTouchSocketAsync(IHttpService httpService)
{
MessageModel<string> messageModel = new MessageModel<string>();
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<HeartbeatAndReceivePlugin>();
a.Add<MyWebSocketPlugin>();//MyWebSocketPlugin是继承自WebSocketPluginBase的插件。
a.UseWebApi();
}))
.Start();//启动
Console.WriteLine("服务器成功启动");
service.Logger.Info("服务器成功启动");
messageModel.success = true;
messageModel.msg = "OK";
return Task.FromResult(messageModel);
}
}
}