112 lines
3.9 KiB
C#
112 lines
3.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.Extensions.Logging;
|
|
using Squirrowse.Core.Models;
|
|
|
|
namespace Squirrowse.Service.Hubs
|
|
{
|
|
public class StreamHub : Hub, IStreamHub //fujka
|
|
{
|
|
private readonly ILogger<StreamHub> logger;
|
|
private readonly IStreamManager manager;
|
|
|
|
|
|
public StreamHub(ILogger<StreamHub> logger, IStreamManager manager)
|
|
{
|
|
this.logger = logger;
|
|
this.manager = manager;
|
|
}
|
|
|
|
public async Task AddUser(string UserName, ConnectionType type)
|
|
{
|
|
await manager.AddUser(Context.ConnectionId, UserName, type);
|
|
switch (type)
|
|
{
|
|
case ConnectionType.Client:
|
|
await AddToGroup(Core.Models.Groups.normal);
|
|
break;
|
|
case ConnectionType.Server:
|
|
await AddToGroup(Core.Models.Groups.superUser);
|
|
break;
|
|
}
|
|
|
|
logger.LogInformation($"{nameof(AddUser)}: {UserName} of {type}");
|
|
}
|
|
|
|
|
|
public async Task AddToGroup(Groups group, string user = "")
|
|
{
|
|
var connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user;
|
|
await Groups.AddToGroupAsync(connectionId, group.ToString());
|
|
logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// USE ONLY FOR DEBUG
|
|
/// </summary>
|
|
/// <param name="act"></param>
|
|
/// <returns>DESTRUCTION</returns>
|
|
public async Task ExecCommandOnAll(string command, object[] act)
|
|
{
|
|
await Clients.All.SendCoreAsync(command, act);
|
|
}
|
|
|
|
public async Task RemoveFromGroup(Groups group, string user = "")
|
|
{
|
|
var connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user;
|
|
await Groups.RemoveFromGroupAsync(connectionId, group.ToString());
|
|
logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}");
|
|
}
|
|
|
|
public async Task Startstream(string clientId)
|
|
{
|
|
manager.StreamOn() = true;
|
|
//var client = Clients.Client(clientId);
|
|
await Clients.Groups(Core.Models.Groups.normal.ToString()).SendAsync("Start");
|
|
// await client.SendAsync("Start");
|
|
}
|
|
|
|
|
|
public async Task StopStream(string clientId)
|
|
{
|
|
streamOn = false;
|
|
//var client = Clients.Client(clientId);
|
|
// await Clients.Groups(Core.Models.Groups.normal.ToString()).SendAsync("Stop");
|
|
//await client.SendAsync("Stop");
|
|
}
|
|
|
|
public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream)
|
|
{
|
|
await foreach (var frame in stream)
|
|
{
|
|
logger.LogInformation($"Got frame size: {frame.Length} ");
|
|
if (!streamOn) continue;
|
|
logger.LogInformation($"Send frame of size: {frame.Length} to {Core.Models.Groups.superUser.ToString()}");
|
|
await Clients.Groups(Core.Models.Groups.superUser.ToString()).SendAsync("RecData", frame);
|
|
|
|
//await Task.Delay(100); //leave some delay for debug purpose
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<User>> GetListOfTypeUser(ConnectionType t)
|
|
{
|
|
if (t == ConnectionType.Client) return manager.getClientSideUsers();
|
|
if (t == ConnectionType.Server) return manager.getServerSideUsers();
|
|
return manager.getAllUsers();
|
|
}
|
|
|
|
public async Task<IEnumerable<User>> GetAllUsers()
|
|
{
|
|
return manager.getAllUsers();
|
|
}
|
|
|
|
public async IAsyncEnumerable<User> GetListOfTypeUserAsync(ConnectionType t)
|
|
{
|
|
var client = await GetListOfTypeUser(t);
|
|
foreach (var va in client) yield return va;
|
|
}
|
|
}
|
|
} |