Windows 用于广播的通用 (IoT) Websocket 服务器

Windows Universal (IoT) Websocket server to broadcasting

我需要像库 Websocket Sharp 这样的东西来 Windows 通用。我正在尝试在 Windows 10 IoT 上构建服务器来广播一些数据(温度和其他事件),我想构建类似流媒体的东西来广播来自 Arduino 的实时数据。 在 samples 中,我只能找到制作 websocket 客户端的说明。有任何想法吗?

通用 Windows 平台 (UWP) 支持客户端和服务器使用 WebSockets。您可能会找到一个名为 IoTWeb 的库,它允许您将简单的 HTTP 和 WebSocket 服务器嵌入到您的 UWP 应用程序中。这个库提供了一个简单的 HTTP 服务器,支持 WebSocket。

class WebSocketHandler : IWebSocketRequestHandler
{
    private Dictionary<string,WebSocket> _webSockets = new Dictionary<string, WebSocket>();
    public event MessageRecivedHandler MessageRecived;
    private IdHelper _portMappings;

    public WebSocketHandler(IdHelper portMappings)
    {
        _portMappings = portMappings;
    }



    public void Connected(WebSocket socket)
    {
        var guid = Guid.NewGuid().ToString();
        _webSockets.Add(guid, socket);


        socket.DataReceived += (webSocket, frame) => MessageRecived?.Invoke(_webSockets.First(x => x.Value == webSocket).Key,webSocket, frame);
        socket.ConnectionClosed += (webSocket) =>
        {
            var connection = _webSockets.First(s => s.Value == webSocket);
            _webSockets.Remove(connection.Key);
        };

        if (_portMappings.IsBindingPosible())
        {
            _portMappings.Bind(guid);

            var message = new ServerMessage()
            {
                ClientID = guid,
                Command = "Init",
                Value = _portMappings.CheckBinding(guid).ToString()
            };
            SendMessage(guid, message);
        }
        else
        {
            var message = new ServerMessage()
            {
                ClientID = guid,
                Command = "Init",
                Value = "Max Number Of Clients Reached. Please close connection"
            };
            SendMessage(guid, message);
        }


    }

    public bool WillAcceptRequest(string uri, string protocol)
    {
        return true;
    }

    public async Task BroadcastMessage(string message)
    {
        await ThreadPool.RunAsync((workItem) => _webSockets?.AsParallel().ForAll(webSocket => webSocket.Value.Send(message)));
    }

    public async Task SendMessage(string guid, ServerMessage message)
    {
        await ThreadPool.RunAsync((workItem) => _webSockets?.First(x => x.Key == guid).Value.Send(JsonConvert.SerializeObject(message)));
    }
    public delegate void MessageRecivedHandler(string guid, WebSocket socket, string message);
}