Skip to content

Commit

Permalink
changed variables for clarity and removed nonexistent method
Browse files Browse the repository at this point in the history
  • Loading branch information
datadius committed Mar 6, 2024
1 parent b2a86a1 commit 968cd7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
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

0 comments on commit 968cd7f

Please sign in to comment.