Comments (2)
It appears Blazor does not support websockets, see https://stackoverflow.com/questions/61906292/how-can-i-use-socket-in-blazor-client-side.
from pusher-websocket-dotnet.
Blazor wasm does support web sockets and has been support since blazor 3.2.0 wasm beta 1 (if I remember correctly).
I have a few different production projects using web sockets in blazor wasm to send and receive messages.
This is a old demo of them supporting websockets with a echo back service.
@page "/"
@using System.Net.WebSockets
@using System.Text
@using System.Threading
@implements IDisposable
<PageTitle>Index</PageTitle>
<h1>Echo test</h1>
<h3>State: @_webSocket.State</h3>
@if (_webSocket.State == WebSocketState.Open)
{
<form @onsubmit="@SendMessageAsync">
Message: <input @bind="@_message" />
<button type="submit">Send</button>
</form>
<pre>@_log</pre>
}
@code {
CancellationTokenSource _disposalTokenSource = new CancellationTokenSource();
ClientWebSocket _webSocket = new ClientWebSocket();
string _message = "Hello, websocket!";
string _log = "";
protected override async Task OnInitializedAsync()
{
await _webSocket.ConnectAsync(new Uri("wss://ws.postman-echo.com/raw"), _disposalTokenSource.Token);
_ = ReceiveLoop();
}
async Task SendMessageAsync()
{
_log += $"Sending: {_message}\n";
var dataToSend = new ArraySegment<byte>(Encoding.UTF8.GetBytes(_message));
await _webSocket.SendAsync(dataToSend, WebSocketMessageType.Text, true, _disposalTokenSource.Token);
}
async Task ReceiveLoop()
{
var buffer = new ArraySegment<byte>(new byte[1024]);
while (!_disposalTokenSource.IsCancellationRequested)
{
var received = await _webSocket.ReceiveAsync(buffer, _disposalTokenSource.Token);
if (buffer.Array != null)
{
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);
}
}
from pusher-websocket-dotnet.
Related Issues (20)
- precenceCannel.member does not exist HOT 2
- ConnectAsyn exception HOT 2
- How to bind to "pusher: subscription_succeeded" event? HOT 2
- Mixing events HOT 3
- Deadlock when unsibscribe from channels HOT 5
- A call to SSPI could not be made; see inner exception (.NET Framework 4.5 and 4.7.2) HOT 3
- Buggy Pusher Client 2.0.0 NuGet HOT 1
- Update from 1.2 to 2.1 changed JSON structure HOT 2
- SemaphoreFullException HOT 7
- RemoteCertificateChainErrors with PusherClient in Xamarin forms HOT 4
- NullReferenceException when disposing websocket HOT 4
- ArgumentNullException HOT 2
- PusherClient 2.1.0 doesn't decrypts messages HOT 1
- Error when subscribing to channels from ios platform app HOT 3
- Reporting a vulnerability HOT 1
- Unable to save excel file with pictures. HOT 1
- Changed host HOT 1
- Tls 1.2 support HOT 1
- pusher:ping not being sent HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pusher-websocket-dotnet.