Squirrowse/Squirrowse.Web/Pages/Hub.razor
2019-11-16 13:07:20 +01:00

137 lines
4.3 KiB
Plaintext

@page "/hub"
<h3>Hub</h3>
@using System.Collections
@using Microsoft.AspNetCore.SignalR.Client
@using Squirrowse.Core.Models
@using Squirrowse.Core.Services
@inject IConnectionManager _connection;
@*<button id="LoadData"class="btn btn-success btn-sm" @onclick="@(() => loadData())">dupa123 </button>*@
<div class="card border-primary mb-3" style="max-width: 20rem;">
@if (agents != null && agents.Any())
{
@foreach (var agent in agents)
{
<div class="card-body">
<div>
<h3 class="badge-primary">
@agent.AgentName -> @agent.UserType.ToString()
</h3>
<div style="padding-top:10px">
<button id="ViewCast" disabled="@(IsViewingCastOf(agent.AgentName))" class="btn btn-success btn-sm" @onclick="@(() => OnViewCastClicked(agent.ConnectionId))">
View cast
</button>
<button id="StopViewCast" disabled="@(IsViewingCastOf(agent.AgentName))" class="btn btn-warning btn-sm" @onclick="@(() => OnStopViewCastClicked(agent.ConnectionId))">
Stop cast
</button>
<button id="StartRecording" disabled="@(IsViewingCastOf(agent.AgentName))" class="btn btn-warning btn-sm" @onclick="@(() =>StartRecording(agent.ConnectionId))">
Stop cast
</button>
<button id="StartRecording" disabled="@(IsViewingCastOf(agent.AgentName))" class="btn btn-warning btn-sm" @onclick="@(() =>StopRecording(agent.ConnectionId))">
Stop cast
</button>
</div>
</div>
</div>
}
}
else
{
<div class="card-body">
<h3 class="card-header badge-warning">No Cams</h3>
</div>
}
</div>
<div class="border">
<img id='screenImage' src="@imageSource" />
</div>
@code{
private List<User> agents=new List<User>();
HubConnection connection;
string imageSource = null;
string CurrentViewCastAgent = null;
protected override async Task OnInitializedAsync()
{
await _connection.InitConnection(ConnectionType.Server);
connection = await _connection.GetConnection();
connection.On<byte[]>("RecData", OnStreamDataReceived);
await foreach (var user in connection.StreamAsync<User>("GetListOfTypeUserAsync", ConnectionType.Client).ConfigureAwait(false))
{
agents.Add(user);
this.StateHasChanged();
}
}
bool IsViewingCastOf(string agentName)
{
return agentName == CurrentViewCastAgent;
}
async Task StartRecording(string agent)
{
await connection.SendAsync("StartRecord");
StateHasChanged();
}
async Task StopRecording(string agent)
{
await connection.SendAsync("StopRecord");
StateHasChanged();
}
async Task OnStreamDataReceived(byte[] streamData)
{
//await foreach (var t in streamData)
//{
// var base64 = Convert.ToBase64String(t);
// imageSource = String.Format("data:image/jpg;base64,{0}", base64);
// StateHasChanged();
//}
var base64 = Convert.ToBase64String(streamData);
imageSource = String.Format("data:image/jpg;base64,{0}", base64);
StateHasChanged();
}
async Task OnStreamDataReceivedEn(byte[] streamData)
{
//await foreach (var t in streamData)
//{
// var base64 = Convert.ToBase64String(t);
// imageSource = String.Format("data:image/jpg;base64,{0}", base64);
// StateHasChanged();
//}
var base64 = Convert.ToBase64String(streamData);
imageSource = String.Format("data:image/jpg;base64,{0}", base64);
StateHasChanged();
}
private async Task OnViewCastClicked(string agentName)
{
CurrentViewCastAgent = agentName;
await connection.InvokeAsync("Startstream", agentName);
StateHasChanged();
}
private async Task OnStopViewCastClicked(string agentName)
{
CurrentViewCastAgent = null;
await connection.InvokeAsync("StopStream", agentName);
imageSource = null;
StateHasChanged();
}
}