An Uno Platform implementation of .NET System.Net.WebSocket for WebAssembly
Перейти к файлу
Jérôme Laban 85732a09f9
docs: Update readme
2022-01-12 08:14:40 -05:00
build Initial sources 2018-12-16 21:16:49 -05:00
src feat: Enable support for net5 2020-11-07 23:11:21 -05:00
.gitattributes Initial sources 2018-12-16 21:16:49 -05:00
.gitignore Initial sources 2018-12-16 21:16:49 -05:00
.vsts-ci.yml ci: Update version, gitversion 2020-06-29 15:44:26 -04:00
License.md Initial sources 2018-12-16 21:16:49 -05:00
Readme.md docs: Update readme 2022-01-12 08:14:40 -05:00
gitversion.yml ci: Update version, gitversion 2020-06-29 15:44:26 -04:00

Readme.md

Notice

This repository is now deprecated, as the support for ClientWebSocket is included in .NET 6 for WebAssembly.

Uno.Wasm.WebSockets

Uno.Wasm.WebSockets is a concrete implementation of the System.Net.WebSocket class for WebAssembly, named Uno.Wasm.WebSockets.WasmWebSocket.

This package requires the use of the Uno.Wasm.Bootstrap package to work properly.

Sample applications

Try WebSockets from .NET code live here: https://websockets-wasm.platform.uno

The sample applications use the Uno Platform and are built to use WebSockets in their respective platforms. The WebAssembly one uses Uno.Wasm.WebSockets.WasmWebSocket.

Open [Uno.Wasm.WebSockets.sln] to discover the sample, in the WasmWebSocketsSample.Shared project.

How to use the WasmWebSocket class

Given a project that already references Uno.Wasm.Bootstrap, add the Uno.Wasm.WebSockets nuget package, and write the following:

var ws = new Uno.Wasm.WebSockets.WasmWebSocket();

// Connect to a simple echo WebSocket server
await ws.ConnectAsync(new Uri("wss://echo.websocket.org"), CancellationToken.None);

Console.WriteLine("Program: Connected!");

// Send some data
var data = new ArraySegment<byte>(Encoding.UTF8.GetBytes("Hello websocket !"));
await ws.SendAsync(data, System.Net.WebSockets.WebSocketMessageType.Binary, false, CancellationToken.None);

Console.WriteLine("Program: Sent!");

// Read the echo back
var buffer = new byte[1024];
var received = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);

var receivedString = Encoding.UTF8.GetString(buffer, 0, received.Count);
Console.WriteLine($"Received {received.Count} bytes: {receivedString}");