wsutil

package
v0.0.0-...-6b20bf8 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2019 License: MIT, BSD-2-Clause Imports: 7 Imported by: 0

README

wsutil

Like net/http/httputil but for WebSockets.

GoDoc

A Reverse Proxy Example

package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"net/url"
	"time"

	"github.com/yhat/wsutil"
	"golang.org/x/net/websocket"
)

func main() {
	backend := ":8001"
	proxy := ":8002"

	// an webscket echo server
	backendHandler := websocket.Handler(func(ws *websocket.Conn) {
		io.Copy(ws, ws)
	})

	// make a proxy pointing at that backend url
	backendURL := &url.URL{Scheme: "ws://", Host: backend}
	p := wsutil.NewSingleHostReverseProxy(backendURL)

	// run both servers and give them a second to start up
	go http.ListenAndServe(backend, backendHandler)
	go http.ListenAndServe(proxy, p)
	time.Sleep(1 * time.Second)

	// connect to the proxy
	origin := "http://localhost/"
	ws, err := websocket.Dial("ws://"+proxy, "", origin)
	if err != nil {
		log.Fatal(err)
	}

	// send a message along the websocket
	msg := []byte("isn't yhat awesome?")
	if _, err := ws.Write(msg); err != nil {
		log.Fatal(err)
	}

	// read the response from the proxy
	resp := make([]byte, 4096)
	if n, err := ws.Read(resp); err != nil {
		log.Fatal(err)
	} else {
		fmt.Printf("%s\n", resp[:n])
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsWebSocketRequest

func IsWebSocketRequest(r *http.Request) bool

IsWebSocketRequest returns a boolean indicating whether the request has the headers of a WebSocket handshake request.

Types

type ReverseProxy

type ReverseProxy struct {
	// Director must be a function which modifies
	// the request into a new request to be sent
	// using Transport. Its response is then copied
	// back to the original client unmodified.
	Director func(*http.Request)

	// Dial specifies the dial function for dialing the proxied
	// server over tcp.
	// If Dial is nil, net.Dial is used.
	Dial func(network, addr string) (net.Conn, error)

	// TLSClientConfig specifies the TLS configuration to use for 'wss'.
	// If nil, the default configuration is used.
	TLSClientConfig *tls.Config

	// ErrorLog specifies an optional logger for errors
	// that occur when attempting to proxy the request.
	// If nil, logging goes to os.Stderr via the log package's
	// standard logger.
	ErrorLog *log.Logger
}

ReverseProxy is a WebSocket reverse proxy. It will not work with a regular HTTP request, so it is the caller's responsiblity to ensure the incoming request is a WebSocket request.

func NewSingleHostReverseProxy

func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy

NewSingleHostReverseProxy returns a new websocket ReverseProxy. The path rewrites follow the same rules as the httputil.ReverseProxy. If the target url has the path '/foo' and the incoming request '/bar', the request path will be updated to '/foo/bar' before forwarding. Scheme should specify if 'ws' or 'wss' should be used.

func (*ReverseProxy) ServeHTTP

func (p *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

Function to implement the http.Handler interface.

Jump to

Keyboard shortcuts

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