Documentation
¶
Overview ¶
Package websocket provides a browser-compatible WebSocket client for the gojs JavaScript engine. Calling Install adds a global WebSocket class to a VM, implemented against a hand-written RFC 6455 client that runs the socket on its own goroutines and delivers open/message/error/close events back onto the VM event loop.
The package depends only on the Go standard library and the gojs embedding API; it pulls in no third-party WebSocket library and implements the framing protocol directly.
Scripting surface ¶
After Install, scripts use the familiar Web API:
const ws = new WebSocket("ws://localhost:8080/chat");
ws.binaryType = "arraybuffer";
ws.onopen = () => ws.send("hello");
ws.onmessage = (e) => console.log("recv", e.data);
ws.onclose = (e) => console.log("closed", e.code, e.reason, e.wasClean);
ws.addEventListener("error", (e) => console.log("error", e.message));
Browser differences ¶
gojs has no Blob type, so binary messages are always delivered as an ArrayBuffer regardless of binaryType, and binaryType defaults to "arraybuffer" (the DOM default is "blob"). Everything else — readyState constants, the handler properties, addEventListener/removeEventListener/dispatchEvent for the open/message/error/close types, and the close handshake with code/reason/ wasClean — matches the browser API.
Index ¶
- func Install(vm *gojs.VM, opts ...Option) error
- type MessageType
- type Option
- type ServerConn
- func (s *ServerConn) Close(code int, reason string) error
- func (s *ServerConn) CloseNow() error
- func (s *ServerConn) Ping(payload []byte) error
- func (s *ServerConn) ReadMessage() (MessageType, []byte, error)
- func (s *ServerConn) WriteFrame(fin bool, opcode byte, payload []byte) error
- func (s *ServerConn) WriteMessage(typ MessageType, data []byte) error
- type UpgradeConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type MessageType ¶
type MessageType int
MessageType identifies the kind of a WebSocket data message.
const ( // TextMessage is a UTF-8 text message (opcode 0x1). TextMessage MessageType = 1 // BinaryMessage is a binary message (opcode 0x2). BinaryMessage MessageType = 2 )
type Option ¶
type Option func(*options)
Option configures the WebSocket installation.
func WithCloseTimeout ¶
WithCloseTimeout bounds how long a client-initiated close waits for the peer to echo the Close frame before dropping the socket. The default is 5 seconds.
func WithHandshakeTimeout ¶
WithHandshakeTimeout bounds the time allowed to establish the TCP/TLS connection and complete the opening handshake. The default is 30 seconds.
func WithMaxFrameSize ¶
WithMaxFrameSize bounds the payload of a single incoming frame. A frame larger than the limit fails the connection with close code 1009. Zero means unlimited. The default is 16 MiB.
func WithMaxMessageSize ¶
WithMaxMessageSize bounds the total size of a reassembled (possibly fragmented) message. Exceeding it fails the connection with close code 1009. The default is 16 MiB.
func WithOrigin ¶
WithOrigin sets an Origin header sent with the opening handshake.
func WithTLSConfig ¶
WithTLSConfig sets the TLS configuration used for wss:// connections. When nil, a default configuration is used with ServerName derived from the URL.
type ServerConn ¶
type ServerConn struct {
Subproto string
// contains filtered or unexported fields
}
ServerConn is an accepted server-side WebSocket connection. Its methods are not safe for concurrent use by multiple goroutines except that Close may be called concurrently with Read/Write to unblock them.
func Upgrade ¶
func Upgrade(w http.ResponseWriter, r *http.Request, cfg UpgradeConfig) (*ServerConn, error)
Upgrade performs the server side of the RFC 6455 opening handshake on an incoming HTTP request, hijacks the connection, and returns a ServerConn. The caller owns the connection and must Close it.
func (*ServerConn) Close ¶
func (s *ServerConn) Close(code int, reason string) error
Close sends a Close frame with the given code and reason and then closes the transport.
func (*ServerConn) CloseNow ¶
func (s *ServerConn) CloseNow() error
CloseNow closes the transport without a Close frame, simulating an abrupt disconnect.
func (*ServerConn) Ping ¶
func (s *ServerConn) Ping(payload []byte) error
Ping sends a Ping frame with the given payload.
func (*ServerConn) ReadMessage ¶
func (s *ServerConn) ReadMessage() (MessageType, []byte, error)
ReadMessage reads the next complete data message, transparently answering Ping frames with a Pong and reassembling fragments. It returns (0, nil, err) when the peer sends a Close frame or the connection ends; the error is io.EOF-like or a *wsError describing a protocol violation.
func (*ServerConn) WriteFrame ¶
func (s *ServerConn) WriteFrame(fin bool, opcode byte, payload []byte) error
WriteFrame writes a single raw frame (unmasked, as a server must). It exposes FIN/opcode so callers — including tests — can send fragments or malformed frames deliberately.
func (*ServerConn) WriteMessage ¶
func (s *ServerConn) WriteMessage(typ MessageType, data []byte) error
WriteMessage sends a complete unfragmented data message.
type UpgradeConfig ¶
type UpgradeConfig struct {
// Subprotocols lists protocols the server supports, in preference order. The
// first client-offered protocol that appears here is selected.
Subprotocols []string
// MaxFrameSize and MaxMessageSize bound incoming frames/messages (0 =
// unlimited / 16 MiB default respectively).
MaxFrameSize int
MaxMessageSize int
}
UpgradeConfig tunes an Upgrade. The zero value is valid.