async Task OnRoomJoin(JoinRoom msg, IOperationContext context) { var newMember = new RoomMember { RoomName = msg.RoomName, UserName = msg.ClientName, UserChannel = context.ReplyChannel }; var newEvent = new RoomEvent() { UserName = msg.ClientName, Text = "Joined" }; RoomData room; if (!Rooms.TryGetValue(msg.RoomName, out room)) { room = new RoomData(msg.RoomName); room.Members = new List<RoomMember> { newMember }; Rooms.Add(msg.RoomName, room); } else { if (room.Members.Any(x => x.UserName == msg.ClientName)) { context.ReplyChannel.Reply(context, new JoinRoomResponse() { RetCode = JoinRoomRetCode.NameIsTaken }); return; } else { room.Members.ForEach(m => m.UserChannel.Send(newEvent)); room.Members.Add(newMember); } } RoomMembers.Add(context.ReplyChannel, newMember); room.History.Add(newEvent); using (var tx = StateManager.CreateTransaction()) { await _roomsState.SetAsync(tx, room.Name, room); await tx.CommitAsync(); } context.ReplyChannel.Reply(context, new JoinRoomResponse() { RetCode = JoinRoomRetCode.Ok }); foreach (var roomEvent in room.History) { context.ReplyChannel.Send(roomEvent); } }
static void OnRoomJoin(JoinRoom msg, IOperationContext context) { var newMember = new RoomMember { RoomName = msg.RoomName, UserName = msg.ClientName, UserChannel = context.ReplyChannel }; List<RoomMember> members; if (!Rooms.TryGetValue(msg.RoomName, out members)) { members = new List<RoomMember> { newMember }; Rooms.Add(msg.RoomName, members); } else { if (members.Any(x => x.UserName == msg.ClientName)) { context.ReplyChannel.Reply(context, new JoinRoomResponse(){RetCode = JoinRoomRetCode.NameIsTaken}); return; } else { members.ForEach((m) => m.UserChannel.Send(new RoomEvent(){UserName = msg.ClientName, Text = "Joined"})); members.Add(newMember); } } RoomMembers.Add(context.ReplyChannel, newMember); context.ReplyChannel.Reply(context, new JoinRoomResponse() { RetCode = JoinRoomRetCode.Ok }); }