Documentation
¶
Overview ¶
Package gocketio provides a client for the https://socket.io/ protocol
Index ¶
- Variables
- type AckFunc
- type Manager
- type ManagerConfig
- type Socket
- func (s *Socket) Connected() bool
- func (s *Socket) Emit(event string, data ...interface{}) error
- func (s *Socket) EmitWithAck(event string, ackFunc AckFunc, data ...interface{}) error
- func (s *Socket) ID() string
- func (s *Socket) Namespace() string
- func (s *Socket) Off(event string)
- func (s *Socket) On(event string, handler interface{}) error
- func (s *Socket) Send(data ...interface{}) error
- func (s *Socket) SetErrorHandler(handler func(data interface{}))
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrBlacklistedEvent = errors.New("Blacklisted event")
ErrBlacklistedEvent is returned when an attempt is made to Emit a reserved event from a Socket
var ErrInvalidAddress = errors.New("Invalid address")
ErrInvalidAddress is returned when the user-supplied address is invalid
Functions ¶
This section is empty.
Types ¶
type AckFunc ¶
type AckFunc func(id int64, data interface{})
AckFunc is a func that can be called in response to receiving an ACK packet
type Manager ¶
Manager manages Socket.IO connections to a single server across multiple namespaces
func DialContext ¶
DialContext creates a new instance of Manager connected to the server at address
ctx is used to create derived contexts for a variety of long-running operations. If ctx is cancelled, either manually or automatically (i.e. created with context.WithTimeout or or context.WithDeadline) the Manager and its Sockets will cease to function and must be recreated using a new context and call to DialContext
func (*Manager) Connected ¶
Connected returns true if the underlying transport is connected, false otherwise
type ManagerConfig ¶
type ManagerConfig struct {
// Backoff is the backoff strategy used for connection attempts
BackOff backoff.BackOff
// ConnectionTimeout is the amount of time to wait during a connection
// attempt before stopping and trying again
ConnectionTimeout time.Duration
// AdditionalQueryArgs is a map of additional string values that are appended to the
// query string of the server address
AdditionalQueryArgs map[string]string
}
ManagerConfig contains configuration information for a Manager
func DefaultManagerConfig ¶
func DefaultManagerConfig() *ManagerConfig
DefaultManagerConfig returns a ManagerConfig with sane defaults
ConnectionTimeout is 20 seconds Backoff is backoff.ExponentialBackoff AdditionalQueryArgs is an empty map
type Socket ¶
Socket sends messages to and receive messages from a Socket.IO Namespace https://socket.io/docs/rooms-and-namespaces/
func (*Socket) Connected ¶
Connected returns true if the Socket is connected, false otherwise
The socket is considered to be connected if 1. The Manager that created the Socket is connected and 2. The socket has not received a DISCONNCT message from the server
func (*Socket) Emit ¶
Emit raises an event on the server
The Emit method is safe for use by multiple concurrent goroutines; however, the order that events are raised on the server is not guaranteed.
Example ¶
// Connect to a Socket.IO server at example.com
m, err := DialContext(context.Background(), "https://example.com/", DefaultManagerConfig())
if err != nil {
log.Fatal(err)
}
// Get a reference to a Socket for the root namespace
s, err := m.Namespace("/")
if err != nil {
log.Fatal(err)
}
// Raises the "hello" event on the server with "world" as the event data
err = s.Emit("hello", "world")
if err != nil {
log.Fatal(err)
}
func (*Socket) EmitWithAck ¶
EmitWithAck raises an event on the server, and registers a callback that is invoked when the server acknowledges receipt
The Emit withAckMethod is safe for use by multiple concurrent goroutines; however, the order that events are raised on the server is not guaranteed.
Example ¶
// Connect to a Socket.IO server at example.com
m, err := DialContext(context.Background(), "https://example.com/", DefaultManagerConfig())
if err != nil {
log.Fatal(err)
}
// Get a reference to a Socket for the root namespace
s, err := m.Namespace("/")
if err != nil {
log.Fatal(err)
}
// Raises the "hello" event on the server with "world" as the event data
// and registers an event handler that will be called once the server
// has acknowledged receipt of the event
err = s.EmitWithAck("hello", func(id int64, data interface{}) {
fmt.Printf("Ack %v with data %v\n", id, data)
}, "world")
if err != nil {
log.Fatal(err)
}
func (*Socket) Off ¶
Off removes the event handler for the event. Calling Off() for an event that does not have a handler defined is a no-op.
The Off method is safe for use by multiple concurrent goroutines
Example ¶
// Connect to a Socket.IO server at example.com
m, err := DialContext(context.Background(), "https://example.com/", DefaultManagerConfig())
if err != nil {
log.Fatal(err)
}
// Get a reference to a Socket for the root namespace
s, err := m.Namespace("/")
if err != nil {
log.Fatal(err)
}
// Add a handler for the hello event
err = s.On("hello", func(from string) {
fmt.Printf("Hello from %v\n", from)
})
if err != nil {
log.Fatal(err)
}
// After the call to s.Off, the handler for the "hello" event
// will never be called
s.Off("hello")
func (*Socket) On ¶
On sets the event handler for the event. It returns an error if the event name is blacklisted, or if the handler is not a func
There may be exactly one handler for an event at any time. Once an event handler has been set, subsequent calls to On will replace the existing handler.
The On method is safe for use by multiple concurrent goroutines; however, the order in which the event handlers are added is not strictly defined.
Example ¶
// Connect to a Socket.IO server at example.com
m, err := DialContext(context.Background(), "https://example.com/", DefaultManagerConfig())
if err != nil {
log.Fatal(err)
}
// Get a reference to a Socket for the root namespace
s, err := m.Namespace("/")
if err != nil {
log.Fatal(err)
}
// Add a handler for the hello event
err = s.On("hello", func(from string) {
fmt.Printf("Hello from %v\n", from)
})
if err != nil {
log.Fatal(err)
}
func (*Socket) Send ¶
Send raises a "message" event on the server
The Send method is safe for use by multiple concurrent goroutines
Example ¶
// Connect to a Socket.IO server at example.com
m, err := DialContext(context.Background(), "https://example.com/", DefaultManagerConfig())
if err != nil {
log.Fatal(err)
}
// Get a reference to a Socket for the root namespace
s, err := m.Namespace("/")
if err != nil {
log.Fatal(err)
}
// Raises a "message" event on the server with "hello", "world" as the event data
err = s.Send("hello", "world")
if err != nil {
log.Fatal(err)
}
func (*Socket) SetErrorHandler ¶ added in v0.9.5
func (s *Socket) SetErrorHandler(handler func(data interface{}))
SetErrorHandler sets the error handler that is invoked when Error packets are received
Note that this is not a general error handler; if an error is returned from an internal process - e.g. encoding or decoding a packet, network timeout, etc. - this handler will not be invoked