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.
|
|
|
|
using Fleck;
|
|
|
|
|
using SLH.SSDMS.Common;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SLH.SSDMS.WebSocket
|
|
|
|
|
{
|
|
|
|
|
public class WebSocketServer
|
|
|
|
|
{
|
|
|
|
|
private static List<IWebSocketConnection> allSockets = new List<IWebSocketConnection>();
|
|
|
|
|
|
|
|
|
|
public static void Init()
|
|
|
|
|
{
|
|
|
|
|
FleckLog.Level = LogLevel.Debug;
|
|
|
|
|
var server = new Fleck.WebSocketServer("ws://0.0.0.0:7181");
|
|
|
|
|
server.Start(socket =>
|
|
|
|
|
{
|
|
|
|
|
socket.OnOpen = () =>
|
|
|
|
|
{
|
|
|
|
|
var data = socket.ConnectionInfo;
|
|
|
|
|
Console.WriteLine("Open!");
|
|
|
|
|
allSockets.Add(socket);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
socket.OnClose = () =>
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Close!");
|
|
|
|
|
allSockets.Remove(socket);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
socket.OnMessage = message =>
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(message);
|
|
|
|
|
socket.Send("Echo:" + message);
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void sendMessage(string message)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var socket in allSockets.ToList())
|
|
|
|
|
{
|
|
|
|
|
socket.Send(message);
|
|
|
|
|
LogHelper.Info("WebSocket接口上传数据:" + message);
|
|
|
|
|
}
|
|
|
|
|
}catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LogHelper.Error("WebSocket上传数据异常", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|