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()
}
Output:
Index ¶
- func Dial(ctx context.Context, rawURL string, opts transport.DialOptions, h Handler, ...) error
- func HTTP(h Handler, opts handshake.Options, cfg ws.Config) http.Handler
- func Serve(h Handler, cfg ws.Config) func(net.Conn, handshake.Result, *transport.Request)
- type Base
- type Conn
- type Handler
- type PanicError
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.
Types ¶
type Conn ¶
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 ¶
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