websocket

package
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Pine's websocket package is a websocket server that supports multiple channels This feature is experimental and may change in the future. Please use it with caution and at your own risk.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(handler func(conn *Conn, ctx *pine.Ctx), config ...Config) pine.Handler

Called to open a new connection and upgrade it to a websocket connection this is the main function to use to create a new websocket connection Use this function if the Type is set to "self"

func Watch added in v1.1.4

func Watch(dir string, done <-chan struct{}, onChange func(absPath string)) error

Watch monitors dir recursively for file-system changes and calls onChange with the absolute path of each changed file. It blocks until done is closed or the underlying watcher fails.

New subdirectories created after the watch starts are added automatically. Watch is the shared primitive used by both WatchFolder (streaming) and render.LiveReload (template hot-reload).

func WatchFile

func WatchFile(path string, conn *Conn) error

WatchFile streams a single file's changes to conn over WebSocket.

On connection it sends up to maxFileSize bytes from the tail of the file as initial content, then streams every new byte appended afterwards.

The function blocks until the client disconnects or a fatal write error occurs. The underlying watcher is always closed before returning.

func WatchFolder added in v1.1.4

func WatchFolder(dir string, conn *Conn) error

WatchFolder watches all files inside a directory (and its subdirectories) for write events and streams newly appended bytes to conn.

Each WebSocket message is a JSON-encoded FolderMessage:

{"path": "logs/app.log", "content": "new bytes appended since last read"}

New subdirectories created after the watch starts are automatically watched. The function blocks until the client disconnects or a fatal write error occurs.

Types

type Config

type Config struct {
	// ReadBufferSize and WriteBufferSize specify I/O buffer sizes in bytes. If a buffer
	// size is zero, then buffers allocated by the HTTP server are used. The
	// I/O buffer sizes do not limit the size of the messages that can be sent
	// or received.
	ReadBufferSize, WriteBufferSize int

	// Subprotocols specifies the server's supported protocols in order of
	// preference. If this field is not nil, then the Upgrade method negotiates a
	// subprotocol by selecting the first match in this list with a protocol
	// requested by the client. If there's no match, then no protocol is
	// negotiated (the Sec-Websocket-Protocol header is not included in the
	// handshake response).
	SubprotocolsAllowed []string

	// CheckOrigin returns true if the request Origin header is acceptable. If
	// CheckOrigin is nil, then a safe default is used: return false if the
	// Origin request header is present and the origin host is not equal to
	// request Host header.
	//
	// A CheckOrigin function should carefully validate the request origin to
	// prevent cross-site request forgery.
	CheckOrigin func(r *http.Request) bool

	// Error specifies the function for generating HTTP error responses. If Error
	// is nil, then http.Error is used to generate the HTTP response.
	Error func(w http.ResponseWriter, r *http.Request, status int, reason error)

	// EnableCompression specify if the server should attempt to negotiate per
	// message compression (RFC 7692). Setting this value to true does not
	// guarantee that compression will be supported. Currently only "no context
	// takeover" modes are supported.
	EnableCompression bool

	// HandshakeTimeout specifies the duration for the handshake to complete.
	HandshakeTimeout time.Duration
}

Config is a struct that holds the configuration for the websocket server

type Conn

type Conn struct {
	*websocket.Conn
	// contains filtered or unexported fields
}

Conn is a struct that holds the websocket connection

type FolderMessage added in v1.1.4

type FolderMessage struct {
	Path    string `json:"path"`    // relative path within the watched directory
	Content string `json:"content"` // newly appended bytes
}

FolderMessage is the JSON envelope sent by WatchFolder for every file-change event. The client uses Path to know which file was updated and Content for the new bytes that were appended since the last read.

Jump to

Keyboard shortcuts

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