//func (sg *sockgroup) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (sg *sockgroup) Handler(baseUrl string) http.Handler { return sockjs.NewRouter(baseUrl, func(sock sockjs.Conn) { sg.register <- sock // incoming message pump go func() { var err error for err == nil { _, err = sock.ReadMessage() } sg.unregister <- sock }() }, sockjs.DefaultConfig) }
func ExampleNewRouter() { // Echo Handler var handler = func(c sockjs.Conn) { for { msg, err := c.ReadMessage() if err == sockjs.ErrConnectionClosed { return } c.WriteMessage(msg) } } router := sockjs.NewRouter("/echo", handler, sockjs.DefaultConfig) http.Handle("/echo", router) http.ListenAndServe(":8080", nil) }
func GenericStreamingHandler(g *GenericStreamer, w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) // Build a URI like representation of the datatype types := []string{vars["datatype"]} if key, ok := vars["key"]; ok { types = append(types, key) } if childtype, ok := vars["childtype"]; ok { types = append(types, childtype) } crudstring := strings.Join(types, "/") sockHandler := sockjs.NewRouter("/", func(session sockjs.Conn) { g.SockJSHandler(crudstring, session) }, sockjs.DefaultConfig) (http.StripPrefix("/"+crudstring, sockHandler)).ServeHTTP(w, r) }