Skip to content

Commit

Permalink
Merge 13171ed into c8a8c64
Browse files Browse the repository at this point in the history
  • Loading branch information
wavded committed May 30, 2018
2 parents c8a8c64 + 13171ed commit bb37de9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
16 changes: 16 additions & 0 deletions sockjs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"regexp"
"strings"
"sync"

"github.com/gorilla/websocket"
)

var (
Expand All @@ -19,6 +21,7 @@ type handler struct {
options Options
handlerFunc func(Session)
mappings []*mapping
upgrader websocket.Upgrader

sessionsMux sync.Mutex
sessions map[string]*session
Expand All @@ -36,6 +39,19 @@ func newHandler(prefix string, opts Options, handlerFunc func(Session)) *handler
options: opts,
handlerFunc: handlerFunc,
sessions: make(map[string]*session),

upgrader: websocket.Upgrader{
ReadBufferSize: WebSocketReadBufSize,
WriteBufferSize: WebSocketWriteBufSize,
CheckOrigin: func(_ *http.Request) bool {
// Allow all connections by default.
return true
},
Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {
// Don't return errors to maintain backwards compatibility.
},
EnableCompression: opts.WebsocketCompression,
},
}

sessionPrefix := prefix + "/[^/.]+/[^/.]+"
Expand Down
3 changes: 3 additions & 0 deletions sockjs/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type Options struct {
Websocket bool
// This option can be used to enable raw websockets support by the server. By default raw websockets are disabled.
RawWebsocket bool
// Enable per-message compression extensions for browsers that support.
// See https://godoc.org/github.com/gorilla/websocket#hdr-Compression_EXPERIMENTAL for more details.
WebsocketCompression bool
// In order to keep proxies and load balancers from closing long running http requests we need to pretend that the connection is active
// and send a heartbeat packet once in a while. This setting controls how often this is done.
// By default a heartbeat packet is sent every 25 seconds.
Expand Down
2 changes: 1 addition & 1 deletion sockjs/rawwebsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (h *handler) rawWebsocket(rw http.ResponseWriter, req *http.Request) {
conn, err := websocket.Upgrade(rw, req, nil, WebSocketReadBufSize, WebSocketWriteBufSize)
conn, err := h.upgrader.Upgrade(rw, req, nil)
if _, ok := err.(websocket.HandshakeError); ok {
http.Error(rw, `Can "Upgrade" only to "WebSocket".`, http.StatusBadRequest)
return
Expand Down
2 changes: 1 addition & 1 deletion sockjs/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var WebSocketReadBufSize = 4096
var WebSocketWriteBufSize = 4096

func (h *handler) sockjsWebsocket(rw http.ResponseWriter, req *http.Request) {
conn, err := websocket.Upgrade(rw, req, nil, WebSocketReadBufSize, WebSocketWriteBufSize)
conn, err := h.upgrader.Upgrade(rw, req, nil)
if _, ok := err.(websocket.HandshakeError); ok {
http.Error(rw, `Can "Upgrade" only to "WebSocket".`, http.StatusBadRequest)
return
Expand Down

0 comments on commit bb37de9

Please sign in to comment.