WebSockets

The WebSocket Protocol defines a mechanism for fast, secure two-way communication between a client and a server over the web. Data is transferred immediately over a full-duplex single socket connection, allowing messages to be sent and received from both endpoints in real time. WebSockets are ideal for use in real-time gaming where instant social network notifications and up-to-date displays of information (game statistics ) need to be secure and use fast data transfer. Xbox One supports RFC 6455, the same spec version of the protocol supported on Windows 8.

To establish a WebSocket connection, a specific, HTTP-based handshake is exchanged between the client and the server. If successful, the application-layer protocol is “upgraded” from HTTP to WebSockets, using the previously established TCP connection. Once this occurs, HTTP is completely out of the picture; data can be sent or received using the WebSocket protocol by both endpoints, until the WebSocket connection is closed.

Note A client cannot use WebSockets to transfer data unless the server also uses the WebSocket protocol. If the server does not support WebSockets, you must use another method of data transfer.

The Windows.Networking.Sockets namespace defines two types of WebSocket objects: Windows.Networking.Sockets.MessageWebSocket and Windows.Networking.Sockets.StreamWebSocket. The table below describes the two types of WebSockets.

MessageWebSocket StreamWebSocket
Suitable for typical scenarios where messages are not extremely large. Suitable for scenarios in which large files (such as photos or videos) are being transferred.
Enables notification that an entire WebSocket message has been received. Allows sections of a message to be read with each read operation.
Supports both UTF-8 and binary messages. Supports only binary messages.
Somewhat comparable to a UDP or datagram socket. Somewhat comparable to a TCP or stream socket.

In most cases you’ll want to use a secure WebSocket connection to encrypt data sent and received. This will also increase the chances that your connection will succeed, as many proxies will reject unencrypted WebSocket connections.

The WebSocket Protocol defines two URI schemes.

To encrypt a WebSocket connection, use the wss: URI scheme. For example:

    MessageWebSocket^ webSocket = = ref new MessageWebSocket();
    // skipping code needed to setup event listeners for received messages and when websocket is closed

    Uri^ server = "wss://www.contoso.com";
    MessageWebSocket->ConnectAysnc(server);  

WebSockets can be used to connect to the RTA service.

In this section

How To Use MessageWebSocket
Shows how to use Windows.Networking.Sockets.MessageWebSocket to establish a connection and send and receive data.

How To Use Advanced Controls with WebSockets
Shows how to use advanced controls with Windows.Networking.Sockets.MessageWebSocket and Windows.Networking.Sockets.StreamWebSocket.

How To Handle Network Exceptions on WebSocket Operations
Shows how to handle network exceptions on Windows.Networking.Sockets.MessageWebSocket and Windows.Networking.Sockets.StreamWebSocket operations.

How To Set Timeouts on WebSocket Operations
Shows how to set timeouts on Windows.Networking.Sockets.MessageWebSocket and Windows.Networking.Sockets.StreamWebSocket operations.

See also

Xbox Live Services API Reference

Windows.Xbox.Networking Namespace

Windows.Networking.Sockets Namespace