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

Minimal and idiomatic WebSocket library for Go with Chat Example

License

NotificationsYou must be signed in to change notification settings

neodigm/websocket

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

godoc

websocket is a minimal and idiomatic WebSocket library for Go.

Install

go get nhooyr.io/websocket

Features

Roadmap

Examples

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

For a full stack Chat example designed for chat rooms, see thechat example.It is designed for WSS so ensure your chat server is secured.

Here is an example command to get the chat server running locally on port 8080

go run. localhost:8080

Server

http.HandlerFunc(func (w http.ResponseWriter,r*http.Request) {c,err:=websocket.Accept(w,r,nil)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",nil)iferr!=nil {// ...}deferc.Close(websocket.StatusInternalError,"the sky is falling")err=wsjson.Write(ctx,c,"hi")iferr!=nil {// ...}c.Close(websocket.StatusNormalClosure,"")

Comparison

gorilla/websocket

Advantages ofgorilla/websocket:

Advantages of nhooyr.io/websocket:

golang.org/x/net/websocket

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

Thenet.Conn can help in transitioningto nhooyr.io/websocket.

gobwas/ws

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

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

About

Minimal and idiomatic WebSocket library for Go with Chat Example

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go98.7%
  • Other1.3%

[8]ページ先頭

©2009-2025 Movatter.jp