public string removeUser(string userName) { try { _userList.Remove(userName); UserHandle.removeUser(userName); broadcast(Message.MessageHandle.genRoomNotifi(userName + "离开房间" + _roomName, Message.MessageTypeEnum.notification)); return("Success"); } catch (Exception e) { LogService.LogError(e.ToString()); return(e.ToString()); } }
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()); } } } }