This topic shows how to use advanced controls when using WebSockets. 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 a StreamWebSocket as an example, but the same process can be used with a MessageWebSocket.
Both StreamWebSocket and MessageWebSocket impose requirements on when advanced controls can be set:
The following sections detail the advanced controls available on the different web socket classes.
There are several advanced options on the StreamWebSocket.
As an example, let’s look at the StreamWebSocketControl.NoDelay option in more detail.
Nagle’s algorithm is a technique for improving the efficiency of TCP/IP network communications by reducing the number of packets that are needed to be sent over the network. The algorithm tries to deal with problems caused by an application that repeatedly emits data in small chunks. A TCP packet for IPv4 without any other header options has a 40-byte header (20 bytes for IP and 20 bytes for TCP). So if an app sends a small number of bytes in a packet, the overhead on the packet data is very large. Over a slow link, many of these packets may be in transit over the network at the same time. Nagle’s algorithm works by combining a number of small outgoing messages, and sending them all at once. When there is a sent packet for which the sender has received no acknowledgment, the sender keeps buffering output until it has a full packet’s worth of output. This allows the output to be sent all at once. The impact of applying Nagle’s algorithm is to increase the bandwidth at the expense of latency. A well-written app that buffers sends internally should not need to use Nagle’s algorithm.
The default setting when a StreamWebSocket is created sets this option 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 call other methods on the StreamWebSocket to register callbacks and connect the StreamWebSocket.
using Windows::Networking::Sockets;
StreamWebSocket^ clientSocket = ref new StreamWebSocket();
// The StreamWebSocketControl object is associated with the StreamWebSocket->Control property.
// Get the current setting for this option
// This isn't necessary before setting the value; it is included here to show how to get a control setting's current value.
bool currentSetting = clientSocket->Control->NoDelay;
// Don't disable the Nagle algorithm
clientSocket->Control->NoDelay = false;
// Once all control setting changes are made, use the StreamWebSocket->ConnectAsync method to connect to the server.
Some of the advanced options on the MessageWebSocket are the same as those on the StreamWebSocket, but some are different.
As an example, let’s look at the MessageWebSocketControl.MessageType option in more detail.
A WebSocket message on the MessageWebSocket object can be either a binary message or a UTF-8 message. This property only affects write operations. It does not affect the format of received messages. Your app can set the MessageWebSocketControl.MessageType property when needed and can switch between binary and UTF-8 messages.
The following example creates a MessageWebSocket and initially sets the MessageWebSocketControl.MessageType to UTF-8.
using Windows::Networking::Sockets;
MessageWebSocket^ clientSocket = ref new MessageWebSocket();
// The StreamWebSocketControl object is associated with the MessageWebSocket->Control property.
// Get the current setting for this option
// This isn't necessary before setting the value; it is included here to show how to get a control setting's current value.
SocketMessageType currentSetting = clientSocket->Control->MessageType;
// Set the message type to UTF-8
clientSocket->Control->MessageType = SocketMessageType::Utf8;
// Once all control setting changes are made, use the MessageWebSocket->ConnectAsync method to connect to the server.
In addition to control data, there are a similar set of related classes that provide access to additional socket information on the MessageWebSocket and StreamWebSocket classes.
The model to access additional socket information follows the same design as the access to control data. The discussion below uses a StreamWebSocket as an example, but the same process can be used with a MessageWebSocket.
There is one significant difference between the WebSocket information and WebSocket control classes. The properties on a StreamWebSocketControl instance are readable or writable (get or set). In contrast, the properties on a StreamWebSocketInformation instance are read-only (get). An app may retrieve the value of a property on a StreamWebSocketControl or StreamWebSocketInformation instance at any time after the StreamWebSocket was created. However, an app must always set a property on a StreamWebSocketControl instance before issuing a connect operation.