示例#1
0
文件: sockgroup.go 项目: ku-ovdp/api
//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)
}
示例#2
0
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)
}
示例#3
0
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)

}