Movatterモバイル変換


[0]ホーム

URL:


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

A minimal and idiomatic WebSocket library for Go

License

NotificationsYou must be signed in to change notification settings

EZJasonBoy/websocket-1

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoDocCodecov

websocket is a minimal and idiomatic WebSocket library for Go.

This library is not final and the API is subject to change.

If you have any feedback, please feel free to open an issue.

Install

go get nhooyr.io/websocket@v0.2.0

Features

  • Minimal yet pragmatic API
  • First class context.Context support
  • Thoroughly tested, fully passes theautobahn-testsuite
  • Concurrent writes
  • Zero dependencies outside of the stdlib for the core library
  • JSON and ProtoBuf helpers in the wsjson and wspb subpackages

Roadmap

  • WebSockets over HTTP/2#4
  • Deflate extension support#5

Examples

For a production quality example that shows off the full API, see theecho example on the godoc. On github, the example is atexample_echo_test.go.

Server

http.HandlerFunc(func (w http.ResponseWriter,r*http.Request) {c,err:=websocket.Accept(w,r, websocket.AcceptOptions{})iferr!=nil {// ...}deferc.Close(websocket.StatusInternalError,"the sky is falling")ctx,cancel:=context.WithTimeout(r.Context(),time.Second*10)defercancel()varvinterface{}err=wsjson.Read(ctx,c,&v)iferr!=nil {// ...}log.Printf("received: %v",v)c.Close(websocket.StatusNormalClosure,"")})

Client

ctx,cancel:=context.WithTimeout(context.Background(),time.Minute)defercancel()c,_,err:=websocket.Dial(ctx,"ws://localhost:8080", websocket.DialOptions{})iferr!=nil {// ...}deferc.Close(websocket.StatusInternalError,"the sky is falling")err=wsjson.Write(ctx,c,"hi")iferr!=nil {// ...}c.Close(websocket.StatusNormalClosure,"")

Design considerations

  • Minimal API is easier to maintain and learn
  • Context based cancellation is more ergonomic and robust than setting deadlines
  • No ping support because TCP keep alives work fine for HTTP/1.1 and they do not makesense with HTTP/2 (see#1)
  • net.Conn is never exposed as WebSocket over HTTP/2 will not have a net.Conn.
  • Using net/http's Client for dialing means we do not have to reinvent dialing hooksand configurations like other WebSocket libraries

Comparison

While I believe nhooyr/websocket has a better API than existing libraries,both gorilla/websocket and gobwas/ws were extremely useful in implementing theWebSocket protocol correctly sobig thanks to the authors of both. In particular,I made sure to go through the issue tracker of gorilla/websocket to make sureI implemented details correctly and understood how people were using the packagein production.

gorilla/websocket

https://github.com/gorilla/websocket

This package is the community standard but it is 6 years old and over timehas accumulated cruft. Using is not clear as there are many ways to do thingsand there are some rough edges. Just compare the godoc ofnhooyr/websocket side by side withgorilla/websocket.

The API for nhooyr/websocket has been designed such that there is only one way to do thingswhich makes it easy to use correctly.

Furthermore, nhooyr/websocket has support for newer Go idioms such as context.Context andalso uses net/http's Client and ResponseWriter directly for WebSocket handshakes.gorilla/websocket writes its handshakes to the underlying net.Conn which meansit has to reinvent hooks for TLS and proxying and prevents support of HTTP/2.

Another advantage of nhooyr/websocket is that it supports concurrent writers out of the box.

x/net/websocket

https://godoc.org/golang.org/x/net/websocket

Unmaintained and the API does not reflect WebSocket semantics. Should never be used.

Seegolang/go#18152

gobwas/ws

https://github.com/gobwas/ws

This library has an extremely flexible API but that comes at the cost of usabilityand clarity.

This library is fantastic in terms of performance. The author put in significanteffort to ensure its speed and I have applied as many of its optimizations asI could into nhooyr/websocket. Definitely check out his fantasticblog postabout performant WebSocket servers.

If you want a library that gives you absolute control over everything, this is the library,but for most users, the API provided by nhooyr/websocket will fit better as it is nearly justas performant but much easier to use correctly and idiomatic.

About

A minimal and idiomatic WebSocket library for Go

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go93.9%
  • Shell4.2%
  • Dockerfile1.5%
  • HCL0.4%

[8]ページ先頭

©2009-2025 Movatter.jp