Skip to content
This repository was archived by the owner on May 24, 2023. It is now read-only.

Commit 3e81a3d

Browse files
committed
v0.2.1
Add constants
1 parent 89ea67a commit 3e81a3d

File tree

5 files changed

+131
-67
lines changed

5 files changed

+131
-67
lines changed

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@ func main() {
2323
app := fiber.New()
2424

2525
app.Use(func(c *fiber.Ctx) {
26-
c.Locals("Hello", "World")
27-
c.Next()
26+
// IsWebsocketUpgrade returns true if the client
27+
// requested upgrade to the WebSocket protocol.
28+
if websocket.IsWebsocketUpgrade(c) {
29+
c.Locals("allowed", true)
30+
c.Next()
31+
}
2832
})
29-
30-
app.Get("/ws", websocket.New(func(c *websocket.Conn) {
31-
fmt.Println(c.Locals("Hello")) // "World"
32-
33-
// Websocket stuff
33+
34+
app.Get("/ws/:id?", websocket.New(func(c *websocket.Conn) {
35+
// Locals & Params are added to the *websocket.Conn
36+
fmt.Println(c.Locals("allowed")) // true
37+
fmt.Println(c.Params("id")) // "1337"
38+
39+
// websocket.Conn bindings https://pkg.go.dev/github.com/fasthttp/websocket?tab=doc#pkg-index
3440
for {
3541
mt, msg, err := c.ReadMessage()
3642
if err != nil {
@@ -44,8 +50,10 @@ func main() {
4450
break
4551
}
4652
}
53+
4754
}))
4855

49-
app.Listen(3000) // Listen on ws://localhost:3000/ws
56+
app.Listen(3000)
57+
// Access the websocket server: ws://localhost:3000/ws/12345
5058
}
5159
```

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/gofiber/websocket
33
go 1.11
44

55
require (
6-
github.com/gofiber/fiber v1.9.3
76
github.com/fasthttp/websocket v1.4.2
8-
github.com/valyala/fasthttp v1.9.0
7+
github.com/gofiber/fiber v1.9.6
8+
github.com/valyala/fasthttp v1.12.0
99
)

go.sum

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
github.com/fasthttp/websocket v1.4.2/go.mod h1:smsv/h4PBEBaU0XDTY5UwJTpZv69fQ0FfcLJr21mA6Y=
2+
github.com/gofiber/fiber v1.9.3/go.mod h1:o2YQgwJW8+Z16x8MTos4nYn8PD1RJpzu9fojiGqjSjI=
3+
github.com/gofiber/fiber v1.9.6 h1:HtzOdbdNn/K/NlMMLbCNkiI79Ft0zjio7STcgY/Rox0=
4+
github.com/gofiber/fiber v1.9.6/go.mod h1:o2YQgwJW8+Z16x8MTos4nYn8PD1RJpzu9fojiGqjSjI=
5+
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
6+
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
7+
github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
8+
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=
9+
github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
10+
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
11+
github.com/savsgio/gotils v0.0.0-20200117113501-90175b0fbe3f/go.mod h1:lHhJedqxCoHN+zMtwGNTXWmF0u9Jt363FYRhV6g0CdY=
12+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
13+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
14+
github.com/valyala/fasthttp v1.9.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
15+
github.com/valyala/fasthttp v1.12.0 h1:TsB9qkSeiMXB40ELWWSRMjlsE+8IkqXHcs01y2d9aw0=
16+
github.com/valyala/fasthttp v1.12.0/go.mod h1:229t1eWu9UXTPmoUkbpN/fctKPBY4IJoFXQnxHGXy6E=
17+
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
18+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
19+
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
20+
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
21+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
22+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
23+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

main.go

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3241
func 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) {
8498
type 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.
110128
func (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+
)

utils.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)