Connecting with WebSockets (XAML)
Use the Windows.Networking.Sockets namespace provided in the Windows Runtime environment to enable your Windows Store app to send and receive data using WebSockets for low-latency, secure, real-time bi-directional communication.
What are 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 Windows Store apps that include real-time gaming, instant social network notifications, up-to-date displays of stock or weather information, and other apps requiring secure and fast data transfer.
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.
Important 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.
Windows 8 provides support for both client and server use of WebSockets. The Windows.Networking.Sockets namespace defines two types of WebSocket objects for use by clients in Windows Store apps: MessageWebSocket and StreamWebSocket. The table below describes the two types of WebSockets.
|Suitable for typical scenarios where messages are not extremely large.||Suitable for scenarios in which large files (such as photos or movies) 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 socket (DatagramSocket).||Somewhat comparable to a TCP socket (StreamSocket).|
Developers using VB/C#/C++ and XAML interested in creating Windows Runtime apps that connect to a web service using WebSockets.
The following information applies to Windows Runtime apps written in C++/XAML and apps using the .NET Framework 4.5 in C# or Visual Basic.
In this section
|How to connect with a MessageWebSocket||
Details the requirements and specific steps for using a MessageWebSocket to send and receive data from a web server.
|How to connect with a StreamWebSocket||
Details the requirements and specific steps for using a StreamWebSocket to send and receive data from a web server.
|How to use advanced WebSocket controls||
Explains how to use advanced socket controls to set options on a WebSocket object.
|How to secure WebSocket connections with TLS/SSL||
Explains how to secure WebSocket connections with Transport Layer Security (TLS)/Secure Sockets Layer (SSL).
Learn about similar and related features
|How to configure network capabilities||
Explains the network isolation feature and how to configure network isolation capabilities to fully control network access for a Windows Store app.
|Making network connections with sockets||
Details the requirements and specific steps for using sockets to send and receive data from a network resource.
|Adding support for networking||
Contains information on adding support for networking to your app.
|Troubleshoot and debug network connections||
Provides suggestions and information to help troubleshoot connection issues.