简体   繁体   中英

Handling WebSocket server-side

I'm trying to get websockets working on the Hololens. I currently have a StreamSocketListener acting as a server on the Hololens. The websocket upgrade request is received and I perform the websocket handshake correctly, the socket remains open on both client and server.

However, the type of socket which makes the handshake is a StreamSocket, not StreamWebSocket and I can't seem to write on the socket:

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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