Home > Articles > Web Development

WebSockets in Windows Store Apps

  • Print
  • + Share This
The WebSockets protocol provides full-duplex communication on top of a TCP connection that is initiated by an HTTP handshake. The fact that it runs over existing HTTP and HTTPS ports allows it to pass easily through most existing firewall configurations. The Windows Runtime provides a powerful set of networking APIs that includes components specifically designed for WebSockets communications. In this article, Jeremy Likness shows how to connect to WebSockets using both message-based and real-time connections from Windows Store apps.
Like this article? We recommend

The WebSocket protocol was standardized in 2011. It was designed specifically to be implemented in web browsers and web servers. The standard is TCP-based and by default runs on the same ports as HTTP and HTTPS for standard and secured implementations, respectively. A common myth is that it uses HTTP for communications. The HTTP protocol is only used for an initial handshake to establish communication and upgrade the request to use the WebSockets protocol. Once the connection is established, it provides a continuous socket between the client and the server available for real-time communications to facilitate scenarios ranging from online games to collaboration and messaging apps.

The WebSockets protocol is fully supported by Windows Store apps through the Windows Runtime (WinRT). This makes it useful to provide a way for the Windows Store app to maintain a single connection with a server and send data both ways while keeping that connection open. It is less likely to be blocked by firewalls because it uses the same ports that the HTTP protocol does. The implementation for WebSockets exists in the Windows.Networking.Sockets namespace.

A Sockets Primer

Sockets facilitate inter-process communication (IPC) between processes running on a network. Although it is possible to use IPC on the same machine, the most common case is for communication between physically separate machines. The most common protocol for IPC communication is called Transmission Control Protocol/Internet Protocol (TCP/IP). TCP/IP is often referred to as “the language of the internet.”

TCP/IP provides two layers. One layer, TCP, handles the actual messaging component. It can take large messages, or streams, break them into packets to transmit over the network, then verify that there was no data loss and reassemble the packets at the target site. The second layer, IP, handles routing to ensure the messages make it to the right destination.

A typical session involves two endpoints. An endpoint is the combination of an IP address and a port. A port is simply a unique number used to describe the type of communication that is expected. By convention, popular protocols use specific ports, although this can be overridden. For example, the HTTP protocol uses TCP on port 80, while the secured HTTPS protocol uses port 443 by default. The client endpoint will “call” server endpoint using an address and port. If the server responds, the client typically provides a dynamic port from a range of reusable ports to establish the client endpoint. This is shared with the server and two-way communications can begin.

Protocols like WebSockets and HTTP use TCP. In the case of HTTP, well-defined messages are exchanged via HTTP that defined requests and responses. When you navigate to a web page, a request is sent using TCP that the server understands. For example, if you type in the URL to my personal website, http://www.jeremylikness.com/, something like this will be sent to the server:

GET http://www.jeremylikness.com/ HTTP/1.1
Host: http://www.jeremylikness.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36
 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
DNT: 1
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

The message indicates how the page should be fetched, what the page is, and even the types of responses the browser is capable of handling. The user agent portion of the request describes the client and is useful for both tracking analytics by the server as well as serving the appropriate page that is supported by the client. Notice the request can also include language and encoding options such as the ability to decompress data if the server should choose to compress it before sending.

The WebSocket protocol uses the same port but enables real-time, two-way messaging. Like HTTP, it uses TCP for the underlying connection. Unlike HTTP, it allows information to be streamed both ways simultaneously. This allows the client to provide continuous data to the server (such as actions that are taking place in an online game) while the server can provide its own updates, whether those describe what is happening on the virtual game universe or contain the latest numbers for a stock ticker display.

The WinRT Implementation of WebSockets

The Windows Runtime provides implementations for creating a WebSockets client or server. A server is less practical because clients typically roam and do not have well-known IP addresses, so this article will focus primarily on the client implementation. There are two types of WebSocket client APIs you can use.

The MessageWebSocket is a simpler implementation that is suitable for smaller messages. It queues an entire “message” and provides notification when those messages are received. A message can be UTF-8 or binary formatted. The StreamWebSocket is suitable for larger messages or continuous, real-time streams. You control how much of a message you read at any given moment, and because the messages are implemented as streams, they support only binary messages.

WebSockets in Action

My latest book, Programming the Windows Runtime: A Comprehensive Guide to WinRT with Examples in C# and XAML, provides extensive coverage of networking APIs in Chapter 10. The sample code for the book is open source. You can download the sample code from http://winrtexamples.codeplex.com/ and open the WebSocketsExamples project from the Chapter10 solution folder to follow along with this example.

The example project demonstrates both APIs you can use from WinRT to take advantage of the WebSockets protocol. It uses a server provided by the http://WebSocket.org website that exposes an “echo service.” This service will echo back any data sent to it by a connect client. WebSockets are accessed using a standard URI, as declared in MainPage.xaml.cs:

private readonly Uri echoService = 
    new Uri("ws://echo.websocket.org", UriKind.Absolute);

The MessageWebSocket class is an abstraction of the protocol that focuses on sending simple messages. A message is either read or written in a single operation, as opposed to being streamed continuously. It is also the class you must use to support UTF8 messages, because the stream-based API only supports binary (although you can encode and decode the binary to and from UTF8, the MessageWebSocket class provides native support for this). In order to use any socket type within a Windows Store app, you must enable either the Home or Work Networking or the Internet (Client & Server) capability.

The ButtonBase_OnClick method in the MainPage.xaml.cs file demonstrates how to use the MessageWebSocket class. After creating an instance of the class, set the type of the message (either binary or UTF8):

this.socket.Control.MessageType = SocketMessageType.Utf8;

You can also register for events that will fire whenever a message is received and when the socket is closed. The socket uses underlying unmanaged resources and should be disposed when you are done using it. The easiest way to do this is to call Dispose in the Closed event handler.

Initiate the connection by calling and waiting for ConnectAsync to complete:

await this.socket.ConnectAsync(echoService);

The example app will accept any message you type and send it to the echo service. The message must be sent using the IOutputStream exposed by the socket. The easiest way to do this is to create an instance of a DataWriter to send the message. The DataWriter allows you to write various data types that it buffers until you call StoreAsync. This will flush the buffer to the underlying stream.

var writer = new DataWriter(this.socket.OutputStream);
writer.WriteString(this.Text.Text);
await writer.StoreAsync();

Not all error messages for the socket are mapped to .NET Exception class instances. Instead, you must inspect the HResult of the underlying exception to determine what went wrong. Fortunately, the class provides a static method that translates the result to the corresponding WebErrorStatus enumeration. The ToErrorMessage method returns a string with the original message and the enumeration value.

private static string ToErrorMessage(Exception ex)
{
    var status = WebSocketError.GetStatus(
        ex.GetBaseException().HResult);
    return string.Format("{0} ({1})", ex.Message, status);
}

The MessageReceived event is raised whenever a message is sent from the server to the client through the socket. In the example app, this should happen any time data is sent because the server will echo the data back. The event sends the socket that the information was received from within event arguments that provide access to the message. You can inspect the message type (binary or UTF8) and open a reader or stream to access the message. In this example, the reader is set to use UFT8 encoding, then obtains the message and displays it in the SocketMessageReceived event handler.

using (var reader = args.GetDataReader())
{
    reader.UnicodeEncoding = UnicodeEncoding.Utf8;
    var text = reader.ReadString(reader.UnconsumedBufferLength);
    this.Response.Text = text;
}

This is the simplest method for dealing with sockets that are designed to share messages. In cases where you are using the socket to stream real-time information and don’t necessarily have simple messages, you may want to use the StreamWebSocket implementation instead. This provides a continuous two-way stream for sending and receiving information. The example app uses the same echo service to stream prime numbers and echo them back to the display when you click the Start button.

You create and connect to a StreamWebSocket the same way as a MessageWebSocket. You can also register for the Closed event. Instead of sending and receiving messages, however, the stream version expects you to interface directly with the input and output streams provided by the socket. The example app starts a long-running Task encapsulated in the ComputePrimes method. It is passed the OutputStream of the socket. It iterates through positive integers and writes out any that are computed to be primes, then delays for 1 second:

if (IsPrime(x))
{
    var array = Encoding.UTF8.GetBytes(string.Format(" {0} ", x));
    await outputStream.WriteAsync(array.AsBuffer());
    await Task.Delay(TimeSpan.FromSeconds(1));
}

If the integer is not a prime, it delays for a millisecond just to prevent hogging the CPU. Another long-running task is used to receive the echo. It allocates a buffer, waits for data to arrive in the stream, then reads and decodes the data.

var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
    var text = Encoding.UTF8.GetString(buffer, 0, bytesRead);
    this.DispatchTextToPrimes(text);                        
} 

This example also demonstrates the fact that you can have multiple sockets open to the same destination at once. You can run the example, click the button to start generating primes, then use the message-based version to send and receive messages without interrupting the stream of prime numbers. Both methods for communicating with the socket simplify the amount of code you have to write by not worrying about the details of the underlying transport (TCP). When you need to manage a raw TCP connection, you can use the traditional sockets components.

Advanced WebSockets

The example app demonstrates the basic use of WebSockets in Windows Store apps. The components for WebSocket communication support advanced options as well. The MessageWebSocket component provides the following settings:

  • MaxMessageSize – Used to configure the maximum size of a single message sent or received.
  • MessageType – Controls the type of message (UTF8 or Binary).
  • OutboundBufferSizeInBytes – Controls the size of the send buffer.
  • ProxyCredential – Use this to configure a credential for the proxy server (will be sent as part of the HTTP header authentication).
  • ServerCredential – Used to configure a credential to be read by the WebSocket server.
  • SupportedProtocols – Provides a list of sub-protocols supported by the web socket (these are protocols that use the WebSocket but provide additional functionality through their own protocol implementation).

The StreamWebSocket control provides these options:

  • NoDelay – Turn the Nagle algorithm on or off.
  • OutboundBufferSizeInBytes – Like the MessageWebsocket, controls the size of the send buffer.
  • ProxyCredential – Credentials to authenticate to the proxy server.
  • ServerCredential – Credentials to authenticate to the WebSocket server.
  • SupportedProtocols – Provides a list of sub-protocols.

In addition, you can also secure the WebSocket communications. The default scheme ws:// uses the standard, unsecured protocol. If the server supports encrypted connections, use the wss:// scheme instead. This will secure the connection by encrypting the streams. It works by tunneling the stream through Transport Layer Security (TLS) and Secure Sockets Layer (SSL).

The WebSockets protocol is a new but popular protocol that makes it easy to build real-time communications into your Windows Store apps. WinRT supports a variety of network protocols that are all covered in Chapter 10 of Programming the Windows Runtime: A Comprehensive Guide to WinRT with Examples in C# and XAML. The book contains 20 chapters plus a comprehensive glossary and more than 80 open-source projects providing specific examples that use WinRT APIs.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020