gosocketio

package module
Version: v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: GPL-3.0 Imports: 16 Imported by: 0

README

Golang Socket.IO

This library was forked from github.com/graarh/golang-socketio

It provides a simple Golang implementation of socket.io client and server.

The code was heavily refactored, also implemented XHR polling transport for client and server upgrade XHR -> websocket.

Pull requests appreciated

Usage examples

Please observe the examples directory for usage examples:

JavaScript client:    examples/assets/index.html, serve it with:
Go server:            go run examples/server/server.go

Go client via WS:     go run examples/client_websocket/client.go
Go client via XHR:    go run examples/client_xhr_polling/client.go

Please note that no Go client upgrade implemented yet.

This client is mainly for testing purposes.

Installation

go get github.com/mtfelian/golang-socketio

TODOs, ideas to further development

  • write tests, make a good test coverage
  • Go client's upgrade from XHR to WS
  • Go server's ability to fallback from WS to XHR
  • Go client's ability to fallback from WS to XHR
  • support newer versions of socket.io protocol

Documentation

Index

Constants

View Source
const (
	OnConnection    = "connection"
	OnDisconnection = "disconnection"
	OnError         = "error"
)

Variables

View Source
var (
	ErrorSendTimeout     = errors.New("timeout")
	ErrorSocketOverflood = errors.New("socket overflood")
)
View Source
var (
	ErrorHandlerIsNotFunc   = errors.New("f is not a function")
	ErrorHandlerHasNot2Args = errors.New("f should have 1 or 2 arguments")
	ErrorHandlerWrongResult = errors.New("f should return no more than one value")
)
View Source
var (
	ErrorServerNotSet       = errors.New("server was not set")
	ErrorConnectionNotFound = errors.New("connection not found")
)
View Source
var (
	ErrorAckWaiterNotFound = errors.New("ack waiter not found")
)

Functions

func AddrPolling

func AddrPolling(host string, port int, secure bool) string

AddrPolling returns an url for socket.io connection for polling transport

func AddrWebsocket

func AddrWebsocket(host string, port int, secure bool) string

AddrWebsocket returns an url for socket.io connection for websocket transport

func CountOverfloodingChannels

func CountOverfloodingChannels() int

CountOverfloodingChannels returns an amount of overflooding channels

Types

type Channel

type Channel struct {
	// contains filtered or unexported fields
}

Channel represents socket.io connection

func (*Channel) Ack

func (c *Channel) Ack(name string, payload interface{}, timeout time.Duration) (string, error)

Ack a synchronous event with the given name and payload and wait for/receive the response

func (*Channel) Amount

func (c *Channel) Amount(room string) int

Amount returns an amount of channels joined to the given room, using channel

func (*Channel) BroadcastTo

func (c *Channel) BroadcastTo(room, name string, payload interface{})

BroadcastTo the the given room an event with given name and payload, using channel

func (*Channel) Close

func (c *Channel) Close() error

Close the client (Channel) connection

func (*Channel) Emit

func (c *Channel) Emit(name string, payload interface{}) error

Emit an asynchronous event with the given name and payload

func (*Channel) IP

func (c *Channel) IP() string

IP returns an IP of the socket client

func (*Channel) Id

func (c *Channel) Id() string

Id returns an ID of the current socket connection

func (*Channel) IsAlive

func (c *Channel) IsAlive() bool

IsAlive checks that Channel is still alive

func (*Channel) Join

func (c *Channel) Join(room string) error

Join this channel to the given room

func (*Channel) Leave

func (c *Channel) Leave(room string) error

Leave the given room (remove channel from it)

func (*Channel) List

func (c *Channel) List(room string) []*Channel

List returns a list of channels joined to the given room, using channel

func (*Channel) RequestHeader

func (c *Channel) RequestHeader() http.Header

RequestHeader returns a connection request connectionHeader

type Client

type Client struct {
	*Channel
	// contains filtered or unexported fields
}

Client represents socket.io client

func Dial

func Dial(addr string, tr transport.Transport) (*Client, error)

Dial connects to server and initializes socket.io protocol The correct ws protocol addr example: ws://myserver.com/socket.io/?EIO=3&transport=websocket

func (*Client) Close

func (c *Client) Close()

Close client connection

func (Client) On

func (e Client) On(name string, f interface{}) error

On registers message processing function and binds it to the given event name

type Server

type Server struct {
	http.Handler
	// contains filtered or unexported fields
}

Server represents a socket.io server instance

func NewServer

func NewServer() *Server

NewServer creates new socket.io server

func (*Server) Amount

func (s *Server) Amount(room string) int

Get amount of channels, joined to given room, using server

func (*Server) BroadcastTo

func (s *Server) BroadcastTo(room, name string, payload interface{})

BroadcastTo the the given room an handler with payload, using server

func (*Server) BroadcastToAll

func (s *Server) BroadcastToAll(method string, payload interface{})

Broadcast to all clients

func (*Server) CountChannels

func (s *Server) CountChannels() int

CountChannels returns an amount of connected channels

func (*Server) CountRooms

func (s *Server) CountRooms() int

CountRooms returns an amount of rooms with at least one joined channel

func (*Server) GetChannel

func (s *Server) GetChannel(sid string) (*Channel, error)

GetChannel by it's sid

func (*Server) List

func (s *Server) List(room string) []*Channel

List returns a list of channels joined to the given room, using server

func (Server) On

func (e Server) On(name string, f interface{}) error

On registers message processing function and binds it to the given event name

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP makes Server to implement http.Handler

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL