Skip to content

Commit 86dc5c7

Browse files
BluebugsCedric BAIL
authored andcommitted
Add support for wasm by switching to nhooyr/websocket library.
1 parent 2d3b04d commit 86dc5c7

File tree

7 files changed

+179
-114
lines changed

7 files changed

+179
-114
lines changed

NOTICE.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ Go Sync
6262
* License: BSD 3-Clause style license and patent grant.
6363
* Project: https://cs.opensource.google/go/x/sync/
6464

65-
Gorilla Websockets v1.4.2
65+
Nhooyr Websocket v1.8.7
66+
67+
* License: MIT
68+
* Project: https://github.com/nhooyr/websocket
6669

67-
* License: BSD 2-Clause "Simplified" License
68-
* Project: https://github.com/gorilla/websocket
6970

7071
## Cryptography
7172

@@ -74,4 +75,4 @@ may have restrictions on the import, possession, and use, and/or re-export to
7475
another country, of encryption software. BEFORE using any encryption software,
7576
please check the country's laws, regulations and policies concerning the import,
7677
possession, or use, and re-export of encryption software, to see if this is
77-
permitted.
78+
permitted.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ go get github.com/eclipse/paho.mqtt.golang
3838
```
3939

4040
The client depends on Google's [proxy](https://godoc.org/golang.org/x/net/proxy) package and the
41-
[websockets](https://godoc.org/github.com/gorilla/websocket) package, also easily installed with the commands:
41+
[websockets](https://godoc.org/github.com/nhooyr/websocket) package, also easily installed with the commands:
4242

4343
```
44-
go get github.com/gorilla/websocket
44+
go get github.com/nhooyr/websocket
4545
go get golang.org/x/net/proxy
4646
```
4747

@@ -193,4 +193,4 @@ Discussion of the Paho clients takes place on the [Eclipse paho-dev mailing list
193193

194194
General questions about the MQTT protocol are discussed in the [MQTT Google Group](https://groups.google.com/forum/?hl=en-US&fromgroups#!forum/mqtt).
195195

196-
There is much more information available via the [MQTT community site](http://mqtt.org).
196+
There is much more information available via the [MQTT community site](http://mqtt.org).

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/eclipse/paho.mqtt.golang
33
go 1.14
44

55
require (
6-
github.com/gorilla/websocket v1.4.2
76
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0
87
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
8+
nhooyr.io/websocket v1.8.7
99
)

go.sum

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,71 @@
1-
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
2-
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
5+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
6+
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
7+
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
8+
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
9+
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
10+
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
11+
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
12+
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
13+
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
14+
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
15+
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
16+
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
17+
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
18+
github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
19+
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
20+
github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
21+
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
22+
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
23+
github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls=
24+
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
25+
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
26+
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
27+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
28+
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
29+
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
30+
github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8=
31+
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
32+
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
33+
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
34+
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
35+
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
36+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
37+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
38+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
39+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
40+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
41+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
42+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
43+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
44+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
45+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
46+
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
47+
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
48+
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
49+
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
350
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
451
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U=
552
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
653
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
754
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
855
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
56+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
57+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
958
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1059
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
60+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
61+
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
62+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
63+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
64+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
65+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
66+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
67+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
68+
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
69+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
70+
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
71+
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=

websocket.go

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,8 @@
1414
package mqtt
1515

1616
import (
17-
"crypto/tls"
18-
"fmt"
19-
"io"
20-
"net"
2117
"net/http"
2218
"net/url"
23-
"sync"
24-
"time"
25-
26-
"github.com/gorilla/websocket"
2719
)
2820

2921
// WebsocketOptions are config options for a websocket dialer
@@ -34,99 +26,3 @@ type WebsocketOptions struct {
3426
}
3527

3628
type ProxyFunction func(req *http.Request) (*url.URL, error)
37-
38-
// NewWebsocket returns a new websocket and returns a net.Conn compatible interface using the gorilla/websocket package
39-
func NewWebsocket(host string, tlsc *tls.Config, timeout time.Duration, requestHeader http.Header, options *WebsocketOptions) (net.Conn, error) {
40-
if timeout == 0 {
41-
timeout = 10 * time.Second
42-
}
43-
44-
if options == nil {
45-
// Apply default options
46-
options = &WebsocketOptions{}
47-
}
48-
if options.Proxy == nil {
49-
options.Proxy = http.ProxyFromEnvironment
50-
}
51-
dialer := &websocket.Dialer{
52-
Proxy: options.Proxy,
53-
HandshakeTimeout: timeout,
54-
EnableCompression: false,
55-
TLSClientConfig: tlsc,
56-
Subprotocols: []string{"mqtt"},
57-
ReadBufferSize: options.ReadBufferSize,
58-
WriteBufferSize: options.WriteBufferSize,
59-
}
60-
61-
ws, resp, err := dialer.Dial(host, requestHeader)
62-
63-
if err != nil {
64-
if resp != nil {
65-
WARN.Println(CLI, fmt.Sprintf("Websocket handshake failure. StatusCode: %d. Body: %s", resp.StatusCode, resp.Body))
66-
}
67-
return nil, err
68-
}
69-
70-
wrapper := &websocketConnector{
71-
Conn: ws,
72-
}
73-
return wrapper, err
74-
}
75-
76-
// websocketConnector is a websocket wrapper so it satisfies the net.Conn interface so it is a
77-
// drop in replacement of the golang.org/x/net/websocket package.
78-
// Implementation guide taken from https://github.com/gorilla/websocket/issues/282
79-
type websocketConnector struct {
80-
*websocket.Conn
81-
r io.Reader
82-
rio sync.Mutex
83-
wio sync.Mutex
84-
}
85-
86-
// SetDeadline sets both the read and write deadlines
87-
func (c *websocketConnector) SetDeadline(t time.Time) error {
88-
if err := c.SetReadDeadline(t); err != nil {
89-
return err
90-
}
91-
err := c.SetWriteDeadline(t)
92-
return err
93-
}
94-
95-
// Write writes data to the websocket
96-
func (c *websocketConnector) Write(p []byte) (int, error) {
97-
c.wio.Lock()
98-
defer c.wio.Unlock()
99-
100-
err := c.WriteMessage(websocket.BinaryMessage, p)
101-
if err != nil {
102-
return 0, err
103-
}
104-
return len(p), nil
105-
}
106-
107-
// Read reads the current websocket frame
108-
func (c *websocketConnector) Read(p []byte) (int, error) {
109-
c.rio.Lock()
110-
defer c.rio.Unlock()
111-
for {
112-
if c.r == nil {
113-
// Advance to next message.
114-
var err error
115-
_, c.r, err = c.NextReader()
116-
if err != nil {
117-
return 0, err
118-
}
119-
}
120-
n, err := c.r.Read(p)
121-
if err == io.EOF {
122-
// At end of message.
123-
c.r = nil
124-
if n > 0 {
125-
return n, nil
126-
}
127-
// No data read, continue to next message.
128-
continue
129-
}
130-
return n, err
131-
}
132-
}

websocket_js.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This program and the accompanying materials
3+
* are made available under the terms of the Eclipse Public License v2.0
4+
* and Eclipse Distribution License v1.0 which accompany this distribution.
5+
*
6+
* The Eclipse Public License is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
* and the Eclipse Distribution License is available at
9+
* http://www.eclipse.org/org/documents/edl-v10.php.
10+
*
11+
* Contributors:
12+
*/
13+
14+
package mqtt
15+
16+
import (
17+
"context"
18+
"crypto/tls"
19+
"fmt"
20+
"net"
21+
"net/http"
22+
"time"
23+
24+
"nhooyr.io/websocket"
25+
)
26+
27+
// NewWebsocket returns a new websocket and returns a net.Conn compatible interface using the gorilla/websocket package
28+
func NewWebsocket(host string, _ *tls.Config, _ time.Duration, _ http.Header, _ *WebsocketOptions) (net.Conn, error) {
29+
dialOptions := websocket.DialOptions{
30+
Subprotocols: []string{"mqtt"},
31+
}
32+
33+
ctx := context.Background()
34+
35+
ws, resp, err := websocket.Dial(ctx, host, &dialOptions)
36+
37+
if err != nil {
38+
if resp != nil {
39+
WARN.Println(CLI, fmt.Sprintf("Websocket handshake failure. StatusCode: %d. Body: %s", resp.StatusCode, resp.Body))
40+
}
41+
return nil, err
42+
}
43+
44+
return websocket.NetConn(ctx, ws, websocket.MessageBinary), nil
45+
}

websocket_notjs.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//go:build !js
2+
// +build !js
3+
4+
/*
5+
* This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v2.0
7+
* and Eclipse Distribution License v1.0 which accompany this distribution.
8+
*
9+
* The Eclipse Public License is available at
10+
* https://www.eclipse.org/legal/epl-2.0/
11+
* and the Eclipse Distribution License is available at
12+
* http://www.eclipse.org/org/documents/edl-v10.php.
13+
*
14+
* Contributors:
15+
*/
16+
17+
package mqtt
18+
19+
import (
20+
"context"
21+
"crypto/tls"
22+
"fmt"
23+
"net"
24+
"net/http"
25+
"time"
26+
27+
"nhooyr.io/websocket"
28+
)
29+
30+
// NewWebsocket returns a new websocket and returns a net.Conn compatible interface using the gorilla/websocket package
31+
func NewWebsocket(host string, tlsc *tls.Config, timeout time.Duration, requestHeader http.Header, options *WebsocketOptions) (net.Conn, error) {
32+
if timeout == 0 {
33+
timeout = 10 * time.Second
34+
}
35+
36+
if options == nil {
37+
// Apply default options
38+
options = &WebsocketOptions{}
39+
}
40+
41+
httpTransport := &http.Transport{TLSClientConfig: tlsc, Proxy: options.Proxy}
42+
httpClient := &http.Client{Transport: httpTransport, Timeout: timeout}
43+
44+
dialOptions := websocket.DialOptions{
45+
HTTPClient: httpClient,
46+
HTTPHeader: requestHeader,
47+
Subprotocols: []string{"mqtt"},
48+
}
49+
50+
ctx := context.Background()
51+
52+
ws, resp, err := websocket.Dial(ctx, host, &dialOptions)
53+
54+
if err != nil {
55+
if resp != nil {
56+
WARN.Println(CLI, fmt.Sprintf("Websocket handshake failure. StatusCode: %d. Body: %s", resp.StatusCode, resp.Body))
57+
}
58+
return nil, err
59+
}
60+
61+
return websocket.NetConn(ctx, ws, websocket.MessageBinary), nil
62+
}

0 commit comments

Comments
 (0)