func sockStartStopHandler(conn sockjs.Conn) { log.Println("start/stop session") ticker := time.NewTicker(time.Second) open := true for { if msg, err := conn.ReadMessage(); err != nil { log.Println("getting err: ", err) ticker.Stop() open = false return } else { if string(msg) == `"start"` { if !open { ticker = time.NewTicker(time.Second) open = true } go func() { for t := range ticker.C { conn.WriteMessage([]byte(fmt.Sprintf(`"%d"`, t.Second()))) } }() } else if string(msg) == `"stop"` { ticker.Stop() open = false } } } }
func (s *GenericStreamer) SockJSHandler(crudpath string, session sockjs.Conn) { localChan := make(chan *Bundle) s.bundleChannels = append(s.bundleChannels, localChan) if s.debug { log.Println("Streams:", len(s.bundleChannels), "- open", crudpath) } go func() { for { bundle, ok := <-localChan if !ok { // Chanel was closed break } if crudpath != bundle.Val.CRUDPrefix() { continue } json, err := json.Marshal(bundle) if err != nil { log.Println(err) continue // Keep trying! } _, err = session.WriteMessage(json) if err != nil { log.Println(err) break // Assume connection has died } } }() for { // This is needed to catch the closure of the sock _, err := session.ReadMessage() if err != nil { break } } for i, sliceChan := range s.bundleChannels { if sliceChan == localChan { s.bundleChannels = append(s.bundleChannels[0:i], s.bundleChannels[i+1:len(s.bundleChannels)]...) break } } if s.debug { log.Println("Streams:", len(s.bundleChannels), "- close", crudpath) } close(localChan) }
func (self *SockjsDispatcherTransport) ConnectionHandler(session sockjs.Conn) { log.Println("Client session created: transport=sockjs)") for { _, err := session.ReadMessage() if err != nil { break } go func() { session.WriteMessage([]byte("{\"Id\":\"PING\", \"Body\":\"Ping\", \"Error\":null}")) }() time.Sleep(1 * time.Second) } log.Println("Client session closed: transport=sockjs") }
func SockJSHandler(session sockjs.Conn) { log.Println("Session created") for { val, err := session.ReadMessage() if err != nil { break } go func() { session.WriteMessage(val) }() } log.Println("session closed") }
func sockEchoHandler(conn sockjs.Conn) { log.Println("echo session") for { if msg, err := conn.ReadMessage(); err != nil { log.Println("getting err:", err) return } else { go func() { conn.WriteMessage(msg) }() } } }
func EchoHandler(conn sockjs.Conn) { for { if msg, err := conn.ReadMessage(); err == nil { _, err := conn.WriteMessage(msg) if err != nil { log.Fatal(err) } } else { return } } }
func sockPingHandler(conn sockjs.Conn) { log.Println("ping session") for { if msg, err := conn.ReadMessage(); err != nil { log.Println("getting err:", err) return } else { if string(msg) == `"ping"` { go func() { conn.WriteMessage([]byte(`"pong"`)) }() } } } }
func sockSineHandler(conn sockjs.Conn) { log.Println("sine session") var x, y float64 var sine string ticker := time.NewTicker(time.Second) open := true for { if msg, err := conn.ReadMessage(); err != nil { log.Println("getting err:", err) ticker.Stop() open = false return } else { if string(msg) == `"start"` { if !open { ticker = time.NewTicker(time.Second) open = true } go func() { for t := range ticker.C { x = float64(t.Nanosecond()) / 1000 y = 2.5 * (1 + math.Sin(x)) sine = fmt.Sprintf(`{"x": "%f", "y": "%f"}`, x, y) log.Printf(sine) conn.WriteMessage([]byte(sine)) } }() } else if string(msg) == `"stop"` { log.Println("stop sine") ticker.Stop() open = false } } } }
func CloseHandler(conn sockjs.Conn) { conn.Close() }