socketio

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodePacket

func EncodePacket(p *Packet) ([]byte, error)

EncodePacket serializes a packet to JSON bytes.

Types

type ClientSocket

type ClientSocket struct {
	*Socket
}

ClientSocket represents a client-side Socket.IO event connection.

func DialSocket

func DialSocket(urlStr string, requestHeader http.Header) (*ClientSocket, error)

DialSocket connects to a Socket.IO WebSocket server.

type EventHandler

type EventHandler func(data []byte) (any, error)

EventHandler handles an incoming event. It may return a response payload or error for ACKs.

type Packet

type Packet struct {
	Type  PacketType      `json:"type"`
	Event string          `json:"event,omitempty"`
	Data  json.RawMessage `json:"data,omitempty"`
	AckID uint64          `json:"ack_id,omitempty"`
	Error string          `json:"error,omitempty"`
}

Packet represents a Socket.IO event or ACK payload.

func DecodePacket

func DecodePacket(data []byte) (*Packet, error)

DecodePacket deserializes JSON bytes into a packet.

type PacketType

type PacketType byte

PacketType defines the type of Socket.IO packet.

const (
	PacketEvent PacketType = 1
	PacketAck   PacketType = 2
)

type RoomBroadcaster

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

RoomBroadcaster handles room emissions.

func (*RoomBroadcaster) Emit

func (rb *RoomBroadcaster) Emit(event string, data any) error

Emit sends an event to all sockets in the specified room.

type Server

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

Server manages Socket.IO event-driven connections.

func NewServer

func NewServer(cfg zsocket.Config) *Server

NewServer creates a new Socket.IO event server with default settings.

func (*Server) OnConnection

func (srv *Server) OnConnection(fn func(s *Socket))

OnConnection registers a callback for new client connections.

func (*Server) ServeHTTP

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

ServeHTTP handles incoming HTTP websocket upgrade requests.

func (*Server) To

func (srv *Server) To(room string) *ServerRoomBroadcaster

To returns a ServerRoomBroadcaster for global room broadcasts.

type ServerRoomBroadcaster

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

ServerRoomBroadcaster broadcasts events to rooms from the server instance.

func (*ServerRoomBroadcaster) Emit

func (srb *ServerRoomBroadcaster) Emit(event string, data any) error

Emit broadcasts an event to all sockets in the room.

type Socket

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

Socket represents an event-driven WebSocket client connection.

func (*Socket) BroadcastTo

func (s *Socket) BroadcastTo(room string) *RoomBroadcaster

BroadcastTo returns a RoomBroadcaster for sending to a room, excluding this socket.

func (*Socket) Close

func (s *Socket) Close() error

Close closes the socket connection.

func (*Socket) Conn

func (s *Socket) Conn() *zsocket.Conn

Conn returns the underlying *zsocket.Conn.

func (*Socket) Emit

func (s *Socket) Emit(event string, data any) error

Emit sends an event to the peer without waiting for an ACK.

func (*Socket) EmitWithAck

func (s *Socket) EmitWithAck(ctx context.Context, event string, data any) ([]byte, error)

EmitWithAck sends an event and waits for the peer to acknowledge/reply.

func (*Socket) ID

func (s *Socket) ID() string

ID returns the unique socket identifier.

func (*Socket) Join

func (s *Socket) Join(room string)

Join subscribes this socket to a room.

func (*Socket) Leave

func (s *Socket) Leave(room string)

Leave unsubscribes this socket from a room.

func (*Socket) On

func (s *Socket) On(eventName string, handler EventHandler)

On registers an event listener for eventName.

func (*Socket) To

func (s *Socket) To(room string) *RoomBroadcaster

To returns a RoomBroadcaster for sending messages to a room.

Jump to

Keyboard shortcuts

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