websocketproxy

package module
v0.0.0-...-90a5336 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2018 License: MIT Imports: 9 Imported by: 0

README

WebsocketProxy GoDoc Build Status

WebsocketProxy is an http.Handler interface build on top of gorilla/websocket that you can plug into your existing Go webserver to provide WebSocket reverse proxy.

Install

go get github.com/button-chen/websocketproxy

Example

Below is a simple server that proxies to the given backend URL

package main

import (
	"flag"
	"net/http"
	"net/url"

	"github.com/button-chen/websocketproxy"
)

var (
	flagBackend = flag.String("backend", "", "Backend URL for proxying")
)

func main() {
	u, err := url.Parse(*flagBackend)
	if err != nil {
		log.Fataln(err)
	}
	proxy := websocketproxy.NewProxy()
	proxy.AddBackend(u)
	err := http.ListenAndServe(":80", proxy)
	if err != nil {
		log.Fataln(err)
	}
}

Save it as proxy.go and run as:

go run proxy.go -backend ws://example.com:3000

Now all incoming WebSocket requests coming to this server will be proxied to ws://example.com:3000

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

	// DefaultForwardMode default mode
	DefaultForwardMode = 0

	// RedirectForwardMode default mode
	RedirectForwardMode = 1
)

Functions

func ProxyHandler

func ProxyHandler() http.Handler

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

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.
	Backends []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

	ReqCount int

	DesolateBackend map[int]int

	//  ForwardMode if set 0 reverse mode, set 1 http redirect mode
	ForwardMode int
}

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

func NewProxy

func NewProxy() *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) AddBackend

func (w *WebsocketProxy) AddBackend(target *url.URL)

AddBackend append backend to proxy

func (*WebsocketProxy) ServeHTTP

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

ServeHTTP implements the http.Handler that proxies WebSocket connections.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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