Skip to content

Instantly share code, notes, and snippets.

@afcapel
Created December 16, 2021 10:58
Show Gist options
  • Save afcapel/4e1012bd658f6818e70115178edd3489 to your computer and use it in GitHub Desktop.
Save afcapel/4e1012bd658f6818e70115178edd3489 to your computer and use it in GitHub Desktop.
module main
go 1.17
require github.com/gorilla/websocket v1.4.2 // indirect
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"github.com/gorilla/websocket"
)
var addr = flag.String("addr", "localhost:8080", "http service address")
var upgrader = websocket.Upgrader{
EnableCompression: true,
}
func echo(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %d bytes", len(message))
err = c.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
break
}
}
}
func home(w http.ResponseWriter, r *http.Request) {
homeTemplate.Execute(w, "ws://"+r.Host+"/echo")
}
func main() {
flag.Parse()
log.SetFlags(0)
http.HandleFunc("/echo", echo)
http.HandleFunc("/", home)
log.Fatal(http.ListenAndServe(*addr, nil))
}
var longMessage = strings.Repeat("All work and no play makes Jack a dull boy\n", 1024*72)
var templateString = fmt.Sprintf(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
window.addEventListener("load", function(evt) {
var output = document.getElementById("output");
var input = document.getElementById("input");
var ws;
var print = function(message) {
var d = document.createElement("div");
d.textContent = message;
output.appendChild(d);
output.scroll(0, output.scrollHeight);
};
document.getElementById("open").onclick = function(evt) {
if (ws) {
return false;
}
ws = new WebSocket("{{.}}");
ws.onopen = function(evt) {
print("OPEN");
}
ws.onclose = function(evt) {
print("CLOSE");
ws = null;
}
ws.onmessage = function(evt) {
print("RECEIVED: " + evt.data.length + " chars");
}
ws.onerror = function(evt) {
print("ERROR: " + evt.data);
}
return false;
};
document.getElementById("send").onclick = function(evt) {
if (!ws) {
return false;
}
print("SEND: " + input.value.length + " chars");
ws.send(input.value);
return false;
};
document.getElementById("close").onclick = function(evt) {
if (!ws) {
return false;
}
ws.close();
return false;
};
});
</script>
</head>
<body>
<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>
<p>
<button id="open">Open</button>
</p>
<p>
<button id="close">Close</button>
</p>
<p>
<textarea id="input">%s</textarea>
</p>
<p>
<button id="send">Send</button>
</p>
</form>
<div id="output" style="max-height: 70vh;overflow-y: scroll;"></div>
</body>
</html>
`, longMessage)
var homeTemplate = template.Must(template.New("").Parse(templateString))
@afcapel
Copy link
Author

afcapel commented Dec 16, 2021

Run the server with go run server.go. When sending a long message over a compressed connection (using permessage-deflate) the websocket crashes, presumably because Safari is compressing just the first frame in a message, instead of the whole message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment