You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
Andrey Kovalev 7fe2f59f05
go mod tidy && go mod vendor
3 years ago
..
internal go mod tidy && go mod vendor 3 years ago
.gitignore go mod tidy && go mod vendor 3 years ago
LICENSE.txt go mod tidy && go mod vendor 3 years ago
README.md go mod tidy && go mod vendor 3 years ago
accept.go go mod tidy && go mod vendor 3 years ago
accept_js.go go mod tidy && go mod vendor 3 years ago
close.go go mod tidy && go mod vendor 3 years ago
close_notjs.go go mod tidy && go mod vendor 3 years ago
compress.go go mod tidy && go mod vendor 3 years ago
compress_notjs.go go mod tidy && go mod vendor 3 years ago
conn.go go mod tidy && go mod vendor 3 years ago
conn_notjs.go go mod tidy && go mod vendor 3 years ago
dial.go go mod tidy && go mod vendor 3 years ago
doc.go go mod tidy && go mod vendor 3 years ago
frame.go go mod tidy && go mod vendor 3 years ago
netconn.go go mod tidy && go mod vendor 3 years ago
read.go go mod tidy && go mod vendor 3 years ago
stringer.go go mod tidy && go mod vendor 3 years ago
write.go go mod tidy && go mod vendor 3 years ago
ws_js.go go mod tidy && go mod vendor 3 years ago

README.md

websocket

godoc coverage

websocket is a minimal and idiomatic WebSocket library for Go.

Install

go get nhooyr.io/websocket

Highlights

Roadmap

  • HTTP/2 #4

Examples

For a production quality example that demonstrates the complete API, see the echo example.

For a full stack example, see the chat example.

Server

http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
	c, err := websocket.Accept(w, r, nil)
	if err != nil {
		// ...
	}
	defer c.Close(websocket.StatusInternalError, "the sky is falling")

	ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
	defer cancel()

	var v interface{}
	err = wsjson.Read(ctx, c, &v)
	if err != nil {
		// ...
	}

	log.Printf("received: %v", v)

	c.Close(websocket.StatusNormalClosure, "")
})

Client

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)
if err != nil {
	// ...
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")

err = wsjson.Write(ctx, c, "hi")
if err != nil {
	// ...
}

c.Close(websocket.StatusNormalClosure, "")

Comparison

gorilla/websocket

Advantages of gorilla/websocket:

Advantages of nhooyr.io/websocket:

golang.org/x/net/websocket

golang.org/x/net/websocket is deprecated. See golang/go/issues/18152.

The net.Conn can help in transitioning to nhooyr.io/websocket.

gobwas/ws

gobwas/ws has an extremely flexible API that allows it to be used in an event driven style for performance. See the author's blog post.

However when writing idiomatic Go, nhooyr.io/websocket will be faster and easier to use.