Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/echo changed variables for clarity and removed nonexistent method #902

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions examples/echo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ func main() {
go func() {
defer close(done)
for {
mt, message, err := c.ReadMessage()
messageType, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
log.Printf("recv: %s, type: %s", message, websocket.FormatMessageType(mt))
if messageType == websocket.TextMessage {
log.Printf("received text message: %s", message)
} else if messageType == websocket.BinaryMessage {
log.Printf("received binary message: %s", message)
}
}
}()

Expand All @@ -68,7 +72,10 @@ func main() {

// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
err := c.WriteMessage(
websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
)
if err != nil {
log.Println("write close:", err)
return
Expand Down
16 changes: 10 additions & 6 deletions examples/echo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ func echo(w http.ResponseWriter, r *http.Request) {
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
messageType, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}

log.Printf("recv: %s, type: %s", message, websocket.FormatMessageType(mt))
err = c.WriteMessage(mt, message)
if messageType == websocket.TextMessage {
log.Printf("received text message: %s", message)
} else if messageType == websocket.BinaryMessage {
log.Printf("received binary message: %s", message)
}
err = c.WriteMessage(messageType, message)
if err != nil {
log.Println("write:", err)
break
Expand All @@ -60,7 +64,7 @@ var homeTemplate = template.Must(template.New("").Parse(`
<html>
<head>
<meta charset="utf-8">
<script>
<script>
window.addEventListener("load", function(evt) {

var output = document.getElementById("output");
Expand Down Expand Up @@ -118,8 +122,8 @@ window.addEventListener("load", function(evt) {
<body>
<table>
<tr><td valign="top" width="50%">
<p>Click "Open" to create a connection to the server,
"Send" to send a message to the server and "Close" to close the connection.
<p>Click "Open" to create a connection to the server,
"Send" to send a message to the server and "Close" to close the connection.
You can change the message and send multiple times.
<p>
<form>
Expand Down
Loading