public async Task Join(string userName, string roomName) { var user = RTCUser.Get(userName, Context.ConnectionId); var room = Room.Get(roomName); if (user.CurrentRoom != null) { room.Users.Remove(user); await SendUserListUpdate(Clients.Others, room, false); } user.CurrentRoom = room; room.Users.Add(user); await SendUserListUpdate(Clients.Caller, room, true); await SendUserListUpdate(Clients.Others, room, false); lock (RoomsThatAreActive) { if (!RoomsThatAreActive.Any(x => x.Name == room.Name)) { RoomsThatAreActive.Add(room); } } lock (RoomsThatAreFull) { if (room.Users.Count() == 2) { RoomsThatAreFull.Add(room); } } }
// WebRTC Signal Handler public async Task SendSignal(string signal, string targetConnectionId) { var callingUser = RTCUser.Get(Context.ConnectionId); var targetUser = RTCUser.Get(targetConnectionId); // Make sure both users are valid if (callingUser == null || targetUser == null) { return; } // These folks are in a call together, let's let em talk WebRTC await Clients.Client(targetConnectionId).SendAsync("receiveSignal", callingUser, signal); }
public async Task HangUp() { var callingUser = RTCUser.Get(Context.ConnectionId); if (callingUser == null) { return; } if (callingUser.CurrentRoom != null) { callingUser.CurrentRoom.Users.Remove(callingUser); await SendUserListUpdate(Clients.Others, callingUser.CurrentRoom, false); } lock (RoomsThatAreActive) { if (RoomsThatAreActive.Count() > 0 && callingUser.CurrentRoom != null) { var toRemove = RoomsThatAreActive.Where(m => m.Name == callingUser.CurrentRoom.Name).Select(m => m.Users).FirstOrDefault(); if (toRemove != null) { toRemove.Remove(callingUser); } } lock (RoomsThatAreFull) { if (callingUser.CurrentRoom != null && callingUser.CurrentRoom.Users.Count() == 0) { RoomsThatAreFull.Remove(callingUser.CurrentRoom); RoomsThatAreActive.Remove(callingUser.CurrentRoom); Room.Remove(callingUser.CurrentRoom); } } } RTCUser.Remove(callingUser); }
public async Task UserDisconnected(string userName) { var user = RTCUser.Get(userName, Context.ConnectionId); await Clients.All.SendAsync("UserDisconnected", user.UserName); }