//添加一个房间 public ClientRoom addRoom(string roomName) { if (_roomList.ContainsKey(roomName)) { return((ClientRoom)_roomList[roomName]); } else { ClientRoom clientRoom = new ClientRoom(roomName); _roomList.Add(roomName, clientRoom); return(clientRoom); } }
//判断用户在哪个房间 public ClientRoom whichRoom(string userName) { ClientRoom clientRoom = new ClientRoom(); foreach (DictionaryEntry room in _roomList) { clientRoom = (ClientRoom)room.Key; if (clientRoom.isThisRoom(userName)) { return(clientRoom); } } Debug.WriteLine("出错,没有找到用户在的组"); return(clientRoom); }
public User(string userName, Socket userSocket, ClientRoom clientRoom) { _userName = userName; _userSocket = userSocket; _clientRoom = clientRoom; }
//判断用户在哪个房间 public ClientRoom whichRoom(string userName) { ClientRoom clientRoom = new ClientRoom(); foreach (DictionaryEntry room in _roomList) { clientRoom = (ClientRoom)room.Key; if (clientRoom.isThisRoom(userName)) { return clientRoom; } } Debug.WriteLine("出错,没有找到用户在的组"); return clientRoom; }
//添加一个房间 public ClientRoom addRoom(string roomName) { if (_roomList.ContainsKey(roomName)) { return (ClientRoom)_roomList[roomName]; } else { ClientRoom clientRoom = new ClientRoom(roomName); _roomList.Add(roomName, clientRoom); return clientRoom; } }
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()); } } } }