websocket

package
v0.0.0-...-cb5eaf0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package websocket is a lightweight client implementation of the WebSocket protocol (RFC 6455). It is designed specifically for fast, efficient and idiomatic client-side communication with Chrome DevTools in Blink-based browsers.

Examples for unsupported features: server-side functionality, proxies, TLS for "wss://" addresses, and handshake customizations such as sub-protocols, extensions, and other HTTP headers (e.g. authentication, cookies).

Specifically, this implementation does not support the "permessage-deflate" extension (RFC 7692), for the same reasons it doesn't support TLS: almost all communication with Chrome DevTools should happen on the same localhost, and most transactions involve very small amounts of data, so the security benefits of TLS and performance benefits of compression become irrelevant in this use-case, and are in fact wasteful and unnecessarily slow.

More readaing materials about other WebSocket implementations:

https://centrifugal.github.io/centrifugo/blog/scaling_websocket/

https://www.freecodecamp.org/news/million-websockets-and-go-cc58418460bb/

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

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

Conn defines a WebSocket connection with a buffered I/O interface.

func Handshake

func Handshake(ctx context.Context, addr, path string) (*Conn, error)

Handshake initiates a WebSocket connection with "ws://addr/path", based on https://datatracker.ietf.org/doc/html/rfc6455#section-4.1.

func NewConn

func NewConn(c net.Conn) *Conn

NewConn initializes a WebSocket connection based on an open TCP connection. This does not initiate a WebSocket handshake, nor do you need to call it directly. Instead, call the `Handshake` function.

func (*Conn) Close

func (c *Conn) Close(statusCode uint16, reason []byte) error

Close sends a "connection close" control frame to a WebSocket server, and then closes the underlying TCP network connection. Valid status codes are described in https://datatracker.ietf.org/doc/html/rfc6455#section-7.4.1, and the reason is a UTF-8-encoded string which may be empty. Based on https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.1 and https://datatracker.ietf.org/doc/html/rfc6455#section-7.

func (*Conn) Read

func (c *Conn) Read() ([]byte, error)

Read receives a full message from a WebSocket server. It handles all the implementation details internally, such as frame de/fragmentation, masking, and handling control frames.

func (*Conn) WriteBinary

func (c *Conn) WriteBinary(msg []byte) error

WriteBinary sends a full binary text message to a WebSocket server. It handles all the implementation details internally, such as frame de/fragmentation, masking, and handling control frames.

func (*Conn) WritePing

func (c *Conn) WritePing(appData []byte) error

WritePing sends a "ping" control frame to a WebSocket server, as a keep-alive or as a means to verify that the server is still responsive. Based on https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.2.

func (*Conn) WritePong

func (c *Conn) WritePong(appData []byte) error

WritePong sends a "pong" control frame to a WebSocket server, as a response to a "ping" (with the ping's "application data" payload), or as an unsolicited unidirectional heartbeat. Based on https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.3.

func (*Conn) WriteText

func (c *Conn) WriteText(msg []byte) error

WriteText sends a full UTF-8 text message to a WebSocket server. It handles all the implementation details internally, such as frame de/fragmentation, masking, and handling control frames.

Jump to

Keyboard shortcuts

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