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 ¶
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 ¶
Close closes the underlying websocket connection. It sends a Close packet to the server before closing the connection.
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 )