Documentation ¶
Overview ¶
Package sockjs implements server side counterpart for the SockJS-client browser library.
See http://sockjs.org for more information about SockJS.
Example ¶
package main import ( "fmt" "github.com/fzzy/sockjs-go/sockjs" "net/http" ) func echoHandler(s sockjs.Session) { for { m := s.Receive() if m == nil { break } s.Send(m) } } func main() { // Handlers for two echo servers and a file server. conf := sockjs.NewConfig() dwsconf := sockjs.NewConfig() dwsconf.Websocket = false mux := sockjs.NewServeMux(http.DefaultServeMux) mux.Handle("/echo", echoHandler, conf) mux.Handle("/disabled_websocket_echo", echoHandler, dwsconf) http.Handle("/static", http.FileServer(http.Dir("./static"))) err := http.ListenAndServe(":8081", mux) if err != nil { fmt.Println(err) } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHandler ¶
NewHandler creates a new SockJS handler with the given prefix, handler function and configuration.
Example ¶
package main import ( "fmt" "github.com/fzzy/sockjs-go/sockjs" "net/http" ) func echoHandler(s sockjs.Session) { for { m := s.Receive() if m == nil { break } s.Send(m) } } func main() { // Handle only SockJS requests prefixed with "/echo". h := sockjs.NewHandler("/echo", echoHandler, sockjs.NewConfig()) err := http.ListenAndServe(":8081", h) if err != nil { fmt.Println(err) } }
Output:
Types ¶
type Config ¶
type Config struct { // URL for SockJS client library. // Default: "https://cdn.sockjs.org/sockjs-0.3.4.min.js" SockjsURL string // Enables websocket transport. // Default: true Websocket bool // Byte limit that can be sent over streaming session before it's closed. // Default: 128 * 1024 ResponseLimit int // Adds JSESSIONID cookie to requests to enable sticky sessions. // Dummy value is used unless JsessionFunc is set. // Default: false. Jsessionid bool // Function that is called to set the JSESSIONID cookie, if Jsessionid setting is set. JsessionidFunc func(http.ResponseWriter, *http.Request) // Enables IP-address checks for legacy transports. // If enabled, all subsequent calls must be from the same IP-address. // Default: true VerifyAddr bool // Heartbeat delay. // Default: 25 seconds HeartbeatDelay time.Duration // Disconnect delay. // Default: 5 seconds DisconnectDelay time.Duration // List of headers that are copied from incoming requests to SessionInfo. // Default: []string{"referer", "x-client-ip", "x-forwarded-for", // "x-cluster-client-ip", "via", "x-real-ip", "host"} Headers []string // Logger used for logging various information. // Useful for debugging. // Set Logger to nil to disable logging. // Default: log.New(os.Stdout, "sockjs: ", log.LstdFlags) Logger *log.Logger // contains filtered or unexported fields }
type RequestInfo ¶
type RequestInfo struct { // URL which was sought. URL url.URL // Copy of the HTTP headers listed in Config.Headers. Header http.Header // Host on which the URL was sought. Host string // Remote address of the client in "IP:port" format. RemoteAddr string // RequestURI is the unmodified Request-URI of the // Request-Line (RFC 2616, Section 5.1) as sent by the client // to a server. Usually the URL field should be used instead. RequestURI string // Prefix of the URL on which the request was handled. Prefix string }
RequestInfo contains information copied from the last received request associated with the session.
type ServeMux ¶
type ServeMux struct {
// contains filtered or unexported fields
}
ServeMux is SockJS-compatible HTTP request multiplexer, similar to http.ServeMux, but just for SockJS handlers. It can optionally wrap an alternate http.Handler which is called for non-SockJS paths.
func NewServeMux ¶
NewServeMux creates a new ServeMux with the given alternate handler. If alt is nil, alternate handler is not used.
type Session ¶
type Session interface { // Receive blocks until a message can be returned from session receive buffer or nil, // if the session is closed. Receive() (m []byte) // Send appends the given message to session send buffer. // Panics, if the session is closed. Send(m []byte) // Close closes the session. // Pending sends will be discarded unless the client receives them within // Config.DisconnectDelay. Close(code int, reason string) // End is a convenience method for closing with the default code and reason, // Close(3000, "Go away!"). End() // Info returns a RequestInfo object containing information copied from the last received // request. Info() RequestInfo // Protocol returns the underlying transport protocol of the session. Protocol() Protocol // String returns a string representation of the session. String() string }
Session describes a SockJS session.
type SessionPool ¶
type SessionPool struct {
// contains filtered or unexported fields
}
SessionPool is a structure for thread-safely storing sessions and broadcasting messages to them.
func NewSessionPool ¶
func NewSessionPool() (p *SessionPool)
func (*SessionPool) Add ¶
func (p *SessionPool) Add(s Session)
Add adds the given session to the session pool.
func (*SessionPool) Broadcast ¶
func (p *SessionPool) Broadcast(m []byte)
Broadcast sends the given message to every session in the pool.
func (*SessionPool) Remove ¶
func (p *SessionPool) Remove(s Session)
Remove removes the given session from the session pool. It is safe to remove non-existing sessions.
Notes ¶
Bugs ¶
Websocket.Close() should specify code and reason. Websocket package does not allow doing this. http://code.google.com/p/go/issues/detail?id=4588
Received websocket message size should be limited to something reasonable. http://code.google.com/p/go/issues/detail?id=5082