1 user.go

  1. // 用户处理消息的业务
  2. func (this *User) DoMessage(msg string) {
  3. if msg == "who" {
  4. // 查询当前在线用户
  5. this.server.mapLock.Lock()
  6. for userName, _ := range this.server.OnlineMap {
  7. this.SendMsg(userName + "在线\n")
  8. }
  9. this.server.mapLock.Unlock()
  10. } else if len(msg) > 7 && msg[:7] == "rename|" {
  11. // 更改自己的用户名, 消息格式: rename|张三
  12. newName := strings.Split(msg, "|")[1]
  13. // 判断name是否存在
  14. _, err := this.server.OnlineMap[newName]
  15. if err {
  16. this.SendMsg("失败, 该用户名已被其它人使用\n")
  17. } else {
  18. this.server.mapLock.Lock()
  19. delete(this.server.OnlineMap, this.Name)
  20. this.server.OnlineMap[newName] = this
  21. this.server.mapLock.Unlock()
  22. this.Name = newName
  23. this.SendMsg("您已成功更新为用户名:" + this.Name + "\n")
  24. }
  25. } else {
  26. this.server.BroadCast(this, msg)
  27. }
  28. }

image.png