Documentation ¶
Overview ¶
Package arbit is a minimal event-driven, websocket library.
Message Format ¶
Messages are formatted as 2-element JSON arrays: [eventName, messageData]. Therefore, the message data must be JSON-serializable.
Concurrency ¶
All functions and methods in this package are safe for concurrent use, with the exception of the On and OnClose handlers. arbit handles the concurrency problems for you.
Example: As Server
arb := arbit.NewServer() arb.On("echo", func(cl *arbit.Client, data interface{}) { cl.Send("echo", "Echo: " + fmt.Sprint(data)) }) http.Handle("/ws", srv) http.ListenAndServe(":8080", nil)
Example: As Client
arb := arbit.NewClient("http://localhost:8080/ws", http.Header{}) arb.On("echo", func(data interface{}) { log.Println("Message: " + fmt.Sprint(data)) arb.Close() }) arb.Send("echo", "Hello world!") arb.Listen()
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Type Client represents a websocket connection.
func (*Client) Close ¶
Method Close closes the connection with a given code. Codes 4000–4999 are available for use by applications.
See https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent for more information about close codes.
func (*Client) Listen ¶
Method Listen starts the event loop that will call the event and close handlers.
func (*Client) On ¶
Method On registers an event handler. Each event can have at most one handler.
The event handler will be executed in the goroutine that called Listen.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Type Server represents a server. It manages a set of websocket connections.
func (*Server) Clients ¶
Method Clients returns a list of Clients that are currently connected to the server.
func (*Server) On ¶
Method On registers a handler for a given event. Each event can have at most one handler.
Calling On and OnClose on the given client will have no effect.
func (*Server) OnClose ¶
Method OnClose registers a close handler, which is called when a connection is closed. There can be at most one close handler.
The code passed in is the code passed to Client.Close; see Client.Close for more information.
Calling On and OnClose on the given client will have no effect.