func OnlineUsersSimpleHandler(w http.ResponseWriter, req *http.Request) { defer func() { if err := recover(); err != nil { utils.Log.Println(err) debug.PrintStack() } }() w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "channel, tourid, tag") var online_users_simple lib.OnlineUsersSimple var channel_name string all_channel.RLock.RLock() defer all_channel.RLock.RUnlock() channel_name = req.Header.Get("channel") channel_name = strings.Trim(channel_name, " ") if channel_name == "" { utils.Log.Printf("[%s] channel name not in header\n", req.RemoteAddr) http.Error(w, "channel name not in header", 400) return } channel := GetChannel(channel_name) online_users_simple.Length = atomic.LoadUint64(&channel.RealUserCount) online_users_simple.Result = 0 buf, err := ffjson.Marshal(online_users_simple) if err != nil { utils.Log.Printf("[%s] Marshal JSON failed: [%s], channel: [%s]\n", req.RemoteAddr, err, channel_name) http.Error(w, "Marshal json failed", 500) return } w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(buf) ffjson.Pool(buf) }
func OnlineUsersSimpleHandler(w http.ResponseWriter, req *http.Request) { defer func() { if err := recover(); err != nil { utils.Log.Println(err) debug.PrintStack() } }() w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "channel, tourid, tag") var online_users_simple lib.OnlineUsersSimple var channel_name string var mqtt_users lib.MQTTSingleTopicUsers all_channel.RLock.RLock() defer all_channel.RLock.RUnlock() channel_name = req.Header.Get("channel") channel_name = strings.Trim(channel_name, " ") if channel_name == "" { utils.Log.Printf("[%s] channel name not in header\n", req.RemoteAddr) http.Error(w, "channel name not in header", 400) return } if Config.MQTTServerEnable == true { resp, err := http.Get("http://" + Config.MQTTServerAddress + ":18088/api/simple_topic/" + channel_name) if resp != nil { defer resp.Body.Close() } if err != nil { utils.Log.Printf("[%s] Get MQTT online users failed: %s, channel: [%s]\n", req.RemoteAddr, err, channel_name) http.Error(w, "failed to get MQTT users", 500) return } body, err := ioutil.ReadAll(resp.Body) if err != nil { utils.Log.Printf("[%s] Read MQTT response failed: %s, channel: [%s]\n", req.RemoteAddr, err, channel_name) http.Error(w, "read MQTT users failed", 500) return } err = json.Unmarshal(body, &mqtt_users) if err != nil { utils.Log.Printf("[%s] Unmarshal JSON failed: [%s], channel: [%s]\n", req.RemoteAddr, err, channel_name) http.Error(w, "Unmarshal json failed", 500) return } online_users_simple.Length = mqtt_users.Length } channel := GetChannel(channel_name) online_users_simple.Length += atomic.LoadUint64(&channel.RealUserCount) online_users_simple.Result = 0 buf, err := ffjson.Marshal(online_users_simple) if err != nil { utils.Log.Printf("[%s] Marshal JSON failed: [%s], channel: [%s]\n", req.RemoteAddr, err, channel_name) http.Error(w, "Marshal json failed", 500) return } w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(buf) ffjson.Pool(buf) }