MessageWebSocket class

Applies to Windows and Windows Phone

Supports network communication that allows reading and writing whole messages using a WebSocket.

Syntax


Public NotInheritable Class MessageWebSocket
    Implements IWebSocket, _
    IDisposable

Attributes

[DualApiPartition()]
[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06020000)]

Members

The MessageWebSocket class has these types of members:

Constructors

The MessageWebSocket class has these constructors.

ConstructorDescription
MessageWebSocket Creates a new MessageWebSocket object.

 

Events

The MessageWebSocket class has these events.

EventDescription
Closed Occurs when a close frame is received on the MessageWebSocket object as part of the close handshake.
MessageReceived An event that indicates that a message was received on the MessageWebSocket object.

 

Methods

The MessageWebSocket class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
Close() [C++, JavaScript]Closes the MessageWebSocket object and sends an empty close frame to the server.
Close(UInt16, String) Closes the MessageWebSocket object and indicates a reason for the closure.
ConnectAsync Starts an asynchronous operation to connect to a remote network destination on a MessageWebSocket object.
Dispose [C#, VB]Performs tasks associated with freeing, releasing, or resetting unmanaged resources.
SetRequestHeader Adds an HTTP request header to the HTTP request message used in the WebSocket protocol handshake by the MessageWebSocket object.

 

Properties

The MessageWebSocket class has these properties.

PropertyAccess typeDescription

Control

Read-onlyGets socket control data on a MessageWebSocket object.

Information

Read-onlyGets socket information on a MessageWebSocket object.

OutputStream

Read-onlyGets the output stream to write to the remote network destination on a MessageWebSocket object.

 

Remarks

The MessageWebSocket class provides a message-based abstraction of the WebSocket protocol. When using MessageWebSocket, the entire WebSocket message is read or written in a single operation. In contrast, the StreamWebSocket allows sections of a message to be read with each read operation, rather than requiring the entire message to be read in a single operation.

For UTF-8 messages, MessageWebSocket must be used. StreamWebSocket only supports binary messages.

Handling exceptions

You must write code to handle exceptions when you call asynchronous methods on the MessageWebSocket class. Exceptions can result from parameter validation errors, name resolutions failures, and network errors. Exceptions from network errors (loss of connectivity, connection failures, and HTTP server failures, for example) can happen at any time. These errors result in exceptions being thrown. If not handled by your app, an exception can cause your entire app to be terminated by the runtime.

The Windows.Networking.Sockets namespace has a convenient helper method and enumeration for handling errors when using WebSockets. This can be useful for handling specific network exceptions differently in your app. An app can also use the HRESULT from the exception on parameter validation errors to learn more detailed information on the error that caused the exception.

For more information on possible exceptions and how to handle exceptions, see Handling exceptions in network apps.

Using MessageWebSocket on Windows Server 2012

On Windows Server 2012 and Windows Server 2012 R2, the Windows.Networking.dll that implements most of the classes in the Windows.Networking.Sockets namespace will fail to load unless the Media Foundation feature is enabled. As a result, apps that use MessageWebSocket and related WebSocket classes in the Windows.Networking.Sockets namespace will fail if the Media Foundation feature is disabled. Windows Server 2012 or Windows Server 2012 R2 installs with the Media Foundation feature disabled.

The Media Foundation feature can be enabled on Windows Server 2012 or Windows Server 2012 R2 using Server Manager or by entering the following text in a command prompt or a script:

dism /online /enable-feature /featurename:ServerMediaFoundation

After the Media Foundation feature is enabled, the user is prompted to restart. Once the computer is restarted, classes for sockets and WebSockets in the Windows.Networking.Sockets namespace will work as expected.

Requirements

Minimum supported client

Windows 8 [Windows Store apps, desktop apps]

Minimum supported server

Windows Server 2012 [Windows Store apps, desktop apps]

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.Networking.Sockets
Windows::Networking::Sockets [C++]

Metadata

Windows.winmd

DLL

Windows.Networking.dll

Capabilities

internetClient
privateNetworkClientServer

See also

Other resources
Connecting with WebSockets (HTML)
Connecting with WebSockets (XAML)
Handling exceptions in network apps
How to connect with a MessageWebSocket (HTML)
How to connect with a MessageWebSocket (XAML)
How to use advanced WebSocket controls (HTML)
How to use advanced WebSocket controls (XAML)
Reference
MessageWebSocketControl
MessageWebSocketInformation
StreamWebSocket
Samples
Connecting with WebSockets sample

 

 

Show:
© 2014 Microsoft