socketio

package module
v4.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: BSD-3-Clause Imports: 17 Imported by: 0

README

go-socket.io

GoDoc Build Status Go Report Card

go-socket.io is library an implementation of Socket.IO in Golang, which is a realtime application framework.

Current this library supports 1.4 version of the Socket.IO client. It supports room, namespaces and broadcast at now.

Help wanted This project is looking for contributors to help fix bugs and implement new features. Please check Issue 192. All help is much appreciated.

Contents

Install

Install the package with:

go get github.com/thisismz/go-socket.io/v4

Import it with:

import "github.com/thisismz/go-socket.io/v4"

and use socketio as the package name inside the code.

Example

Please check more examples into folder in project for details. Examples

FAQ

It is some popular questions about this repository:

  • Is this library supported socket.io version 2?
    • No, but if you wanna you can help to do it. Join us in community chat Telegram
  • How to use go-socket.io with CORS?
  • What is minimal version Golang support for this library?
    • We required Go 1.9 or upper!
  • How to user?
    • Go-socket.io compatibility with Socket.IO 0.9.x, please use branch 0.9.x * or tag go-socket.io@v0.9.1

Community

Telegram chat: @go_socketio

Engineio

This project contains a sub-package called engineio. This used to be a separate package under https://github.com/googollee/go-engine.io.

It contains the engine.io analog implementation of the original node-package. https://github.com/socketio/engine.io It can be used without the socket.io-implementation. Please check the README.md in engineio/.

License

The 3-clause BSD License - see LICENSE for more details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConn

func NewConn(
	engineConn engineio.Conn,
	handlers *Handlers,
) *conn

Types

type Broadcaster

type Broadcaster interface {
	Join(room string, connection Conn)            // Join causes the connection to join a room
	Leave(room string, connection Conn)           // Leave causes the connection to leave a room
	LeaveAll(connection Conn)                     // LeaveAll causes given connection to leave all rooms
	Clear(room string)                            // Clear causes removal of all connections from the room
	Send(room, event string, args ...interface{}) // Send will send an event with args to the room
	SendAll(event string, args ...interface{})    // SendAll will send an event with args to all the rooms
	ForEach(room string, f EachFunc)              // ForEach sends data by DataFunc, if room does not exits sends nothing
	Len(room string) int                          // Len gives number of connections in the room
	Rooms(connection Conn) []string               // Gives list of all the rooms if no connection given, else list of all the rooms the connection joined
	AllRooms() []string                           // Gives list of all the rooms the connection joined
}

Broadcaster is the adaptor to handle broadcasts & rooms for socket.io server API

type Conn

type Conn interface {
	io.Closer
	Namespace

	// ID returns session id
	ID() string
	URL() url.URL
	LocalAddr() net.Addr
	RemoteAddr() net.Addr
	RemoteHeader() http.Header
	Serve()
}

Conn is a connection in go-socket.io

type EachFunc

type EachFunc func(Conn)

EachFunc typed for each callback function

type Handler

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

Handler contains all logics for working with connections

func NewHandler

func NewHandler(nsp string, adapterOpts *RedisAdapterConfig) *Handler

func (*Handler) Clear

func (nh *Handler) Clear(room string) bool

func (*Handler) ForEach

func (nh *Handler) ForEach(room string, f EachFunc) bool

func (*Handler) Join

func (nh *Handler) Join(room string, conn Conn) bool

func (*Handler) Leave

func (nh *Handler) Leave(room string, conn Conn) bool

func (*Handler) LeaveAll

func (nh *Handler) LeaveAll(conn Conn) bool

func (*Handler) Len

func (nh *Handler) Len(room string) int

func (*Handler) OnConnect

func (nh *Handler) OnConnect(f OnConnectHandler)

func (*Handler) OnDisconnect

func (nh *Handler) OnDisconnect(f OnDisconnectHandler)

func (*Handler) OnError

func (nh *Handler) OnError(f OnErrorHandler)

func (*Handler) OnEvent

func (nh *Handler) OnEvent(event string, f interface{})

func (*Handler) Rooms

func (nh *Handler) Rooms(conn Conn) []string

func (*Handler) Send

func (nh *Handler) Send(room string, event string, args ...interface{}) bool

