Skip to content
An Uno Platform implementation of .NET System.Net.WebSocket for WebAssembly
Branch: master
Clone or download
jeromelaban Merge pull request #9 from unoplatform/dev/jela/fix-invalidptr
Workaround missing IntPtr conversion for binary transfers
Latest commit f3d78e7 Jul 18, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
build Initial sources Dec 17, 2018
src
.gitattributes Initial sources Dec 17, 2018
.gitignore Initial sources Dec 17, 2018
.vsts-ci.yml Remove CI workaround for forks Jun 7, 2019
License.md
Readme.md
gitversion.yml Initial sources Dec 17, 2018

Readme.md

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 samples 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}");
You can’t perform that action at this time.