Skip to content

Commit

Permalink
Workaround Safari Websocket Compression issue (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdubbelboer committed Apr 24, 2023
1 parent fbfacf2 commit 41f7465
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ n.on('ready', () => {
const el = document.getElementById('lobbies')
if (el !== null) {
el.innerHTML = ''
if (lobbies.length === 0) {
if (lobbies === null || lobbies.length === 0) {
const li = document.createElement('li')
li.innerHTML = '<i>no lobbies</i>'
el.appendChild(li)
Expand Down
17 changes: 12 additions & 5 deletions internal/signaling/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"net/http"
"strings"
"time"

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

func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.CredentialsClient) http.HandlerFunc {
acceptOptions := &websocket.AcceptOptions{
// Allow any origin/game to connect.
InsecureSkipVerify: true,
}

manager := &TimeoutManager{}
go manager.Run(ctx)

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

userAgentLower := strings.ToLower(r.Header.Get("User-Agent"))
isSafari := strings.Contains(userAgentLower, "safari") && !strings.Contains(userAgentLower, "chrome") && !strings.Contains(userAgentLower, "android")
acceptOptions := &websocket.AcceptOptions{
// Allow any origin/game to connect.
InsecureSkipVerify: true,
}

if isSafari {
acceptOptions.CompressionMode = websocket.CompressionDisabled
}

conn, err := websocket.Accept(w, r, acceptOptions)
if err != nil {
util.ErrorAndAbort(w, r, http.StatusBadRequest, "", err)
Expand Down

0 comments on commit 41f7465

Please sign in to comment.