-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
1,489 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...integration/Websocket.Client.Sample.NetFramework/Websocket.Client.Sample.Blazor/App.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Router AppAssembly="@typeof(App).Assembly"> | ||
<Found Context="routeData"> | ||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /> | ||
<FocusOnNavigate RouteData="@routeData" Selector="h1" /> | ||
</Found> | ||
<NotFound> | ||
<PageTitle>Not found</PageTitle> | ||
<LayoutView Layout="@typeof(MainLayout)"> | ||
<p role="alert">Sorry, there's nothing at this address.</p> | ||
</LayoutView> | ||
</NotFound> | ||
</Router> |
18 changes: 18 additions & 0 deletions
18
...n/Websocket.Client.Sample.NetFramework/Websocket.Client.Sample.Blazor/Pages/Counter.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
@page "/counter" | ||
|
||
<PageTitle>Counter</PageTitle> | ||
|
||
<h1>Counter</h1> | ||
|
||
<p role="status">Current count: @currentCount</p> | ||
|
||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button> | ||
|
||
@code { | ||
private int currentCount = 0; | ||
|
||
private void IncrementCount() | ||
{ | ||
currentCount++; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...Websocket.Client.Sample.NetFramework/Websocket.Client.Sample.Blazor/Pages/FetchData.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
@page "/fetchdata" | ||
@inject HttpClient Http | ||
|
||
<PageTitle>Weather forecast</PageTitle> | ||
|
||
<h1>Weather forecast</h1> | ||
|
||
<p>This component demonstrates fetching data from the server.</p> | ||
|
||
@if (forecasts == null) | ||
{ | ||
<p><em>Loading...</em></p> | ||
} | ||
else | ||
{ | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>Date</th> | ||
<th>Temp. (C)</th> | ||
<th>Temp. (F)</th> | ||
<th>Summary</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
@foreach (var forecast in forecasts) | ||
{ | ||
<tr> | ||
<td>@forecast.Date.ToShortDateString()</td> | ||
<td>@forecast.TemperatureC</td> | ||
<td>@forecast.TemperatureF</td> | ||
<td>@forecast.Summary</td> | ||
</tr> | ||
} | ||
</tbody> | ||
</table> | ||
} | ||
|
||
@code { | ||
private WeatherForecast[]? forecasts; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json"); | ||
} | ||
|
||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public string? Summary { get; set; } | ||
|
||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ion/Websocket.Client.Sample.NetFramework/Websocket.Client.Sample.Blazor/Pages/Index.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@page "/" | ||
|
||
<PageTitle>Index</PageTitle> | ||
|
||
<h1>Hello</h1> | ||
|
||
Welcome to multi platform Websocket client. | ||
|
||
<p> | ||
<a href="websocket">Example</a> | ||
</p> |
115 changes: 115 additions & 0 deletions
115
...ocket.Client.Sample.NetFramework/Websocket.Client.Sample.Blazor/Pages/WebsocketPage.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
@page "/websocket" | ||
@using System.Net.WebSockets | ||
@implements IAsyncDisposable; | ||
|
||
<h1>Websocket Example</h1> | ||
|
||
<h3>Connecting to Bitmex server</h3> | ||
<small>Running: @_client?.IsRunning</small> | ||
|
||
@if (_client?.IsRunning == true) | ||
{ | ||
<div class="sticky-top w-50 mb-3 mt-3"> | ||
<form @onsubmit="@SendMessage"> | ||
|
||
<div class="input-group"> | ||
<div class="input-group-prepend"> | ||
<span class="input-group-text">Message</span> | ||
</div> | ||
<input @bind="@_message" type="text" class="form-control"> | ||
<div class="input-group-append"> | ||
<button type="submit" class="btn btn-outline-secondary">Send</button> | ||
</div> | ||
</div> | ||
</form> | ||
</div> | ||
} | ||
|
||
<pre> | ||
@foreach (var log in GetLogs()) | ||
{ | ||
var timestamp = log.Timestamp.ToLocalTime().ToString("HH:mm:ss.fff"); | ||
switch (log.Severity) | ||
{ | ||
case LogSeverity.Warning: | ||
<div class="text-warning">@timestamp @log.Message</div> | ||
break; | ||
case LogSeverity.Error: | ||
<div class="text-danger">@timestamp @log.Message</div> | ||
break; | ||
default: | ||
<div class="text-secondary">@timestamp @log.Message</div> | ||
break; | ||
} | ||
} | ||
</pre> | ||
|
||
@code { | ||
private readonly List<LogMessage> _logs = new(); | ||
private IWebsocketClient? _client; | ||
private string _message = "ping"; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
var url = new Uri("wss://www.bitmex.com/realtime"); | ||
_client = new WebsocketClient(url); | ||
|
||
_client.Name = "Bitmex"; | ||
_client.ReconnectTimeout = TimeSpan.FromSeconds(120); | ||
_client.ErrorReconnectTimeout = TimeSpan.FromSeconds(30); | ||
_client.ReconnectionHappened.Subscribe(info => | ||
{ | ||
Console.WriteLine($"Reconnection happened, type: {info.Type}, url: {_client.Url}"); | ||
Log($"Reconnected, type: '{info.Type}'", LogSeverity.Warning); | ||
StateHasChanged(); | ||
}); | ||
_client.DisconnectionHappened.Subscribe(info => | ||
{ | ||
Console.WriteLine($"Disconnection happened, type: {info.Type}"); | ||
Log($"Disconnected, type: '{info.Type}', reason: '{info.CloseStatus}'", LogSeverity.Warning); | ||
StateHasChanged(); | ||
}); | ||
|
||
_client.MessageReceived.Subscribe(msg => | ||
{ | ||
Console.WriteLine($"Message received: {msg}"); | ||
Log($"Received: '{msg.Text}'", LogSeverity.Info); | ||
StateHasChanged(); | ||
}); | ||
|
||
Console.WriteLine("Starting..."); | ||
await _client.Start(); | ||
Console.WriteLine("Started."); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
if (_client == null) | ||
return; | ||
|
||
await _client.Stop(WebSocketCloseStatus.NormalClosure, string.Empty); | ||
_client.Dispose(); | ||
} | ||
|
||
private void SendMessage() | ||
{ | ||
Log($"Sending: '{_message}'", LogSeverity.Info); | ||
_client?.Send(_message); | ||
} | ||
|
||
private void Log(string message, LogSeverity severity) | ||
{ | ||
_logs.Add(new LogMessage(message, severity, DateTime.UtcNow)); | ||
} | ||
|
||
private LogMessage[] GetLogs() => _logs.ToArray().Reverse().ToArray(); | ||
|
||
private record LogMessage(string Message, LogSeverity Severity, DateTime Timestamp); | ||
|
||
private enum LogSeverity | ||
{ | ||
Info, | ||
Warning, | ||
Error | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ntegration/Websocket.Client.Sample.NetFramework/Websocket.Client.Sample.Blazor/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Microsoft.AspNetCore.Components.Web; | ||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | ||
using Websocket.Client.Sample.Blazor; | ||
|
||
var builder = WebAssemblyHostBuilder.CreateDefault(args); | ||
builder.RootComponents.Add<App>("#app"); | ||
builder.RootComponents.Add<HeadOutlet>("head::after"); | ||
|
||
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); | ||
|
||
await builder.Build().RunAsync(); |
30 changes: 30 additions & 0 deletions
30
....Client.Sample.NetFramework/Websocket.Client.Sample.Blazor/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:9850", | ||
"sslPort": 44313 | ||
} | ||
}, | ||
"profiles": { | ||
"Websocket.Client.Sample.Blazor": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", | ||
"applicationUrl": "https://localhost:7041;http://localhost:5041", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...bsocket.Client.Sample.NetFramework/Websocket.Client.Sample.Blazor/Shared/MainLayout.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@inherits LayoutComponentBase | ||
|
||
<div class="page"> | ||
<div class="sidebar"> | ||
<NavMenu /> | ||
</div> | ||
|
||
<main> | ||
<div class="top-row px-4"> | ||
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a> | ||
</div> | ||
|
||
<article class="content px-4"> | ||
@Body | ||
</article> | ||
</main> | ||
</div> |
Oops, something went wrong.