coder

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

This package enables the use of the github.com/coder/websocket package with the ws-server-wrapper library. It provides an adapter between a Conn from the github.com/coder/websocket package and a Conn in the ws-server-wrapper library.

Example
// Create ws-wrapper-server and "echo" event handler. Note that the event
// handler function may optionally have a context as its first argument,
// it must always return 2 values, and the second value must implement
// error.
wsServer := wrapper.NewServer()
wsServer.On("echo", func(s string) (string, error) {
	// If a ws-server client sends a "echo" request, it will receive the
	// echoed response.
	return s, nil
})

// Create HTTP handler function that upgrades the HTTP connection to a
// WebSocket using the github.com/coder/websocket package.
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{
		// options go here (i.e. cross-origin setting)...
	})
	if err != nil {
		// websocket.Accept already writes the HTTP response
		return
	}
	// Attach the WebSocket connection to ws-server-wrapper and start
	// listening for inbound messages.
	err = wsServer.Accept(Wrap(conn))
	if err != nil {
		// wsServer.Accept already closes the conn
		return
	}
})

// Start the HTTP server
log.Fatal(http.ListenAndServe("localhost:8080", h))

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wrap

func Wrap(c *websocket.Conn) wrapper.Conn

Wrap wraps a websocket.Conn from github.com/coder/websocket as a wrapper.Conn that can be passed to the wrapper.Server.Accept method.

Types

This section is empty.

Jump to

Keyboard shortcuts

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