Squirrowse/Squirrowse.Client/Service/ConnectionManager.cs

41 lines
1.1 KiB
C#
Raw Normal View History

2019-11-05 17:00:36 +01:00
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
namespace Squirrowse.Client.Service
{
public class ConnectionManager : IConnectionManager
{
private readonly HubConnection _connection;
2019-11-05 17:00:36 +01:00
private bool Connected;
public ConnectionManager(string url, int port)
{
_connection = new HubConnectionBuilder()
.WithUrl($"{url}:{port}/StreamHub")
.AddMessagePackProtocol()
.WithAutomaticReconnect()
.Build();
2019-11-05 17:00:36 +01:00
}
2019-11-05 17:00:36 +01:00
public async Task<HubConnection> GetConnection()
2019-11-04 09:29:37 +01:00
{
2019-11-05 17:00:36 +01:00
if (Connected)
{
return _connection;
}
await _connection.StartAsync();
await RegisterOnHub();
Connected = true;
2019-11-04 09:29:37 +01:00
return _connection;
}
2019-11-05 17:00:36 +01:00
private async Task RegisterOnHub()
{
await _connection.SendAsync("AddUser", Environment.UserName);
}
}
}