@@ -12,6 +12,7 @@ import (
12
12
"net/http"
13
13
"os"
14
14
"path/filepath"
15
+ "strings"
15
16
"time"
16
17
17
18
"github.com/gorilla/sessions"
@@ -35,16 +36,33 @@ type HttpSessionStore interface {
35
36
36
37
// HttpEngine serves live for net/http.
37
38
type HttpEngine struct {
38
- sessionStore HttpSessionStore
39
+ acceptOptions * websocket.AcceptOptions
40
+ sessionStore HttpSessionStore
39
41
* BaseEngine
40
42
}
41
43
44
+ // WithWebsocketAcceptOptions apply websocket accept options to the HTTP engine.
45
+ func WithWebsocketAcceptOptions (options * websocket.AcceptOptions ) EngineConfig {
46
+ return func (e Engine ) error {
47
+ if httpEngine , ok := e .(* HttpEngine ); ok {
48
+ httpEngine .acceptOptions = options
49
+ }
50
+ return nil
51
+ }
52
+ }
53
+
42
54
// NewHttpHandler returns the net/http handler for live.
43
55
func NewHttpHandler (store HttpSessionStore , handler Handler , configs ... EngineConfig ) * HttpEngine {
44
- return & HttpEngine {
56
+ e := & HttpEngine {
45
57
sessionStore : store ,
46
- BaseEngine : NewBaseEngine (handler , configs ... ),
58
+ BaseEngine : NewBaseEngine (handler ),
47
59
}
60
+ for _ , conf := range configs {
61
+ if err := conf (e ); err != nil {
62
+ log .Println ("warning:" , fmt .Errorf ("could not apply config to engine: %w" , err ))
63
+ }
64
+ }
65
+ return e
48
66
}
49
67
50
68
// ServeHTTP serves this handler.
@@ -267,7 +285,16 @@ func (h *HttpEngine) serveWS(ctx context.Context, w http.ResponseWriter, r *http
267
285
return
268
286
}
269
287
270
- c , err := websocket .Accept (w , r , nil )
288
+ // https://github.com/nhooyr/websocket/issues/218
289
+ // https://github.com/gorilla/websocket/issues/731
290
+ if strings .Contains (r .UserAgent (), "Safari" ) {
291
+ if h .acceptOptions == nil {
292
+ h .acceptOptions = & websocket.AcceptOptions {}
293
+ }
294
+ h .acceptOptions .CompressionMode = websocket .CompressionDisabled
295
+ }
296
+
297
+ c , err := websocket .Accept (w , r , h .acceptOptions )
271
298
if err != nil {
272
299
h .Error ()(ctx , err )
273
300
return
0 commit comments