Skip to content

Commit e935398

Browse files
committed
Add WebSocket API
Closes #121
1 parent e09e295 commit e935398

23 files changed

+104
-17
lines changed

accept.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

accept_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

ci/wasm.sh

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ cd "$(git rev-parse --show-toplevel)"
66

77
GOOS=js GOARCH=wasm go vet ./...
88
go install golang.org/x/lint/golint
9-
# Get passing later.
10-
#GOOS=js GOARCH=wasm golint -set_exit_status ./...
11-
GOOS=js GOARCH=wasm go test ./internal/wsjs
9+
GOOS=js GOARCH=wasm golint -set_exit_status ./...
10+
GOOS=js GOARCH=wasm go test -race ./internal/wsjs

dial.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

dial_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
// Package websocket is a minimal and idiomatic implementation of the WebSocket protocol.
24
//
35
// https://tools.ietf.org/html/rfc6455

example_echo_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket_test
24

35
import (

example_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket_test
24

35
import (

export_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

header.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

header_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

internal/wsjs/wsjs.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ func handleJSError(err *error, onErr func()) {
2626
}
2727
}
2828

29-
func New(ctx context.Context, url string, protocols []string) (c *WebSocket, err error) {
29+
func New(url string, protocols []string) (c WebSocket, err error) {
3030
defer handleJSError(&err, func() {
31-
c = nil
31+
c = WebSocket{}
3232
})
3333

3434
jsProtocols := make([]interface{}, len(protocols))
3535
for i, p := range protocols {
3636
jsProtocols[i] = p
3737
}
3838

39-
c = &WebSocket{
39+
c = WebSocket{
4040
v: js.Global().Get("WebSocket").New(url, jsProtocols),
4141
}
4242

@@ -57,15 +57,15 @@ type WebSocket struct {
5757
v js.Value
5858
}
5959

60-
func (c *WebSocket) setBinaryType(typ string) {
60+
func (c WebSocket) setBinaryType(typ string) {
6161
c.v.Set("binaryType", string(typ))
6262
}
6363

64-
func (c *WebSocket) BufferedAmount() uint32 {
64+
func (c WebSocket) BufferedAmount() uint32 {
6565
return uint32(c.v.Get("bufferedAmount").Int())
6666
}
6767

68-
func (c *WebSocket) addEventListener(eventType string, fn func(e js.Value)) {
68+
func (c WebSocket) addEventListener(eventType string, fn func(e js.Value)) {
6969
c.v.Call("addEventListener", eventType, js.FuncOf(func(this js.Value, args []js.Value) interface{} {
7070
fn(args[0])
7171
return nil
@@ -78,7 +78,7 @@ type CloseEvent struct {
7878
WasClean bool
7979
}
8080

81-
func (c *WebSocket) OnClose(fn func(CloseEvent)) {
81+
func (c WebSocket) OnClose(fn func(CloseEvent)) {
8282
c.addEventListener("close", func(e js.Value) {
8383
ce := CloseEvent{
8484
Code: uint16(e.Get("code").Int()),
@@ -89,7 +89,7 @@ func (c *WebSocket) OnClose(fn func(CloseEvent)) {
8989
})
9090
}
9191

92-
func (c *WebSocket) OnError(fn func(e js.Value)) {
92+
func (c WebSocket) OnError(fn func(e js.Value)) {
9393
c.addEventListener("error", fn)
9494
}
9595

@@ -99,7 +99,7 @@ type MessageEvent struct {
9999
// See https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
100100
}
101101

102-
func (c *WebSocket) OnMessage(fn func(m MessageEvent)) {
102+
func (c WebSocket) OnMessage(fn func(m MessageEvent)) {
103103
c.addEventListener("message", func(e js.Value) {
104104
var data []byte
105105

@@ -119,23 +119,23 @@ func (c *WebSocket) OnMessage(fn func(m MessageEvent)) {
119119
})
120120
}
121121

122-
func (c *WebSocket) OnOpen(fn func(e js.Value)) {
122+
func (c WebSocket) OnOpen(fn func(e js.Value)) {
123123
c.addEventListener("open", fn)
124124
}
125125

126-
func (c *WebSocket) Close(code int, reason string) (err error) {
126+
func (c WebSocket) Close(code int, reason string) (err error) {
127127
defer handleJSError(&err, nil)
128128
c.v.Call("close", code, reason)
129129
return err
130130
}
131131

132-
func (c *WebSocket) SendText(v string) (err error) {
132+
func (c WebSocket) SendText(v string) (err error) {
133133
defer handleJSError(&err, nil)
134134
c.v.Call("send", v)
135135
return err
136136
}
137137

138-
func (c *WebSocket) SendBytes(v []byte) (err error) {
138+
func (c WebSocket) SendBytes(v []byte) (err error) {
139139
defer handleJSError(&err, nil)
140140
c.v.Call("send", uint8Array(v))
141141
return err

netconn.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

opcode.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
// opcode represents a WebSocket Opcode.

statuscode.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

statuscode_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

websocket.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

websocket_autobahn_python_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This file contains the old autobahn test suite tests that use the
2-
// python binary. The approach is very clunky and slow so new tests
2+
// python binary. The approach is clunky and slow so new tests
33
// have been written in pure Go in websocket_test.go.
4+
// These have been kept for correctness purposes and are occasionally ran.
45
// +build autobahn-python
56

67
package websocket_test

websocket_bench_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket_test
24

35
import (

websocket_js.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package websocket
2+
3+
import (
4+
"context"
5+
"golang.org/x/xerrors"
6+
"nhooyr.io/websocket/internal/wsjs"
7+
"syscall/js"
8+
)
9+
10+
type Conn struct {
11+
closed *
12+
}
13+
14+
func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) {
15+
panic("TODO")
16+
}
17+
18+
func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error {
19+
panic("TODO")
20+
}
21+
22+
type StatusCode int
23+
24+
func (c *Conn) Close(code StatusCode, reason string) {
25+
26+
}
27+
28+
// DialOptions represents the options available to pass to Dial.
29+
type DialOptions struct {
30+
// Subprotocols lists the subprotocols to negotiate with the server.
31+
Subprotocols []string
32+
}
33+
34+
func Dial(ctx context.Context, url string, opts *DialOptions) (*Conn, error){
35+
ws, err := wsjs.New(url, opts.Subprotocols)
36+
if err != nil {
37+
return nil, xerrors.Errorf("failed to dial: %w", err)
38+
}
39+
40+
ws.OnOpen(func(e js.Value) {
41+
42+
})
43+
44+
ws.OnError(func(e js.Value) {
45+
46+
})
47+
48+
<-
49+
}

websocket_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket_test
24

35
import (

xor.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

xor_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

0 commit comments

Comments
 (0)