Skip to content

Commit 41f7465

Browse files
Workaround Safari Websocket Compression issue (#28)
See: coder/websocket#218
1 parent fbfacf2 commit 41f7465

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

example/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ n.on('ready', () => {
5252
const el = document.getElementById('lobbies')
5353
if (el !== null) {
5454
el.innerHTML = ''
55-
if (lobbies.length === 0) {
55+
if (lobbies === null || lobbies.length === 0) {
5656
const li = document.createElement('li')
5757
li.innerHTML = '<i>no lobbies</i>'
5858
el.appendChild(li)

internal/signaling/handler.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"net/http"
7+
"strings"
78
"time"
89

910
"github.com/koenbollen/logging"
@@ -18,11 +19,6 @@ import (
1819
const MaxConnectionTime = 1 * time.Hour
1920

2021
func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.CredentialsClient) http.HandlerFunc {
21-
acceptOptions := &websocket.AcceptOptions{
22-
// Allow any origin/game to connect.
23-
InsecureSkipVerify: true,
24-
}
25-
2622
manager := &TimeoutManager{}
2723
go manager.Run(ctx)
2824

@@ -34,6 +30,17 @@ func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.Cre
3430
ctx, cancel := context.WithTimeout(ctx, MaxConnectionTime)
3531
defer cancel()
3632

33+
userAgentLower := strings.ToLower(r.Header.Get("User-Agent"))
34+
isSafari := strings.Contains(userAgentLower, "safari") && !strings.Contains(userAgentLower, "chrome") && !strings.Contains(userAgentLower, "android")
35+
acceptOptions := &websocket.AcceptOptions{
36+
// Allow any origin/game to connect.
37+
InsecureSkipVerify: true,
38+
}
39+
40+
if isSafari {
41+
acceptOptions.CompressionMode = websocket.CompressionDisabled
42+
}
43+
3744
conn, err := websocket.Accept(w, r, acceptOptions)
3845
if err != nil {
3946
util.ErrorAndAbort(w, r, http.StatusBadRequest, "", err)

0 commit comments

Comments
 (0)