using Microsoft.AspNetCore.SignalR; namespace Back.Program.Common.Chat; public class ChatHub : Hub { // 클라이언트에서 메시지를 보내면 모든 사용자에게 전송 public async Task SendMessage(string user, string message) { Console.WriteLine($"Message received: {user}: {message}"); await Clients.All.SendAsync("ReceiveMessage", user, message); } // 특정 사용자에게 메시지를 보냄 public async Task SendMessageToUser(string connectionId, string message) { await Clients.Client(connectionId).SendAsync("ReceiveMessage", message); } // 클라이언트가 연결될 때 호출 public override async Task OnConnectedAsync() { await Clients.Caller.SendAsync("ReceiveMessage", "System", $"Welcome! Your ID: {Context.ConnectionId}"); Console.WriteLine("OnConnectedAsync"); await base.OnConnectedAsync(); } // 클라이언트가 연결 해제될 때 호출 public override async Task OnDisconnectedAsync(Exception? exception) { await Clients.All.SendAsync("ReceiveMessage", "System", $"{Context.ConnectionId} disconnected"); Console.WriteLine("OnDisconnectedAsync"); await base.OnDisconnectedAsync(exception); } public async Task JoinRoom(string cid) { await Groups.AddToGroupAsync(Context.ConnectionId, cid); } public async Task JoinGroup(string cid, string groupName) { await Groups.AddToGroupAsync(Context.ConnectionId, groupName); } }