func (user *User) read() {
	defer func() {
		user.connect.Close()

	}()
	for {
		//user.connect.SetReadDeadline(time.Now().Add(time.Second * 30))
		mtype, message, err := user.connect.ReadMessage()
		if err != nil {
			log.Println("Can't receive a message because of:", err)
			break
		}
		log.Println("For user "+user.id+", message received:", string(message))
		if mtype == websocket.TextMessage {
			text := string(message)
			if utils.StringStartWith(text, "READY:") {
				id, name := utils.GetIdAndName(text)
				if user.id == "" || user.name == "" {
					user.id = id
					user.name = name
					Save(user.id, user)
				}
			}
			if utils.StringStartWith(text, "GETPERSON:") {
				//id := utils.GetId(text)
			}
		}
		Hub.broadcast <- message
	}
}
Beispiel #2
0
func processMessage(message string) {
	if utils.StringStartWith(message, "READY:") {
		id, _ := utils.GetIdAndName(message)
		user, exist := Get(id)
		if exist {
			user.messageBox <- []byte("ACCEPT:" + id + ":READY")
		}
	}
	if utils.StringStartWith(message, "GETPERSON:") {
		id := utils.GetId(message)
		user, exist := Get(id)
		if exist {
			user2, exist := Get(user.counterparty)
			if exist {
				user.messageBox <- []byte("FOUNDPERSON:" + user2.name)
			}
		}
	}
	if utils.StringStartWith(message, "MESSAGE:") {
		id := utils.GetId(message)
		user, exist := Get(id)
		if exist {
			target, exist := Get(user.counterparty)
			if exist {
				target.messageBox <- []byte(message)
			}
		}
	}
}