122 lines
4.3 KiB
C#
122 lines
4.3 KiB
C#
|
|
using Church.Net.WebAPI.Models;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Newtonsoft.Json.Serialization;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using WebAPI.Models.IceBreak;
|
|
using System.Diagnostics;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace WebAPI.Hubs
|
|
{
|
|
public class GameRoomHub : Hub
|
|
{
|
|
private readonly GameRoomLogic gameRoomLogic;
|
|
|
|
public GameRoomHub(GameRoomLogic gameRoomLogic)
|
|
{
|
|
this.gameRoomLogic = gameRoomLogic;
|
|
}
|
|
|
|
public void SendMessage(SignalRMessage message)
|
|
{
|
|
string jsonString = JsonConvert.SerializeObject(message, new JsonSerializerSettings
|
|
{
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
|
//DateTimeZoneHandling = DateTimeZoneHandling.Local
|
|
});
|
|
Task.Run(() =>
|
|
{
|
|
if (message.Receiver.IsGroup)
|
|
{
|
|
Clients.Group(message.Receiver.SessionId).SendAsync("ReceivedMessage", jsonString);
|
|
}
|
|
else
|
|
{
|
|
Clients.Client(message.Receiver.SessionId).SendAsync("ReceivedMessage", jsonString);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
public override Task OnConnectedAsync()
|
|
{
|
|
var playerInfo = GetPlayerInfoFromRequest();
|
|
//var gameHostName = Context.GetHttpContext().Request.Query["gameHostName"];
|
|
//if (false==string.IsNullOrWhiteSpace(gameHostName))
|
|
//{
|
|
|
|
//}
|
|
//var groupId = Context.GetHttpContext().Request.Query["groupId"];
|
|
//Groups.AddToGroupAsync(Context.ConnectionId, groupId);
|
|
Debug.WriteLine($"{playerInfo.Name}({playerInfo.Id}) Conection Id:{playerInfo.SignalRClientId}");
|
|
|
|
if (!string.IsNullOrWhiteSpace(playerInfo.TabId))
|
|
{
|
|
if (gameRoomLogic.GetOnlinePlayerByTabId(playerInfo.TabId, out IGamePlayer gamePlayer))
|
|
{
|
|
gamePlayer.SignalRClientId = Context.ConnectionId;
|
|
}
|
|
else
|
|
{
|
|
gameRoomLogic.OnlinePlayers.Add(playerInfo);
|
|
}
|
|
}
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(playerInfo.GameRoomId))
|
|
{
|
|
Groups.AddToGroupAsync(Context.ConnectionId, playerInfo.GameRoomId);
|
|
}
|
|
|
|
|
|
this.SendMessage(new SignalRMessage(new SignalRSession(playerInfo.SignalRClientId, ""), "GameRoom", "sendJoinInfo").AddParameter("signalRConnId", Context.ConnectionId));
|
|
|
|
return base.OnConnectedAsync();
|
|
}
|
|
|
|
public override Task OnDisconnectedAsync(Exception exception)
|
|
{
|
|
|
|
var playerInfo = GetPlayerInfoFromRequest();
|
|
Debug.WriteLine($"{playerInfo.Name}({playerInfo.Id}) Conection Id:{playerInfo.SignalRClientId} Disconnected");
|
|
//var groupId = Context.GetHttpContext().Request.Query["groupId"];
|
|
//Groups.RemoveFromGroupAsync(Context.ConnectionId, groupId);
|
|
if (!string.IsNullOrWhiteSpace(playerInfo.GameRoomId))
|
|
{
|
|
Groups.RemoveFromGroupAsync(Context.ConnectionId, playerInfo.GameRoomId);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(playerInfo.TabId))
|
|
{
|
|
gameRoomLogic.UserLeave(playerInfo);
|
|
gameRoomLogic.OnlinePlayers.RemoveAll(u => u.TabId == playerInfo.TabId);
|
|
}
|
|
return base.OnDisconnectedAsync(exception);
|
|
}
|
|
private GamePlayer GetPlayerInfoFromRequest()
|
|
{
|
|
var playerId = Context.GetHttpContext().Request.Query["userId"];
|
|
var username = Context.GetHttpContext().Request.Query["username"];
|
|
var playerTabId = Context.GetHttpContext().Request.Query["tabId"];
|
|
var roomId = Context.GetHttpContext().Request.Query["roomId"];
|
|
return new GamePlayer()
|
|
{
|
|
Id = playerId,
|
|
SignalRClientId = Context.ConnectionId,
|
|
Name = username,
|
|
TabId = playerTabId,
|
|
GameRoomId = roomId,
|
|
};
|
|
}
|
|
}
|
|
}
|