func ExampleNewHandler_simple() {
	handler := sockjs.NewHandler("/echo", sockjs.DefaultOptions, func(session sockjs.Session) {
		for {
			if msg, err := session.Recv(); err == nil {
				if session.Send(msg) != nil {
					break
				}
			} else {
				break
			}
		}
	})
	http.ListenAndServe(":8080", handler)
}
Exemple #2
0
func (s *server) bind() error {
	// primary transport is websockets, pusher uses its own websocket endpoint
	// what ive done is hack the sock js code a little so it has similar interface
	// this can certainly be improved, but for now it works ok
	http.Handle("/app/", pusher.NewHandler("/app", sockjs.DefaultOptions, s.newPusherWSHandlerFunc()))
	// fallback transports to sockjs, this does xhr-streaming, polling, iframes, etc
	http.Handle("/pusher/", sockjs.NewHandler("/pusher", sockjs.DefaultOptions, s.newSockJSHandlerFunc()))
	// add an auth endpoint for generating the signatures used in private and presence channels
	http.HandleFunc("/auth", s.newAuthHandlerFunc())
	// lastly bind web folder for static files
	http.Handle("/", http.FileServer(http.Dir("web/")))

	return http.ListenAndServe(s.opts.WebSocketAddress, nil)
}
func ExampleNewHandler_defaultMux() {
	handler := sockjs.NewHandler("/echo", sockjs.DefaultOptions, func(session sockjs.Session) {
		for {
			if msg, err := session.Recv(); err == nil {
				if session.Send(msg) != nil {
					break
				}
			} else {
				break
			}
		}
	})
	// need to provide path prefix for http.Mux
	http.Handle("/echo/", handler)
	http.ListenAndServe(":8080", nil)
}
Exemple #4
0
// Start will start the server on the given port.
func (v *sockjsVessel) Start(sockPortStr, httpPortStr string) error {
	if err := v.persister.Prepare(); err != nil {
		return err
	}

	sockjsHandler := sockjs.NewHandler(v.uri, sockjs.DefaultOptions, v.handler())
	r := mux.NewRouter()
	r.HandleFunc(v.uri, v.httpHandler.send).Methods("POST")
	r.HandleFunc(v.uri+"/message/{id}", v.httpHandler.pollResponses).Methods("GET")
	r.HandleFunc(v.uri+"/channel/{channel}", v.httpHandler.pollSubscription).Methods("GET")
	http.Handle("/", &httpServer{r})
	go func() {
		http.ListenAndServe(httpPortStr, nil)
	}()
	return http.ListenAndServe(sockPortStr, sockjsHandler)
}