func connect(ns *socketio.NameSpace) { // 获取所有数据 messages := c.FindAll() if err := ns.Emit("connect", messages); err != nil { log.Warn(errors.As(err)) return } id := ns.Id() ip := Ip(sio.Request) content := "我上线了,大家在聊什么呢?" message := &Message{ip, Format(time.Now()), content} // go func(map[string]*socketio.NameSpace, *Message) { for k, ons := range users { if k != id { if err := ons.Emit("join", message); err != nil { log.Warn(errors.As(err, k)) return } } } // }(users, message) if _, ok := users[id]; !ok { // 添加到列表 users[id] = ns ips[id] = ip } }
func main() { http.HandleFunc("/", Public) http.HandleFunc("/index", Index) http.HandleFunc("/add", Add) // listen if err := http.ListenAndServe(":8080", nil); err != nil { log.Warn(errors.As(err)) } }
// index func Index(w http.ResponseWriter, r *http.Request) { // FindAll users := user.FindAll() if err := t.ExecuteTemplate(w, "index.html", users); err != nil { log.Warn(errors.As(err)) } return }
func say(ns *socketio.NameSpace, content string) { ip := ips[ns.Id()] message := c.Add(ip, content) // go func(map[string]*socketio.NameSpace, *Message) { for k, ons := range users { if err := ons.Emit("say", message); err != nil { log.Warn(errors.As(err, k)) return } } // }(users, message) }
// index func Add(w http.ResponseWriter, r *http.Request) { name := r.FormValue("name") if len(name) == 0 { name = "Empty" } user.Add(name) users := user.FindAll() if err := t.ExecuteTemplate(w, "index.html", users); err != nil { log.Warn(errors.As(err)) } return }
func disconnect(ns *socketio.NameSpace) { id := ns.Id() ip := Ip(sio.Request) content := "我已经下线,大家聊的开心!" message := &Message{ip, Format(time.Now()), content} for k, ons := range users { if k != id { if err := ons.Emit("quit", message); err != nil { log.Warn(errors.As(err, k)) return } } } if _, ok := users[id]; ok { // 从列表删除 delete(users, id) delete(ips, ip) } }