func (d *device) SocketHandler(s sockjs.Session) { d.clients.Add(s) defer d.clients.Remove(s) for { time.Sleep(d.delay) m := s.Receive() if m == nil { break } js := message_in{} err := json.Unmarshal(m, &js) log.Print(js) if err != nil { log.Print(err) } fi, err := os.OpenFile(js.Name, os.O_WRONLY, 0666) if err != nil { log.Print(err) } // _, err = fi.Write([]byte(js.Represent())) if err != nil { log.Print(err) } log.Print(js.Represent()) fi.Close() } }
func echoHandler(s sockjs.Session) { for { m := s.Receive() if m == nil { break } s.Send(m) } }
func dataHandler(s sockjs.Session) { sockjsClients.Add(s) defer sockjsClients.Remove(s) for { m := s.Receive() if m == nil { break } } }
func ChatHandler(s sockjs.Session) { Users.Add(s) defer Users.Remove(s) for { m := s.Receive() if m == nil { break } m = []byte(fmt.Sprintf("%s %s: %s", strings.Split(time.Now().Format(layout), " ")[1], username, m)) Users.Broadcast(m) time.Sleep(5 * time.Second) } }
func chatHandler(s sockjs.Session) { users.Add(s) defer users.Remove(s) for { m := s.Receive() if m == nil { break } fullAddr := s.Info().RemoteAddr addr := fullAddr[:strings.LastIndex(fullAddr, ":")] m = []byte(fmt.Sprintf("%s: %s", addr, m)) users.Broadcast(m) } }
func connHandler(s sockjs.Session) { c, err := DialTLS("imap.gmail.com", nil) if err != nil { log.Fatal(err) s.Send(err2byte(err)) s.End() return } go func() { for { r, err := c.Receive() if err != nil { s.Send(err2byte(err)) continue } b, err := json.Marshal(r) if err != nil { s.Send(err2byte(err)) continue } s.Send(b) } }() for { m := s.Receive() if m == nil { break } cmd := &Command{} err := json.Unmarshal(m, cmd) fmt.Printf("%s\n%v\n", m, cmd) if err != nil { s.Send(err2byte(err)) continue } c.Send(cmd) } }