public static void addUser(string userName, User user) { _userList.Add(userName, user); }
public bool Contains(User user) { return this.group.ContainsKey(user.UserName); }
private void startListen() { isListen = true; Socket clientSocket = default(Socket); while (isListen) { try { clientSocket = serverSocket.Accept(); } catch (SocketException e) { Debug.WriteLine(e.ToString()); LogService.LogError(e.ToString()); } Byte[] bytesFromClient = new Byte[4096]; if (clientSocket != null && clientSocket.Connected) { try { int len = clientSocket.Receive(bytesFromClient); byte[] receivedBytes = new byte[len]; Array.Copy(bytesFromClient, receivedBytes, len); if (len > -1) { Message.Message msg = Message.MessageHandle.deserialize(receivedBytes); //如果是新用户 if (msg.messageType == Message.MessageTypeEnum.signup) { if (!UserHandle.isUserExist(msg.userName)) { //注册一个房间 ClientRoom clientRoom = null; if (roomHandle.isRoomExist(msg.room)) { clientRoom = roomHandle.getRoom(msg.room); } else { clientRoom = roomHandle.addRoom(msg.room); } //为刚创建的房间添加当前用户 clientRoom.addUser(msg.userName, clientSocket); User newUser = new User(msg.userName, clientSocket, clientRoom); UserHandle.addUser(msg.userName, newUser); newUser.startListen(); } else { clientSocket.Send(Message.MessageHandle.serialize(Message.MessageHandle.genRoomNotifi("用户名已经存在,请换一个!", Message.MessageTypeEnum.warn))); LogService.Log(Message.MessageHandle.genRoomNotifi("用户名" + msg.userName + "已经存在,请换一个!", Message.MessageTypeEnum.warn)); //? } } } } catch (Exception e) { Debug.WriteLine(e.ToString()); LogService.LogError(e.ToString()); } } } }
public void sendSingleMessage(User user,ByteBuffer buffer) { user.Send(buffer); }