websocketproxy

package
v0.0.0-...-098c817 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package websocketproxy is a reverse proxy for WebSocket connections.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultUpgrader specifies the parameters for upgrading an HTTP
	// connection to a WebSocket connection.
	DefaultUpgrader = &websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	}

	// DefaultDialer is a dialer with all fields set to the default zero values.
	DefaultDialer = websocket.DefaultDialer
)

Functions

func Decode

func Decode(payload []byte) (string, []byte, error)

Decode a message received from a websocket channel.

func Encode

func Encode(msgType string, payload []byte) []byte

Encode a message to be sent over a websocket channel

func InArray

func InArray(item string, array []string) bool

func MarshalJSON

func MarshalJSON(msg proto.Message) ([]byte, error)

MarshalJSON is a wrapper to serialize proto messages with JSON.

func ProxyHandler

func ProxyHandler(target *url.URL) http.Handler

ProxyHandler returns a new http.Handler interface that reverse proxies the request to the given target.

func UnmarshalJSON

func UnmarshalJSON(b []byte, msg proto.Message) error

UnmarshalJSON is a wrapper to deserialize proto messages with JSON.

Types

type WebsocketProxy

type WebsocketProxy struct {
	// Director, if non-nil, is a function that may copy additional request
	// headers from the incoming WebSocket connection into the output headers
	// which will be forwarded to another server.
	Director func(incoming *http.Request, out http.Header)

	// Backend returns the backend URL which the proxy uses to reverse proxy
	// the incoming WebSocket connection. Request is the initial incoming and
	// unmodified request.
	Backend func(*http.Request) *url.URL

	// Upgrader specifies the parameters for upgrading a incoming HTTP
	// connection to a WebSocket connection. If nil, DefaultUpgrader is used.
	Upgrader *websocket.Upgrader

	//  Dialer contains options for connecting to the backend WebSocket server.
	//  If nil, DefaultDialer is used.
	Dialer        *websocket.Dialer
	MarshalFunc   func(pb proto.Message) ([]byte, error)
	UnmarshalFunc func(buf []byte, pb proto.Message) error
}

WebsocketProxy is an HTTP Handler that takes an incoming WebSocket connection and proxies it to another server.

func NewProxy

func NewProxy(target *url.URL) *WebsocketProxy

NewProxy returns a new Websocket reverse proxy that rewrites the URL's to the scheme, host and base path provider in target.

func (*WebsocketProxy) ServeHTTP

func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler that proxies WebSocket connections.

Jump to

Keyboard shortcuts

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