Skip to content

Commit 1b7cc02

Browse files
committed
Add WASM and GopherJS docs
1 parent ff4d818 commit 1b7cc02

File tree

8 files changed

+77
-14
lines changed

8 files changed

+77
-14
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ go get nhooyr.io/websocket
2323
- JSON and ProtoBuf helpers in the [wsjson](https://godoc.org/nhooyr.io/websocket/wsjson) and [wspb](https://godoc.org/nhooyr.io/websocket/wspb) subpackages
2424
- Highly optimized by default
2525
- Concurrent writes out of the box
26+
- [Complete WASM](https://godoc.org/nhooyr.io/websocket#hdr-WASM) support
2627

2728
## Roadmap
2829

2930
- [ ] WebSockets over HTTP/2 [#4](https://github.com/nhooyr/websocket/issues/4)
30-
- [ ] WASM Compilation [#121](https://github.com/nhooyr/websocket/issues/121)
3131

3232
## Examples
3333

@@ -131,6 +131,8 @@ which results in awkward control flow. With nhooyr/websocket you use the Ping me
131131
that sends a ping and also waits for the pong, though you must be reading from the connection
132132
for the pong to be read.
133133

134+
Additionally, nhooyr.io/websocket can compile to WASM for the browser.
135+
134136
In terms of performance, the differences mostly depend on your application code. nhooyr/websocket
135137
reuses message buffers out of the box if you use the wsjson and wspb subpackages.
136138
As mentioned above, nhooyr/websocket also supports concurrent writers.

ci/tools.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package ci
44

55
// See https://github.com/go-modules-by-example/index/blob/master/010_tools/README.md
66
import (
7+
_ "github.com/agnivade/wasmbrowsertest"
78
_ "go.coder.com/go-tools/cmd/goimports"
89
_ "golang.org/x/lint/golint"
910
_ "golang.org/x/tools/cmd/stringer"

ci/wasm.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ WS_ECHO_SERVER_URL="$(head -n 1 "$stdout")"
1313
GOOS=js GOARCH=wasm go vet ./...
1414
go install golang.org/x/lint/golint
1515
GOOS=js GOARCH=wasm golint -set_exit_status ./...
16-
GOOS=js GOARCH=wasm go test ./... -args "$WS_ECHO_SERVER_URL"
16+
go install github.com/agnivade/wasmbrowsertest
17+
GOOS=js GOARCH=wasm go test -exec="wasmbrowsertest" ./... -args "$WS_ECHO_SERVER_URL"
18+
19+
kill %1

doc.go

+23
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,27 @@
1717
//
1818
// Use the errors.As function new in Go 1.13 to check for websocket.CloseError.
1919
// See the CloseError example.
20+
//
21+
// WASM
22+
//
23+
// The client side fully supports compiling to WASM.
24+
// It wraps the WebSocket browser API.
25+
// See https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
26+
//
27+
// Thus the unsupported features when compiling to WASM are:
28+
// - Accept API
29+
// - Reader/Writer API
30+
// - SetReadLimit
31+
// - Ping
32+
// - HTTPClient and HTTPHeader dial options
33+
//
34+
// The *http.Response returned by Dial will always either be nil or &http.Response{} as
35+
// we do not have access to the handshake response in the browser.
36+
//
37+
// Writes are also always async so the passed context is no-op.
38+
//
39+
// Everything else is fully supported. This includes the wsjson and wspb helper packages.
40+
//
41+
// Once https://github.com/gopherjs/gopherjs/issues/929 is closed, GopherJS should be supported
42+
// as well.
2043
package websocket // import "nhooyr.io/websocket"

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module nhooyr.io/websocket
33
go 1.13
44

55
require (
6+
github.com/agnivade/wasmbrowsertest v0.3.0
67
github.com/fatih/color v1.7.0 // indirect
78
github.com/golang/protobuf v1.3.2
89
github.com/google/go-cmp v0.3.1

go.sum

+30
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
1+
github.com/agnivade/wasmbrowsertest v0.3.0 h1:5pAabhWzTVCLoVWqYejEbmWyzNGFR7K/Nu5lsmD1fVc=
2+
github.com/agnivade/wasmbrowsertest v0.3.0/go.mod h1:zQt6ZTdl338xxRaMW395qccVE2eQm0SjC/SDz0mPWQI=
3+
github.com/chromedp/cdproto v0.0.0-20190614062957-d6d2f92b486d/go.mod h1:S8mB5wY3vV+vRIzf39xDXsw3XKYewW9X6rW2aEmkrSw=
4+
github.com/chromedp/cdproto v0.0.0-20190621002710-8cbd498dd7a0 h1:4Wocv9f+KWF4GtZudyrn8JSBTgHQbGp86mcsoH7j1iQ=
5+
github.com/chromedp/cdproto v0.0.0-20190621002710-8cbd498dd7a0/go.mod h1:S8mB5wY3vV+vRIzf39xDXsw3XKYewW9X6rW2aEmkrSw=
6+
github.com/chromedp/chromedp v0.3.1-0.20190619195644-fd957a4d2901 h1:tg66ykM8VYqP9k4DFQwSMnYv84HNTruF+GR6kefFNg4=
7+
github.com/chromedp/chromedp v0.3.1-0.20190619195644-fd957a4d2901/go.mod h1:mJdvfrVn594N9tfiPecUidF6W5jPRKHymqHfzbobPsM=
18
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
29
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
310
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
411
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw=
13+
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
514
github.com/fatih/color v1.6.0 h1:66qjqZk8kalYAvDRtM1AdAJQI0tj4Wrue3Eq3B3pmFU=
615
github.com/fatih/color v1.6.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
716
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
817
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
918
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
1019
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
20+
github.com/go-interpreter/wagon v0.5.1-0.20190713202023-55a163980b6c h1:DLLAPVFrk9iNzljMKF512CUmrFImQ6WU3sDiUS4IRqk=
21+
github.com/go-interpreter/wagon v0.5.1-0.20190713202023-55a163980b6c/go.mod h1:5+b/MBYkclRZngKF5s6qrgWxSLgE9F5dFdO1hAueZLc=
22+
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
23+
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
24+
github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
25+
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
26+
github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
27+
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
1128
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1229
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
1330
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1431
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
1532
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
1633
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
34+
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f h1:Jnx61latede7zDD3DiiP4gmNz33uK0U5HDUaF0a/HVQ=
35+
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
1736
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
1837
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
1938
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
2039
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
40+
github.com/knq/sysutil v0.0.0-20181215143952-f05b59f0f307 h1:vl4eIlySbjertFaNwiMjXsGrFVK25aOWLq7n+3gh2ls=
41+
github.com/knq/sysutil v0.0.0-20181215143952-f05b59f0f307/go.mod h1:BjPj+aVjl9FW/cCGiF3nGh5v+9Gd3VCgBQbod/GlMaQ=
2142
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
2243
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
2344
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
@@ -27,6 +48,10 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
2748
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
2849
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
2950
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
51+
github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
52+
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
53+
github.com/mailru/easyjson v0.0.0-20190620125010-da37f6c1e481 h1:IaSjLMT6WvkoZZjspGxy3rdaTEmWLoRm49WbtVUi9sA=
54+
github.com/mailru/easyjson v0.0.0-20190620125010-da37f6c1e481/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
3055
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
3156
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
3257
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
@@ -61,6 +86,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
6186
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
6287
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
6388
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
89+
github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc h1:RTUQlKzoZZVG3umWNzOYeFecQLIh+dbxXvJp1zPQJTI=
90+
github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A=
6491
go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16 h1:3gGa1bM0nG7Ruhu5b7wKnoOOwAD/fJ8iyyAcpOzDG3A=
6592
go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16/go.mod h1:iKV5yK9t+J5nG9O3uF6KYdPEz3dyfMyB15MN1rbQ8Qw=
6693
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
@@ -86,7 +113,10 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h
86113
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
87114
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
88115
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
116+
golang.org/x/sys v0.0.0-20190306220234-b354f8bf4d9e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
89117
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
118+
golang.org/x/sys v0.0.0-20190618155005-516e3c20635f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
119+
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
90120
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
91121
golang.org/x/sys v0.0.0-20190919044723-0c1ff786ef13 h1:/zi0zzlPHWXYXrO1LjNRByFu8sdGgCkj2JLDdBIB84k=
92122
golang.org/x/sys v0.0.0-20190919044723-0c1ff786ef13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

statuscode.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ const (
1818
StatusGoingAway
1919
StatusProtocolError
2020
StatusUnsupportedData
21+
2122
_ // 1004 is reserved.
23+
2224
StatusNoStatusRcvd
23-
// statusAbnormalClosure is unexported because it isn't necessary, at least until WASM.
24-
// The error returned will indicate whether the connection was closed or not or what happened.
25-
// It only makes sense for browser clients.
26-
statusAbnormalClosure
25+
26+
// This StatusCode is only exported for use with WASM.
27+
// In pure Go, the returned error will indicate whether the connection was closed or not or what happened.
28+
StatusAbnormalClosure
29+
2730
StatusInvalidFramePayloadData
2831
StatusPolicyViolation
2932
StatusMessageTooBig
@@ -32,10 +35,10 @@ const (
3235
StatusServiceRestart
3336
StatusTryAgainLater
3437
StatusBadGateway
35-
// statusTLSHandshake is unexported because we just return
36-
// the handshake error in dial. We do not return a conn
37-
// so there is nothing to use this on. At least until WASM.
38-
statusTLSHandshake
38+
39+
// This StatusCode is only exported for use with WASM.
40+
// In pure Go, the returned error will indicate whether there was a TLS handshake failure.
41+
StatusTLSHandshake
3942
)
4043

4144
// CloseError represents a WebSocket close frame.
@@ -79,7 +82,7 @@ func parseClosePayload(p []byte) (CloseError, error) {
7982
// and https://tools.ietf.org/html/rfc6455#section-7.4.1
8083
func validWireCloseCode(code StatusCode) bool {
8184
switch code {
82-
case 1004, StatusNoStatusRcvd, statusAbnormalClosure, statusTLSHandshake:
85+
case 1004, StatusNoStatusRcvd, StatusAbnormalClosure, StatusTLSHandshake:
8386
return false
8487
}
8588

statuscode_string.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)