func (*Handler) SendAll

func (nh *Handler) SendAll(event string, args ...interface{}) bool

type Handlers

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

func NewHandlers

func NewHandlers() *Handlers

func (*Handlers) Get

func (h *Handlers) Get(nsp string) (*Handler, bool)

func (*Handlers) Set

func (h *Handlers) Set(namespace string, handler *Handler)

type Namespace

type Namespace interface {
	// Context of this connection. You can save one context for one
	// connection, and share it between all handlers. The handlers
	// are called in one goroutine, so no need to lock context if it
	// only accessed in one connection.
	Context() context.Context
	SetContext(ctx context.Context)

	Namespace() string
	Emit(eventName string, v ...interface{})
	EmitByNameSpace(namespace, eventName string, v ...interface{})
	Join(room string)
	Leave(room string)
	LeaveAll()
	Rooms() []string
	Refuse(err error) error
}

Namespace describes a communication channel that allows you to split the logic of your application over a single shared connection.

type OnConnectHandler

type OnConnectHandler func(Conn, map[string]interface{}) error

type OnDisconnectHandler

type OnDisconnectHandler func(Conn, string, map[string]interface{})

type OnErrorHandler

type OnErrorHandler func(Conn, error)

type RedisAdapterConfig

type RedisAdapterConfig struct {
	Addr     string
	Prefix   string
	Network  string
	Password string
	DB       int
}

RedisAdapterConfig is configuration to create new adapter

func GetOptions

func GetOptions(opts *RedisAdapterConfig) *RedisAdapterConfig

type Server

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

Server is a go-socket.io server.

func NewServer

func NewServer(opts *engineio.Options) *Server

NewServer returns a server.

func (*Server) Adapter

func (s *Server) Adapter(opts *RedisAdapterConfig) (bool, error)

Adapter sets redis broadcast adapter.

func (*Server) BroadcastToNamespace

func (s *Server) BroadcastToNamespace(namespace string, event string, args ...interface{}) bool

BroadcastToNamespace broadcasts given event & args to all the connections in the same

func (*Server) BroadcastToRoom

func (s *Server) BroadcastToRoom(namespace string, room, event string, args ...interface{}) bool

BroadcastToRoom broadcasts given event & args to all the connections in the room.

func (*Server) ClearRoom

func (s *Server) ClearRoom(namespace string, room string) bool

ClearRoom clears the room.

func (*Server) Close

func (s *Server) Close() error

Close closes server.

func (*Server) Count

func (s *Server) Count() int

Count number of connections.

func (*Server) ForEach

func (s *Server) ForEach(namespace string, room string, f EachFunc) bool

ForEach sends data by DataFunc, if room does not exit sends anything.

func (*Server) JoinRoom

func (s *Server) JoinRoom(namespace string, room string, conn Conn) bool

JoinRoom joins given connection to the room.

func (*Server) LeaveAllRooms

func (s *Server) LeaveAllRooms(namespace string, conn Conn) bool

LeaveAllRooms leaves the given connection from all rooms.

func (*Server) LeaveRoom

func (s *Server) LeaveRoom(namespace string, room string, conn Conn) bool

LeaveRoom leaves given connection from the room.

func (*Server) OnConnect

func (s *Server) OnConnect(namespace string, f OnConnectHandler)

OnConnect set a handler function f to handle open event for

func (*Server) OnDisconnect

func (s *Server) OnDisconnect(namespace string, f OnDisconnectHandler)

OnDisconnect set a handler function f to handle disconnect event for

func (*Server) OnError

func (s *Server) OnError(namespace string, f OnErrorHandler)

OnError set a handler function f to handle error for

func (*Server) OnEvent

func (s *Server) OnEvent(namespace string, event string, f interface{})

OnEvent set a handler function f to handle event for

func (*Server) RoomLen

func (s *Server) RoomLen(namespace string, room string) int

RoomLen gives number of connections in the room.

func (*Server) Rooms

func (s *Server) Rooms(namespace string) []string

Rooms gives list of all the rooms.

func (*Server) Serve

func (s *Server) Serve() error

Serve serves go-socket.io server.

func (*Server) ServeHTTP

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

ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.

Jump to

Keyboard shortcuts

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