C服务器端

霄
2023-05-22 / 0 评论 / 31 阅读 / 正在检测是否收录...
/// <summary>
/// web套接字模块
/// </summary>
public class WebSocketService
{
//存储所有连接状态
public static List<IWebSocketConnection> Sockets = new List<IWebSocketConnection>();
    //开启套接字连接
    public static void WebSocketStart()
    {

        var server = new WebSocketServer(ws://127.0.0.1:8008);
        server.Start(socket =>
        {
            socket.OnOpen = () =>
            {
                WebSocketService.Sockets.Add(socket);
                socket.Send(即时通讯服务开启成功!);
            };
            socket.OnClose = () =>
            {
                WebSocketService.Sockets.Add(socket);
            };
            socket.OnMessage = message =>
            {
                WebSocketService.Sockets.ToList().ForEach(s => s.Send(message));
            };
        });
    }

    //发送消息
    public static void SendMessage(string message) {
            foreach (var socket in Sockets.ToList())
            {
                socket.Send(message);
            }
        }

}
扫描二维码,在手机上阅读!
55

评论

博主关闭了当前页面的评论