gobwas / ws

Tiny WebSocket library for Go.
MIT License
6.1k stars 373 forks source link

Websocket error on Safari #169

Open gabridego opened 1 year ago

gabridego commented 1 year ago

I am developing a WebRTC service which uses your library for the signalling phase. Everything works well on Chrome, Firefox and Android, but I am experiencing an error on Safari (macOS Ventura 13.3) and iOS:

[Error] WebSocket connection to 'wss://myapp.com/ws' failed: The operation couldn’t be completed. Socket is not connected

Anyway, it works on Safari when disabling the experimental feature 'NSURLSession WebSocket', which makes me suspect of a possible issue with WebSocket compression. Some related issues:

Is there any configuration I can use to make my websocket application work on Safari and iOS as well? Thank you!

cristaloleg commented 1 year ago

Hi, thank you for the detailed issue. I haven't met problems so far, so I cannot share any meaningful configuration.

I will try to debug it but no idea will I find something or not.

gabridego commented 1 year ago

Hi, thank you for your quick reply! I can reproduce the issue with a very minimal example:

package main

import (
    "net/http"

    "github.com/gobwas/ws"
    "github.com/gobwas/ws/wsutil"
)

func main() {
    http.ListenAndServe(":9090", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        conn, _, _, _ := ws.UpgradeHTTP(r, w)
        defer conn.Close()

        wsutil.WriteServerText(conn, []byte("ciao"))
    }))
}

On Safari console:

ws = new WebSocket("ws://127.0.0.1:9090")
ws.onmessage = (msg) => console.log(msg.data)

With NSURLSession WebSocket enabled, it prints ciao and then gives the error. With the function disabled, just the message with no error.

Am I doing anything wrong? Thanks!

sudhakar commented 1 year ago

More discussion about this with possible workarounds in https://github.com/uNetworking/uWebSockets/issues/1347. As per the thread only "Safari 15.0-15.3" is affected and looks like 15.4 onwards works fine

Some workarounds discussed there are,

  1. Disable compression if useragent is "Safari 15.0-15.3"
  2. If compression is needed, have sufficient buffer size, so that message is not fragmented and hence wont trigger this bug in broken safari (https://github.com/uNetworking/uWebSockets/issues/1347#issuecomment-955711895)

If anybody gets to test any of these workarounds, please report here

FZambia commented 1 year ago

The example shown by @gabridego does not use compression. Also, I tested it on Safari 16.1 - and the error is still there. So I think those are two different Safari problems.