sockjs

package
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2020 License: BSD-3-Clause Imports: 15 Imported by: 152

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{
	Websocket:       true,
	JSessionID:      nil,
	SockJSURL:       "//cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js",
	HeartbeatDelay:  25 * time.Second,
	DisconnectDelay: 5 * time.Second,
	ResponseLimit:   128 * 1024,
}

DefaultOptions is a convenient set of options to be used for sockjs

View Source
var (
	// ErrSessionNotOpen error is used to denote session not in open state.
	// Recv() and Send() operations are not suppored if session is closed.
	ErrSessionNotOpen = errors.New("sockjs: session not in open state")
)
View Source
var WebSocketReadBufSize = 4096

WebSocketReadBufSize is a parameter that is used for WebSocket Upgrader. https://github.com/gorilla/websocket/blob/master/server.go#L230

View Source
var WebSocketWriteBufSize = 4096

WebSocketWriteBufSize is a parameter that is used for WebSocket Upgrader https://github.com/gorilla/websocket/blob/master/server.go#L230

Functions

func DefaultJSessionID

func DefaultJSessionID(rw http.ResponseWriter, req *http.Request)

DefaultJSessionID is a default behaviour function to be used in options for JSessionID if JSESSIONID is needed

func NewHandler

func NewHandler(prefix string, opts Options, handleFunc func(Session)) http.Handler

NewHandler creates new HTTP handler that conforms to the basic net/http.Handler interface. It takes path prefix, options and sockjs handler function as parameters

Example (DefaultMux)
package main

import (
	"net/http"

	"gopkg.in/igm/sockjs-go.v2/sockjs"
)

func main() {
	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)
}
Output:

Example (Simple)
package main

import (
	"net/http"

	"gopkg.in/igm/sockjs-go.v2/sockjs"
)

func main() {
	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)
}
Output:

Types

type Options

type Options struct {
	// Transports which don't support cross-domain communication natively ('eventsource' to name one) use an iframe trick.
	// A simple page is served from the SockJS server (using its foreign domain) and is placed in an invisible iframe.
	// Code run from this iframe doesn't need to worry about cross-domain issues, as it's being run from domain local to the SockJS server.
	// This iframe also does need to load SockJS javascript client library, and this option lets you specify its url (if you're unsure,
	// point it to the latest minified SockJS client release, this is the default). You must explicitly specify this url on the server
	// side for security reasons - we don't want the possibility of running any foreign javascript within the SockJS domain (aka cross site scripting attack).
	// Also, sockjs javascript library is probably already cached by the browser - it makes sense to reuse the sockjs url you're using in normally.
	SockJSURL string
	// Most streaming transports save responses on the client side and don't free memory used by delivered messages.
	// Such transports need to be garbage-collected once in a while. `response_limit` sets a minimum number of bytes that can be send
	// over a single http streaming request before it will be closed. After that client needs to open new request.
	// Setting this value to one effectively disables streaming and will make streaming transports to behave like polling transports.
	// The default value is 128K.
	ResponseLimit uint32
	// Some load balancers don't support websockets. This option can be used to disable websockets support by the server. By default websockets are enabled.
	Websocket bool
	// In order to keep proxies and load balancers from closing long running http requests we need to pretend that the connection is active
	// and send a heartbeat packet once in a while. This setting controls how often this is done.
	// By default a heartbeat packet is sent every 25 seconds.
	HeartbeatDelay time.Duration
	// The server closes a session when a client receiving connection have not been seen for a while.
	// This delay is configured by this setting.
	// By default the session is closed when a receiving connection wasn't seen for 5 seconds.
	DisconnectDelay time.Duration
	// Some hosting providers enable sticky sessions only to requests that have JSessionID cookie set.
	// This setting controls if the server should set this cookie to a dummy value.
	// By default setting JSessionID cookie is disabled. More sophisticated behaviour can be achieved by supplying a function.
	JSessionID func(http.ResponseWriter, *http.Request)
}

Options type is used for defining various sockjs options

type Session

type Session interface {
	// Id returns a session id
	ID() string
	// Recv reads one text frame from session
	Recv() (string, error)
	// Send sends one text frame to session
	Send(string) error
	// Close closes the session with provided code and reason.
	Close(status uint32, reason string) error
}

Session represents a connection between server and client.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL