Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

yhat/wsutil

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

13 Commits

Repository files navigation

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])
	}
}

About

Go WebSocket reverse proxy

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

Languages

AltStyle によって変換されたページ (->オリジナル) /