简体   繁体   English

处理WebSocket服务器端

[英]Handling WebSocket server-side

I'm trying to get websockets working on the Hololens. 我正在尝试让Websocket在Hololens上工作。 I currently have a StreamSocketListener acting as a server on the Hololens. 目前,我有一个StreamSocketListener充当Hololens上的服务器。 The websocket upgrade request is received and I perform the websocket handshake correctly, the socket remains open on both client and server. 收到了websocket升级请求,并且我正确执行了websocket握手,套接字在客户端和服务器上均保持打开状态。

However, the type of socket which makes the handshake is a StreamSocket, not StreamWebSocket and I can't seem to write on the socket: 但是,导致握手的套接字类型是StreamSocket,而不是StreamWebSocket,我似乎无法在套接字上写:

private async void ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs eventArgs)
{
    RequestHandler.Request request = await RequestHandler.ParseRequest(eventArgs.Socket.InputStream);

    RequestHandler.Response response = RequestHandler.ProcessRequest(request);

    byte[] encodedResponse = Encoding.UTF8.GetBytes(response.mHeader);
    await eventArgs.Socket.OutputStream.WriteAsync(encodedResponse.AsBuffer());

    if (null != response.mTextContent || null != response.mBinaryContent)
    {
        if (null != response.mTextContent)
        {
            encodedResponse = Encoding.UTF8.GetBytes(response.mTextContent);
        }
        else
        {
            encodedResponse = response.mBinaryContent;
        }

        await eventArgs.Socket.OutputStream.WriteAsync(encodedResponse.AsBuffer());
    }

    if (request.mWebsocketUpgrade)
    {
        receivedSocket = eventArgs.Socket;
    }
    else
    {
        eventArgs.Socket.Dispose();
    }
}

Is it possible to 'upgrade' or 'convert' the received socket to a StreamWebSocket or is this approach flawed and needs to go a different way? 是否可以将接收到的套接字“升级”或“转换”为StreamWebSocket,还是这种方法存在缺陷,需要采取其他方式?

Any advice would be much appreciated! 任何建议将不胜感激!

It turns out that you can just use this socket, at least for my basic intentions which is to be able to push basic events to the browser only. 事实证明,您只能使用此套接字,至少出于我的基本意图,即只能将基本事件推送到浏览器。 There is almost definitely a better way to do this but it does work. 几乎肯定有更好的方法可以做到这一点,但它确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM