Compartir a través de


How to use advanced WebSocket controls (XAML)

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

What you need to know

Technologies

Prerequisites

  • The examples in this topic are provided in C#. 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 call other methods on the StreamWebSocket to connect the socket.

    clientWebSocket = new StreamWebSocket();

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

    // Don't disable the Nagle algorithm
    clientWebSocket.Control.NoDelay = false;

    // Now you can use the StreamWebSocket to call one of the
    // ConnectAsync methods.

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, there are a similar set of related classes that provide access to additional information on these primary classes:

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

Other

Connecting with WebSockets

Create your first Windows Runtime app using C# or Visual Basic

Create your first Windows Runtime app using C++

How to configure network capabilities

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