func pushHandler(ws *websocket.Conn) { client := &Client{ws, "", "", 0, time.Now()} for { var f map[string]interface{} var err error if err = websocket.JSON.Receive(ws, &f); err != nil { log.Println("Websocket Disconnected.", err.Error()) break } client.LastContact = time.Now() log.Println("pushHandler msg: ", f["messageType"]) messageType, found := f["messageType"] if !found { // treat it as a ping websocket.Message.Send(client.Websocket, "{}") continue } switch messageType { case "hello": handleHello(client, f) break case "register": handleRegister(client, f) break case "unregister": handleUnregister(client, f) break case "ack": handleAck(client, f) break default: log.Println(" -> Unknown", f) break } saveState() } log.Println("Closing Websocket!") ws.Close() // if a client disconnected before completing the handshake // it'll have an empty UAID if client.UAID != "" { gServerState.ConnectedClients[client.UAID].Websocket = nil } }
// websocket,统计在线用户数 // uri: /ws func WsHandler(wsConn *websocket.Conn) { mutex.Lock() ServerId++ serverId := ServerId mutex.Unlock() req := wsConn.Request() user, err := strconv.Atoi(req.FormValue("uid")) if err != nil || user == 0 { pos := strings.LastIndex(req.RemoteAddr, ":") ip := req.RemoteAddr[:pos] user = int(util.Ip2long(ip)) } userData := service.Book.AddUser(user, serverId) // 给自己发送消息,告诉当前在线用户数、历史最高在线人数 onlineInfo := map[string]int{"online": service.Book.Len(), "maxonline": service.MaxOnlineNum()} message := service.NewMessage(service.WsMsgOnline, onlineInfo) err = websocket.JSON.Send(wsConn, message) if err != nil { logger.Errorln("Sending onlineusers error:", err) } var clientClosed = false for { select { case message := <-userData.MessageQueue(serverId): if err := websocket.JSON.Send(wsConn, message); err != nil { clientClosed = true } // 心跳 case <-time.After(30e9): if err := websocket.JSON.Send(wsConn, ""); err != nil { clientClosed = true } } if clientClosed { service.Book.DelUser(user, serverId) break } } // 用户退出时需要变更其他用户看到的在线用户数 if !service.Book.UserIsOnline(user) { message := service.NewMessage(service.WsMsgOnline, map[string]int{"online": service.Book.Len()}) go service.Book.BroadcastAllUsersMessage(message) } }
func pushHandler(ws *websocket.Conn) { client := &Client{ws, "", "", 0, time.Now()} for { var f map[string]interface{} var err error if err = websocket.JSON.Receive(ws, &f); err != nil { log.Println("Websocket Disconnected.", err.Error()) break } client.LastContact = time.Now() log.Println("pushHandler msg: ", f["messageType"], len(f), f) if len(f) == 0 { websocket.Message.Send(client.Websocket, "{}") continue } messageType, found := f["messageType"] if !found { // treat it as a ping f["messageType"] = "" messageType = "" } switch messageType.(type) { case string: switch strings.ToLower(messageType.(string)) { case "hello": handleHello(client, f) break case "register": handleRegister(client, f) break case "unregister": handleUnregister(client, f) break case "ack": handleAck(client, f) break case "ping": websocket.Message.Send(client.Websocket, "{}") continue case "purge": verbose("Purging data for ", client.UAID) handlePurge(client, f) break default: log.Println(" -> Unknown", f) verbose("Please only use 'hello', 'register', 'unregister'", "or 'ack' as messageType values.") handleError(client, f, 401, "Invalid Command") break } saveState() default: handleError(client, f, 401, "Invalid Command") verbose("Please use only strings as messageTypes") break } } log.Println("Closing Websocket!") ws.Close() // if a client disconnected before completing the handshake // it'll have an empty UAID if client.UAID != "" { gServerState.ConnectedClients[client.UAID].Websocket = nil } }