Squirrowse/Squirrowse.Service/Hubs/StreamHub.cs

107 lines
3.7 KiB
C#
Raw Normal View History

2019-11-06 19:17:06 +01:00
using System;
using System.Collections.Generic;
2019-11-07 14:26:46 +01:00
using System.Linq;
2019-10-31 12:53:57 +01:00
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using Squirrowse.Core.Models;
2019-10-31 12:53:57 +01:00
namespace Squirrowse.Service.Hubs
{
public class StreamHub : Hub, IStreamHub //fujka
2019-10-31 12:53:57 +01:00
{
private readonly ILogger<StreamHub> logger;
2019-11-05 17:00:36 +01:00
private readonly IStreamManager manager;
2019-11-06 13:47:00 +01:00
2019-11-05 17:00:36 +01:00
public StreamHub(ILogger<StreamHub> logger, IStreamManager manager)
2019-10-31 12:53:57 +01:00
{
this.logger = logger;
2019-11-05 17:00:36 +01:00
this.manager = manager;
2019-10-31 12:53:57 +01:00
}
2019-11-06 17:02:06 +01:00
public async Task AddUser(string UserName, ConnectionType type)
{
2019-11-07 11:52:27 +01:00
await manager.AddUser(Context.ConnectionId, UserName, type);
2019-11-07 14:26:46 +01:00
switch (type)
{
case ConnectionType.Client:
await AddToGroup(Core.Models.Groups.normal);
break;
case ConnectionType.Server:
await AddToGroup(Core.Models.Groups.superUser);
break;
}
2019-11-06 19:17:06 +01:00
logger.LogInformation($"{nameof(AddUser)}: {UserName} of {type}");
}
2019-11-06 19:17:06 +01:00
public async Task AddToGroup(Groups group, string user = "")
{
2019-11-04 09:29:37 +01:00
var connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user;
await Groups.AddToGroupAsync(connectionId, group.ToString());
logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}");
}
2019-11-04 09:29:37 +01:00
/// <summary>
2019-11-04 09:29:37 +01:00
/// USE ONLY FOR DEBUG
/// </summary>
/// <param name="act"></param>
/// <returns>DESTRUCTION</returns>
2019-11-04 09:29:37 +01:00
public async Task ExecCommandOnAll(string command, object[] act)
{
2019-11-04 09:29:37 +01:00
await Clients.All.SendCoreAsync(command, act);
}
public async Task RemoveFromGroup(Groups group, string user = "")
{
2019-11-04 09:29:37 +01:00
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)
{
2019-11-07 14:26:46 +01:00
//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)
{
var client = Clients.Client(clientId);
await client.SendAsync("Stop");
}
2019-11-06 13:47:00 +01:00
2019-10-31 12:53:57 +01:00
public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream)
{
2019-11-07 14:26:46 +01:00
foreach (var user in manager.getServerSideUsers().ToList())
2019-11-06 19:17:06 +01:00
await Clients.Client(user.ConnectionId).SendAsync("RecData", stream);
2019-11-07 11:52:27 +01:00
await foreach (var frame in stream)
2019-11-06 17:02:06 +01:00
{
logger.LogInformation($"Got frame size: {frame.Length} ");
2019-11-07 11:52:27 +01:00
await Task.Delay(100); //leave some delay for debug purpose
2019-10-31 12:53:57 +01:00
}
}
2019-11-06 19:17:06 +01:00
public async Task<IEnumerable<User>> GetListOfTypeUser(ConnectionType t)
{
if (t == ConnectionType.Client) return manager.getClientSideUsers();
if (t == ConnectionType.Server) return manager.getServerSideUsers();
2019-11-06 22:34:02 +01:00
throw new Exception("not found");
}
2019-11-07 11:52:27 +01:00
2019-11-06 19:17:06 +01:00
public async Task<IEnumerable<User>> GetAllUsers()
{
return manager.getAllUsers();
}
2019-11-07 11:52:27 +01:00
public async IAsyncEnumerable<User> GetListOfTypeUserAsync(ConnectionType t)
{
var client = await GetListOfTypeUser(t);
foreach (var va in client) yield return va;
}
2019-10-31 12:53:57 +01:00
}
2019-11-04 09:29:37 +01:00
}