//Push func Push(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) app_key := params["app_key"] content := r.FormValue("content") //option user_tag := r.FormValue("user_tag") if app_key == "" || content == "" { log.Warn(r.RemoteAddr, " empty app_key || content") http.Error(w, "app_key || content empty", 400) return } app := gwspack.Get(app_key) b := []byte(content) if user_tag == "" { app.SendAll(b) } else { app.SendByRegex(user_tag, b) } pushResult := &PushResult{ AppKey: app_key, Content: content, UserTag: user_tag, Total: app.CountById(), } log.Info(r.RemoteAddr, " message send ", content) json.NewEncoder(w).Encode(pushResult) }
func ListClient(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) app_key := params["app_key"] if app_key == "" { log.Warn(r.RemoteAddr, " app_key empty") http.Error(w, "app_key empty", 404) return } app := gwspack.Get(app_key) onlineUsers := app.List() lo := ListOnlineResult{ AppKey: app_key, Total: len(onlineUsers), UserTags: onlineUsers, } log.Info(r.RemoteAddr, " GetAppUsers") json.NewEncoder(w).Encode(lo) }
func WSConnect(w http.ResponseWriter, r *http.Request) { log.Info(r.RemoteAddr, " handshake start") params := mux.Vars(r) app_key := params["app_key"] user_tag := params["user_tag"] if app_key == "" || user_tag == "" { log.Warn(r.RemoteAddr, " app_key & user_tag empty") http.Error(w, "app_key || user_tag empty", 404) return } app := gwspack.Get(app_key) c, err := app.Register(user_tag, w, r, nil) if err != nil { log.Warn(r.RemoteAddr, " ", err) return } log.Info(r.RemoteAddr, " login ", app_key, " Scuess") c.Listen() defer log.Info(r.RemoteAddr, " ", user_tag, " logout", " ", app_key) return }