58 lines
1.4 KiB
C#
58 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
|
|
namespace squirrowse.client
|
|
{
|
|
public class ConnectionManager : IConnectionManager
|
|
{
|
|
private readonly HubConnection _connection;
|
|
public bool Connected;
|
|
|
|
public ConnectionManager(string url, int port)
|
|
{
|
|
_connection = new HubConnectionBuilder()
|
|
.WithUrl($"{url}:{port}/hub")
|
|
.WithAutomaticReconnect()
|
|
.Build();
|
|
}
|
|
|
|
public async Task<HubConnection> GetConnection()
|
|
{
|
|
if (Connected) return _connection;
|
|
|
|
throw new Exception();
|
|
}
|
|
|
|
public async Task InitConnection()
|
|
{
|
|
if (_connection.State == HubConnectionState.Connected) return;
|
|
await _connection.StartAsync();
|
|
Connected = true;
|
|
}
|
|
|
|
public async Task Disconnect()
|
|
{
|
|
if (_connection.State == HubConnectionState.Disconnected) throw new Exception();
|
|
await _connection.StopAsync();
|
|
Connected = false;
|
|
}
|
|
|
|
public bool IsConnected()
|
|
{
|
|
return Connected;
|
|
}
|
|
}
|
|
|
|
public interface IConnectionManager
|
|
{
|
|
Task<HubConnection> GetConnection();
|
|
Task InitConnection();
|
|
Task Disconnect();
|
|
|
|
bool IsConnected();
|
|
}
|
|
}
|