提交代码
This commit is contained in:
52
WCS.BLL/Manager/WebSoceketManager.cs
Normal file
52
WCS.BLL/Manager/WebSoceketManager.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Http;
|
||||
using TouchSocket.Sockets;
|
||||
|
||||
namespace WCS.BLL.Manager
|
||||
{
|
||||
public static class WebSoceketManager
|
||||
{
|
||||
public static HttpService service;
|
||||
public static void InitWebSocket()
|
||||
{
|
||||
service = new HttpService();
|
||||
service.Setup(new TouchSocketConfig()//加载配置
|
||||
.SetListenIPHosts(7789)
|
||||
.ConfigureContainer(a =>
|
||||
{
|
||||
a.AddConsoleLogger();
|
||||
})
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
a.UseWebSocket()//添加WebSocket功能
|
||||
.SetWSUrl("/ws")//设置url直接可以连接。
|
||||
.UseAutoPong();//当收到ping报文时自动回应pong
|
||||
a.Add<WebSocketServicePlugin>();//自定义插件。
|
||||
}));
|
||||
|
||||
service.Start();
|
||||
service.Logger.Info("服务器已启动");
|
||||
}
|
||||
//尝试发送报警信息给前端
|
||||
public static void TrySendMessage(string IpAddress, string Message)
|
||||
{
|
||||
try
|
||||
{
|
||||
var clients = service.GetClients().Where(t => t.IP == IpAddress).ToList();
|
||||
foreach (var client in clients)
|
||||
{
|
||||
client.WebSocket.SendAsync(Message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user