socketio

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package socketio provides a Go implementation of the Socket.IO v2 (Engine.IO v3) protocol. This implementation is a minimalistic version of the protocol, focusing on the Chzzk use case.

Socket.IO consists of three layers:

  • Application layer: Socket.IO protocol
  • Transport Managing layer: Engine.IO protocol
  • Transport layer: WebSocket protocol (canonical protocol for Socket.IO v2)

The actual transport could be any transport such as Websocket, HTTP long-polling, etc. However, this implementation only supports WebSocket as the transport layer, which is the only required transport for accessing Chzzk session API.

Index

Constants

View Source
const (
	SocketIOVersion = 2
	EngineIOVersion = 3
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

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

Conn represents a Socket.IO connection over WebSocket. By the specification, a packet number 42 is a event packet, which is the only packet type that carries application data.

Following is the example of packet 42.

42["SYSTEM",{"type":"connected","data":{"sessionKey":"xyz789"}}]

Since this packet is for application level, the optional handlers can be registered to handle specific events. For example, You can register a handler for the "CHAT" event like this:

conn := socketio.New(url)
conn.WithHandler("SYSTEM", func(p []byte) error {
    fmt.Println("Received SYSTEM event:", string(p))

.     var data struct {
	        Type string `json:"type"`
	        Data struct {
	            SessionKey string `json:"sessionKey"`
	        } `json:"data"`
	    }
	    if err := json.Unmarshal(p, &data); err != nil {
	        return fmt.Errorf("failed to unmarshal SYSTEM event: %w", err)
	    }
	    fmt.Println("Session Key:", data.Data.SessionKey)
	    return nil
	})

>> Note: The event name is case-sensitive. "CHAT" and "chat" are different events.

func New

func New(url string, opts ...ConnOption) *Conn

New creates a new Socket.IO connection with the given URL and optional connection options. This does not establish the connection yet. You need to call Dial() to establish the connection and handshake.

func (*Conn) Close

func (c *Conn) Close(ctx context.Context, status ws.StatusCode, reason string) error

Close closes the underlying websocket connection. It sends a Close packet to the server before closing the connection.

func (*Conn) Dial

func (c *Conn) Dial(ctx context.Context) error

Dial establishes a websocket connection and handshake in Socket.IO protocol. It embeds a Websocket connection to Conn object if both connection and handshake are successful.

func (*Conn) Loop

func (c *Conn) Loop(ctx context.Context) error

Loop reads messages from the websocket connection and decodes them into Socket.IO packets. It also handles ping/pong messages and invokes the registered event handlers for the decoded packets.

type ConnOption

type ConnOption func(*Conn)

ConnOption is a function type that modifies the Conn struct.

func WithHTTPClient

func WithHTTPClient(c *http.Client) ConnOption

func WithHandler

func WithHandler(pattern string, handler func([]byte) error) ConnOption

type EnginePacketType

type EnginePacketType int
const (
	// This None value is a sentinel value for the zero value of EnginePacketType.
	// It is not a valid EnginePacketType and should not be used in any other context.
	EngineNone EnginePacketType = iota - 1

	Open
	Close

	// from server to client
	Ping

	// from client to server
	Pong
	Message
	Upgrade
	Noop
)

type Packet

type Packet struct {
	EnginePacketType EnginePacketType
	SocketPacketType SocketPacketType
	Body             []byte
}

type SocketPacketType

type SocketPacketType int
const (
	// This None value is a sentinel value for the zero value of SocketPacketType.
	// It is not a valid SocketPacketType and should not be used in any other context.
	SocketNone SocketPacketType = iota - 1

	Connect
	Disconnect

	Event
	Ack
	Error

	BinaryEvent
	BinaryAck
)

Jump to

Keyboard shortcuts

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