events

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package events serves connections through a Handler interface instead of a read loop: a thin layer over transport and ws.Serve.

Example
package main

import (
	"log"
	"net"
	"time"

	"github.com/33TU/ews/codec"
	"github.com/33TU/ews/events"
	"github.com/33TU/ews/transport"
	"github.com/33TU/ews/ws"
)

// echo is a gws-style handler, one instance shared by every connection. A
// pong from the peer refreshes the deadline set on open: the usual
// keepalive, with pings from one ticker elsewhere. Base answers pings.
type echo struct{ events.Base }

func (echo) OnOpen(c *events.Conn) {
	log.Printf("%s connected to %s", c.Request.RemoteAddr, c.Request.Path)
	c.SetReadDeadline(time.Now().Add(time.Minute))
}

func (echo) OnMessage(c *events.Conn, op codec.Opcode, payload []byte) error {
	return c.Write(op, payload)
}

func (echo) OnPong(c *events.Conn, _ []byte) error {
	return c.SetReadDeadline(time.Now().Add(time.Minute))
}

func (echo) OnClose(c *events.Conn, err error) { log.Printf("closed: %v", err) }

func main() {
	server := &transport.Server{Handler: events.Serve(echo{}, ws.Config{ValidateUTF8: true})}
	ln, _ := net.Listen("tcp", "127.0.0.1:0")
	go server.Serve(ln)
	server.Close()
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial added in v0.4.0

func Dial(ctx context.Context, rawURL string, opts transport.DialOptions, h Handler, cfg ws.Config) error

Dial connects to rawURL, runs h until the connection ends and closes the transport. It returns the error OnClose received, or the dial error. cfg.UserData is set before OnOpen.

func HTTP

func HTTP(h Handler, opts handshake.Options, cfg ws.Config) http.Handler

HTTP returns an http.Handler that upgrades each request and runs h on a goroutine of its own, so net/http frees the request.

func Serve

func Serve(h Handler, cfg ws.Config) func(net.Conn, handshake.Result, *transport.Request)

Serve returns a transport.Server handler that runs h on each connection. cfg's Role, Compression and ControlHandler are set per connection.

Types

type Base

type Base struct{}

Base is a Handler that answers pings and ignores the rest.

func (Base) OnClose

func (Base) OnClose(*Conn, error)

func (Base) OnOpen

func (Base) OnOpen(*Conn)

func (Base) OnPing

func (Base) OnPing(c *Conn, payload []byte) error

func (Base) OnPong

func (Base) OnPong(*Conn, []byte) error

type Conn

type Conn struct {
	*ws.Conn
	Request transport.Request
}

Conn is the connection the events receive: the WebSocket with its deadlines, addresses and UserData for the handler's per-connection state, and the upgrade request.

type Handler

type Handler interface {
	OnOpen(c *Conn)
	OnMessage(c *Conn, op codec.Opcode, payload []byte) error
	OnPing(c *Conn, payload []byte) error // Must send the pong.
	OnPong(c *Conn, payload []byte) error
	OnClose(c *Conn, err error)
}

Handler receives a connection's events on its read goroutine. Payloads are borrowed until the call returns. An error from an event ends the connection and reaches OnClose, which also receives the *ws.CloseError of a peer's close and the *PanicError of a panic in another event.

One Handler serves every connection; per-connection state goes in Conn.UserData. Embed Base for the defaults.

type PanicError

type PanicError struct {
	Value any
	Stack []byte
}

PanicError is the error OnClose receives when another event panicked: the connection ends, the process does not. OnClose may re-panic to restore that behavior.

func (*PanicError) Error

func (e *PanicError) Error() string

Jump to

Keyboard shortcuts

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