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 allSockets = new List(); 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); } } } }