- Notifications
You must be signed in to change notification settings - Fork329
Add automated test and documentation for echo example#224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletionci/test.mk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionconn_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionexamples/chat/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletionsexamples/chat/chat_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
package main | ||
import ( | ||
18 changes: 0 additions & 18 deletionsexamples/chat/go.sum
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
2 changes: 1 addition & 1 deletionexamples/chat/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletionsexamples/echo/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Echo Example | ||
This directory contains a echo server example using nhooyr.io/websocket. | ||
```bash | ||
$ cd examples/echo | ||
$ go run . localhost:0 | ||
listening on http://127.0.0.1:51055 | ||
``` | ||
You can use a WebSocket client like https://github.com/hashrocket/ws to connect. All messages | ||
written will be echoed back. | ||
## Structure | ||
The server is in `server.go` and is implemented as a `http.HandlerFunc` that accepts the WebSocket | ||
and then reads all messages and writes them exactly as is back to the connection. | ||
`server_test.go` contains a small unit test to verify it works correctly. | ||
`main.go` brings it all together so that you can run it and play around with it. |
161 changes: 31 additions & 130 deletionsexamples/echo/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletionsexamples/echo/server.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
"golang.org/x/time/rate" | ||
"nhooyr.io/websocket" | ||
) | ||
// echoServer is the WebSocket echo server implementation. | ||
// It ensures the client speaks the echo subprotocol and | ||
// only allows one message every 100ms with a 10 message burst. | ||
type echoServer struct { | ||
// logf controls where logs are sent. | ||
logf func(f string, v ...interface{}) | ||
} | ||
func (s echoServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{ | ||
Subprotocols: []string{"echo"}, | ||
}) | ||
if err != nil { | ||
s.logf("%v", err) | ||
return | ||
} | ||
defer c.Close(websocket.StatusInternalError, "the sky is falling") | ||
if c.Subprotocol() != "echo" { | ||
c.Close(websocket.StatusPolicyViolation, "client must speak the echo subprotocol") | ||
return | ||
} | ||
l := rate.NewLimiter(rate.Every(time.Millisecond*100), 10) | ||
for { | ||
err = echo(r.Context(), c, l) | ||
if websocket.CloseStatus(err) == websocket.StatusNormalClosure { | ||
return | ||
} | ||
if err != nil { | ||
s.logf("failed to echo with %v: %v", r.RemoteAddr, err) | ||
return | ||
} | ||
} | ||
} | ||
// echo reads from the WebSocket connection and then writes | ||
// the received message back to it. | ||
// The entire function has 10s to complete. | ||
func echo(ctx context.Context, c *websocket.Conn, l *rate.Limiter) error { | ||
ctx, cancel := context.WithTimeout(ctx, time.Second*10) | ||
defer cancel() | ||
err := l.Wait(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
typ, r, err := c.Reader(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
w, err := c.Writer(ctx, typ) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = io.Copy(w, r) | ||
if err != nil { | ||
return fmt.Errorf("failed to io.Copy: %w", err) | ||
} | ||
err = w.Close() | ||
return err | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.