https://github.com/bjorg/blazorwebsocket
This repository contains a simple application that that connects to the `wss://echo.websocket.org` to show how WebSocket connectivity works in Blazor WebAssembly.
https://github.com/bjorg/blazorwebsocket
Last synced: 4 months ago
JSON representation
This repository contains a simple application that that connects to the `wss://echo.websocket.org` to show how WebSocket connectivity works in Blazor WebAssembly.
- Host: GitHub
- URL: https://github.com/bjorg/blazorwebsocket
- Owner: bjorg
- Created: 2020-05-14T16:14:05.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-14T16:14:36.000Z (about 6 years ago)
- Last Synced: 2025-10-11T19:06:21.460Z (9 months ago)
- Language: HTML
- Size: 208 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: ReadMe.md
Awesome Lists containing this project
README
# Blazor WebAssembly with WebSocket
This repository contains a simple application that that connects to the `wss://echo.websocket.org` to show how WebSocket connectivity works in Blazor WebAssembly.
```cshtml
@page "/"
@using System.Net.WebSockets
@using System.Text
@using System.Threading
@implements IDisposable
Echo test
State: @webSocket.State
@if(webSocket.State == WebSocketState.Open) {
Message:
Send
@log
}
@code {
// Sample adapted from https://gist.github.com/SteveSandersonMS/5aaff6b010b0785075b0a08cc1e40e01
//--- Fields ---
CancellationTokenSource disposalTokenSource = new CancellationTokenSource();
ClientWebSocket webSocket = new ClientWebSocket();
string message = "Hello, websocket!";
string log = "";
//--- Methods ---
protected override async Task OnInitializedAsync() {
await webSocket.ConnectAsync(new Uri("wss://echo.websocket.org"), disposalTokenSource.Token);
_ = ReceiveLoop();
}
private Task SendMessageAsync() {
log += $"Sending: {message}\n";
var dataToSend = new ArraySegment(Encoding.UTF8.GetBytes(message));
return webSocket.SendAsync(dataToSend, WebSocketMessageType.Text, true, disposalTokenSource.Token);
}
private async Task ReceiveLoop() {
var buffer = new ArraySegment(new byte[1024]);
while(!disposalTokenSource.IsCancellationRequested) {
// Note that the received block might only be part of a larger message. If this applies in your scenario,
// check the received.EndOfMessage and consider buffering the blocks until that property is true.
// Or use a higher-level library such as SignalR.
var received = await webSocket.ReceiveAsync(buffer, disposalTokenSource.Token);
var receivedAsText = Encoding.UTF8.GetString(buffer.Array, 0, received.Count);
log += $"Received: {receivedAsText}\n";
StateHasChanged();
}
}
public void Dispose() {
disposalTokenSource.Cancel();
_ = webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Bye", CancellationToken.None);
}
}
```
## License
> Licensed under the Apache License, Version 2.0 (the "License");
> you may not use this file except in compliance with the License.
> You may obtain a copy of the License at
>
> http://www.apache.org/licenses/LICENSE-2.0
>
> Unless required by applicable law or agreed to in writing, software
> distributed under the License is distributed on an "AS IS" BASIS,
> WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> See the License for the specific language governing permissions and
> limitations under the License.