Bagikan melalui


How to secure WebSocket connections with TLS/SSL (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 secure WebSocket connections with Transport Layer Security (TLS)/Secure Sockets Layer (SSL) when using the StreamWebSocket and MessageWebSocket features in a Windows Runtime app.

In most cases you'll want to use a secure WebSocket connection. This will increase the chances that your connection will succeed, as many proxies will reject unencrypted WebSocket connections.

Technologies

Instructions

Step 1: Use the wss: URI scheme

The WebSocket Protocol defines two URI schemes. ws: is used for unencrypted connections, while wss: is used for secure connections that should be encrypted.

  • To encrypt your connection, use the wss: URI scheme. For example:

    var webSocket = new Windows.Networking.Sockets.MessageWebSocket();
    webSocket.connectAsync("wss://www.example.com").done(function() {
         // connect succeeded
    }, function(e) {
         // connect failed
    });
    

Remarks

For additional details on the WebSocket URI schemes, see the WebSocket Protocol.

Other

Connecting with WebSockets

How to connect with a MessageWebSocket

How to connect with a StreamWebSocket

Reference

MessageWebSocket

MessageWebSocket.ConnectAsync

StreamWebSocket

StreamWebSocket.ConnectAsync

Windows.Networking.Sockets

Samples

WebSocket sample