How to use advanced WebSocket controls (HTML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

This topic explains how to use advanced socket controls when using the MessageWebSocket and StreamWebSocket features in a Windows Runtime app.

What you need to know

Technologies

Prerequisites

  • The examples in this topic are provided in JavaScript. A basic understanding of WebSockets is recommended.

Overview of advanced WebSocket controls

The MessageWebSocket and StreamWebSocket classes follow the same model for using advanced controls. Corresponding with each of the above primary classes are related classes to access advanced controls:

The basic model to use advanced controls is the same for both types of WebSockets. The discussion below uses StreamWebSocket as an example, but the same process can be used with a MessageWebSocket.

The app must always set the property on the StreamWebSocketControl before issuing a connect operation. Because of this, it is best to set any advanced options immediately after the socket is created. Do not try and set a StreamWebSocketControl property after a socket has called the ConnectAsync method.

Advanced StreamWebSocket controls

There are several advanced options on the StreamWebSocket.

As an example, let's look at the StreamWebSocketControl.NoDelay option in more detail. The default setting when a StreamWebSocket is created is for this option to be set to true, which disables the Nagle algorithm. However, if the StreamWebSocket will be used for an app that sends many small packets, and latency is not an issue, then the Nagle algorithm could be enabled by setting StreamWebSocketControl.NoDelay to false in order to reduce network traffic.

The following example creates a StreamWebSocket and sets the StreamWebSocketControl.NoDelay to false. Once this is done, the app can connect the StreamWebSocket

    var clientWebSocket = new Windows.Networking.Sockets.StreamWebSocket();

    // Get the current setting for this option
    // This isn't required, but it shows how to get the current setting
    var currentSetting = clientWebSocket.control.noDelay; 

    // Set noDelay to false so that the Nagle algorithm isn't disabled
    clientWebSocket.control.noDelay = false;
   
    // Now you can call the ConnectAsync method to connect the StreamWebSocket.

Advanced MessageWebSocket controls

Many of the advanced options on the MessageWebSocket are the same as those on the StreamWebSocket, but there are some differences.

Setting the options on a MessageWebSocket is done in much the same manner as in the example above.

Remarks

In addition to control data, a similar set of related classes provide access to additional information on these primary classes:

These classes have properties that provide additional information on the socket.

Other

Connecting with WebSockets

How to connect with a MessageWebSocket

How to connect with a StreamWebSocket

How to secure WebSocket connections with TLS/SSL

Reference

MessageWebSocket

StreamWebSocket

Windows.Networking.Sockets

Samples

WebSocket sample