gocketio

package module
v0.9.9 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2020 License: Apache-2.0 Imports: 14 Imported by: 0

README

gocketio

Go (golang) socket.io client

Supports

  • Non-binary Events
  • Binary Events
  • Non-binary Acks
  • Binary Acks
  • Automatic reconnection with exponential backoff
  • Multiplexing multiple namespaces over a single connection
  • Websocket transport

Installation

go get github.com/queue-b/gocketio

Usage

Additional examples are available as godocs.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/queue-b/gocketio"
)

func main() {
	// Connect to a socket.io server at example.com
	manager, err := gocketio.DialContext(context.Background(), "https://example.com/", gocketio.DefaultManagerConfig())

	if err != nil {
		log.Fatal(err)
	}

	// Create a socket for the root namespace
	socket, err := manager.Namespace("/")

	if err != nil {
		log.Fatal(err)
	}

	c := make(chan struct{}, 1)

	// Add a handler for "hello" events
	socket.On("hello", func(from string) {
		fmt.Printf("Hello from %v\n", from)
		c <- struct{}{}
	})

	// Block until someone sends a hello message
	<-c
}

License

Apache

Authors

Alex Kube

Documentation

Overview

Package gocketio provides a client for the https://socket.io/ protocol

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrBlacklistedEvent = errors.New("Blacklisted event")

ErrBlacklistedEvent is returned when an attempt is made to Emit a reserved event from a Socket

View Source
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

type Manager struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Manager manages Socket.IO connections to a single server across multiple namespaces

func DialContext

func DialContext(ctx context.Context, address string, cfg *ManagerConfig) (*Manager, error)

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

func (m *Manager) Connected() bool

Connected returns true if the underlying transport is connected, false otherwise

func (*Manager) Namespace

func (m *Manager) Namespace(namespace string) (*Socket, error)

Namespace returns a Socket for the Namespace. If there is an existing Socket for the Namespace, it is returned. If there is no existing Socket for the Namespace, it is created and returned.

The Namespace method is safe for use by multiple concurrent goroutines

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

type Socket struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Socket sends messages to and receive messages from a Socket.IO Namespace https://socket.io/docs/rooms-and-namespaces/

func (*Socket) Connected

func (s *Socket) Connected() bool

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

func (s *Socket) Emit(event string, data ...interface{}) error

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

func (s *Socket) EmitWithAck(event string, ackFunc AckFunc, data ...interface{}) error

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) ID

func (s *Socket) ID() string

ID returns the ID of the socket

func (*Socket) Namespace

func (s *Socket) Namespace() string

Namespace returns the Namespace that this socket is connected to

func (*Socket) Off

func (s *Socket) Off(event string)

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

func (s *Socket) On(event string, handler interface{}) error

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

func (s *Socket) Send(data ...interface{}) error

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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