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)
			}
		}
	}
}
Beispiel #3
0
func GetMessage() string {
	var result string
	if active {
		//connect.SetReadDeadline(time.Now().Add(time.Second * 1))
		mtype, message, err := connect.ReadMessage()
		if err != nil {
			log.Println("Can't receive a message because of:", err)
			active = false
			CloseConnection()
		}
		switch mtype {
		case websocket.TextMessage:
			result = string(message)
			if utils.StringStartWith(result, "MESSAGE:") {
				pos := strings.LastIndex(result, ":")
				result = string(result[pos+1:])
			}
		case websocket.PingMessage:
			connect.WriteMessage(websocket.PongMessage, []byte(""))
		case websocket.CloseMessage:
			CloseConnection()
		}
	}
	return result
}