@@ -18,17 +18,26 @@ type Config struct {
1818 // Filter defines a function to skip middleware.
1919 // Optional. Default: nil
2020 Filter func (* fiber.Ctx ) bool
21- // Origins
22- // Optional. Default value "1; mode=block".
21+ // HandshakeTimeout specifies the duration for the handshake to complete.
2322 HandshakeTimeout time.Duration
24- Subprotocols []string
25-
26- Origins []string
27- ReadBufferSize int
28- WriteBufferSize int
23+ // Subprotocols specifies the client's requested subprotocols.
24+ Subprotocols []string
25+ // Allowed Origin's based on the Origin header, this validate the request origin to
26+ // prevent cross-site request forgery. Everything is allowed if left empty.
27+ Origins []string
28+ // ReadBufferSize and WriteBufferSize specify I/O buffer sizes in bytes. If a buffer
29+ // size is zero, then a useful default size is used. The I/O buffer sizes
30+ // do not limit the size of the messages that can be sent or received.
31+ ReadBufferSize , WriteBufferSize int
32+ // EnableCompression specifies if the client should attempt to negotiate
33+ // per message compression (RFC 7692). Setting this value to true does not
34+ // guarantee that compression will be supported. Currently only "no context
35+ // takeover" modes are supported.
2936 EnableCompression bool
3037}
3138
39+ // New returns a new `handler func(*Conn)` that upgrades a client to the
40+ // websocket protocol, you can pass an optional config.
3241func New (handler func (* Conn ), config ... Config ) func (* fiber.Ctx ) {
3342 // Init config
3443 var cfg Config
@@ -55,15 +64,20 @@ func New(handler func(*Conn), config ...Config) func(*fiber.Ctx) {
5564 return true
5665 }
5766 origin := string (fctx .Request .Header .Peek ("Origin" ))
58- for _ , o := range cfg .Origins {
59- if o == origin {
67+ for i := range cfg .Origins {
68+ if cfg . Origins [ i ] == origin {
6069 return true
6170 }
6271 }
6372 return false
6473 },
6574 }
75+ // Fix when fiber released v1.9.7
76+ // var params []string
6677 return func (c * fiber.Ctx ) {
78+ // if params != nil {
79+ // params = c.Route().Params
80+ // }
6781 locals := make (map [string ]interface {})
6882 c .Fasthttp .VisitUserValues (func (key []byte , value interface {}) {
6983 locals [string (key )] = value
@@ -84,6 +98,8 @@ func New(handler func(*Conn), config ...Config) func(*fiber.Ctx) {
8498type Conn struct {
8599 * websocket.Conn
86100 locals map [string ]interface {}
101+ // params []string // fiber v1.9.7
102+ // values []string // fiber v1.9.7
87103}
88104
89105// Conn pool
@@ -107,6 +123,71 @@ func releaseConn(conn *Conn) {
107123 poolConn .Put (conn )
108124}
109125
126+ // Locals makes it possible to pass interface{} values under string keys scoped to the request
127+ // and therefore available to all following routes that match the request.
110128func (conn * Conn ) Locals (key string ) interface {} {
111129 return conn .locals [key ]
112130}
131+
132+ // Params is used to get the route parameters.
133+ // Defaults to empty string "", if the param doesn't exist.
134+ // func (conn *Conn) Params(key string) string {
135+ // for i := range conn.params {
136+ // if len(key) != len(conn.params[i]) {
137+ // continue
138+ // }
139+ // if conn.params[i] == key {
140+ // return conn.values[i]
141+ // }
142+ // }
143+ // return ""
144+ // }
145+
146+ // IsWebSocketUpgrade returns true if the client requested upgrade to the
147+ // WebSocket protocol.
148+ func IsWebSocketUpgrade (ctx * fiber.Ctx ) bool {
149+ return websocket .FastHTTPIsWebSocketUpgrade (ctx .Fasthttp )
150+ }
151+
152+ // Constants are taken from https://github.com/fasthttp/websocket/blob/master/conn.go#L43
153+
154+ // Close codes defined in RFC 6455, section 11.7.
155+ const (
156+ CloseNormalClosure = 1000
157+ CloseGoingAway = 1001
158+ CloseProtocolError = 1002
159+ CloseUnsupportedData = 1003
160+ CloseNoStatusReceived = 1005
161+ CloseAbnormalClosure = 1006
162+ CloseInvalidFramePayloadData = 1007
163+ ClosePolicyViolation = 1008
164+ CloseMessageTooBig = 1009
165+ CloseMandatoryExtension = 1010
166+ CloseInternalServerErr = 1011
167+ CloseServiceRestart = 1012
168+ CloseTryAgainLater = 1013
169+ CloseTLSHandshake = 1015
170+ )
171+
172+ // The message types are defined in RFC 6455, section 11.8.
173+ const (
174+ // TextMessage denotes a text data message. The text message payload is
175+ // interpreted as UTF-8 encoded text data.
176+ TextMessage = 1
177+
178+ // BinaryMessage denotes a binary data message.
179+ BinaryMessage = 2
180+
181+ // CloseMessage denotes a close control message. The optional message
182+ // payload contains a numeric code and text. Use the FormatCloseMessage
183+ // function to format a close message payload.
184+ CloseMessage = 8
185+
186+ // PingMessage denotes a ping control message. The optional message payload
187+ // is UTF-8 encoded text.
188+ PingMessage = 9
189+
190+ // PongMessage denotes a pong control message. The optional message payload
191+ // is UTF-8 encoded text.
192+ PongMessage = 10
193+ )
0 commit comments