Church.Net.API/WebAPI/Hubs/AvalonHub.cs
2022-09-08 08:04:32 -07:00

63 lines
1.8 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace WebAPI.Hubs
{
public enum AvalonStage
{
JoinGame,
NewGame,
PickTeammate,
TeamVote,
TeamVoteResult,
QuestVote,
QuestVoteResult
}
public class AvalonHub : Hub
{
private readonly GameRoomLogic gameRoomLogic;
public AvalonHub(GameRoomLogic gameRoomLogic)
{
this.gameRoomLogic = gameRoomLogic;
}
public async Task JoinGame(string connectionId, string username)
{
await Clients.All.SendAsync("JoinGame", connectionId, username);
}
public async Task RefreshStage(AvalonStage stage)
{
await Clients.All.SendAsync("RefreshStage", stage);
}
public async Task Broadcast(string connectionId,string action, string jsonString)
{
//await Clients.AllExcept(connectionId).SendAsync("Broadcast", action, jsonString);
await Clients.Others.SendAsync("Broadcast", action, jsonString);
}
public override Task OnConnectedAsync()
{
var username = Context.GetHttpContext().Request.Query["username"];
var userId = Context.GetHttpContext().Request.Query["userId"];
return base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception exception)
{
var userId = Context.GetHttpContext().Request.Query["userId"];
Clients.All.SendAsync("Broadcast", "LeaveGame", Context.ConnectionId);
return base.OnDisconnectedAsync(exception);
}
}
}