Skip to content

Commit d827e3d

Browse files
committed
Support SockJS
1 parent c2cc1ea commit d827e3d

13 files changed

+315
-145
lines changed

server/conn.go

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

server/conn_sockjs.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package server
2+
3+
import "encoding/json"
4+
5+
type SockJSConn struct {
6+
session SockJSSession
7+
}
8+
9+
func (c *SockJSConn) ReadMessage() (Message, error) {
10+
msg := Message{}
11+
12+
str, err := c.session.Recv()
13+
if err != nil {
14+
return msg, err
15+
}
16+
17+
chars := []byte(str)
18+
if err = json.Unmarshal(chars, &msg); err != nil {
19+
return msg, err
20+
}
21+
22+
return msg, nil
23+
}
24+
25+
func (c *SockJSConn) WriteMessage(msg interface{}) error {
26+
chars, err := json.Marshal(msg)
27+
if err != nil {
28+
return err
29+
}
30+
31+
str := string(chars)
32+
if err = c.session.Send(str); err != nil {
33+
return err
34+
}
35+
36+
return nil
37+
}

server/conn_sockjs_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package server
2+
3+
import (
4+
"errors"
5+
"testing"
6+
)
7+
8+
func Test_SockJSConn_ReadMessage(t *testing.T) {
9+
sock := &TestSockJSSession{in: "{\"id\": \"test-id\"}"}
10+
conn := &SockJSConn{sock}
11+
msg, err := conn.ReadMessage()
12+
13+
if err != nil {
14+
t.Error("should not return an error")
15+
}
16+
17+
if msg.ID != "test-id" {
18+
t.Error("should return correct message")
19+
}
20+
}
21+
22+
func Test_SockJSConn_ReadMessage_ReadError(t *testing.T) {
23+
sock := &TestSockJSSession{err: errors.New("test-err")}
24+
conn := &SockJSConn{sock}
25+
26+
if _, err := conn.ReadMessage(); err.Error() != "test-err" {
27+
t.Error("should return error it read fails")
28+
}
29+
}
30+
31+
func Test_SockJSConn_ReadMessage_UnmarshalError(t *testing.T) {
32+
sock := &TestSockJSSession{in: "not-json"}
33+
conn := &SockJSConn{sock}
34+
35+
if _, err := conn.ReadMessage(); err == nil {
36+
t.Error("should return error if unmarshal fails")
37+
}
38+
}
39+
40+
func Test_SockJSConn_WriteMessage(t *testing.T) {
41+
sock := &TestSockJSSession{}
42+
conn := &SockJSConn{sock}
43+
44+
data := struct {
45+
Foo string `json:"foo"`
46+
}{
47+
Foo: "bar",
48+
}
49+
50+
if err := conn.WriteMessage(data); err != nil {
51+
t.Error("should not return an error")
52+
}
53+
54+
s, ok := conn.session.(*TestSockJSSession)
55+
if !ok {
56+
t.Error("what?")
57+
}
58+
59+
if s.out != `{"foo":"bar"}` {
60+
t.Error("should send correct message")
61+
}
62+
}
63+
64+
func Test_SockJSConn_WriteMessage_WriteError(t *testing.T) {
65+
sock := &TestSockJSSession{err: errors.New("test-error")}
66+
conn := &SockJSConn{sock}
67+
68+
if err := conn.WriteMessage(""); err.Error() != "test-error" {
69+
t.Error("should return error if write fails")
70+
}
71+
}
72+
73+
func Test_SockJSConn_WriteMessage_MarshalError(t *testing.T) {
74+
// TODO => create a Marshal error
75+
}

server/conn_ws.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package server
2+
3+
import "golang.org/x/net/websocket"
4+
5+
type WSConn struct {
6+
ws *websocket.Conn
7+
}
8+
9+
func (c *WSConn) ReadMessage() (Message, error) {
10+
msg := Message{}
11+
err := websocket.JSON.Receive(c.ws, &msg)
12+
return msg, err
13+
}
14+
15+
func (c *WSConn) WriteMessage(msg interface{}) error {
16+
return websocket.JSON.Send(c.ws, msg)
17+
}

server/conn_ws_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package server
2+
3+
import "testing"
4+
5+
func Test_WSConn_ReadMessage(t *testing.T) {
6+
// TODO => write test
7+
}
8+
9+
func Test_WSConn_ReadMessage_ReadError(t *testing.T) {
10+
// TODO => write test
11+
}
12+
13+
func Test_WSConn_ReadMessage_UnmarshalError(t *testing.T) {
14+
// TODO => write test
15+
}
16+
17+
func Test_WSConn_WriteMessage(t *testing.T) {
18+
// TODO => write test
19+
}
20+
21+
func Test_WSConn_WriteMessage_WriteError(t *testing.T) {
22+
// TODO => write test
23+
}
24+
25+
func Test_WSConn_WriteMessage_MarshalError(t *testing.T) {
26+
// TODO => write test
27+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package integration
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/meteorhacks/goddp/server"
8+
)
9+
10+
var (
11+
WSURL = "http://localhost:1337/websocket"
12+
ORIGIN = "http://localhost:1337"
13+
ADDR = "localhost:1337"
14+
s server.Server
15+
)
16+
17+
type MethodError struct {
18+
Error string `json:"error"`
19+
}
20+
21+
type Message struct {
22+
Msg string `json:"msg"`
23+
Session string `json:"session"`
24+
ID string `json:"id"`
25+
Result float64 `json:"result"`
26+
Error MethodError `json:"error"`
27+
}
28+
29+
func Test_StartServer(t *testing.T) {
30+
s = server.New()
31+
32+
s.Method("double", func(ctx server.MethodContext) {
33+
n, ok := ctx.Params[0].(float64)
34+
35+
if !ok {
36+
ctx.SendError("invalid parameters")
37+
} else {
38+
ctx.SendResult(n * 2)
39+
}
40+
41+
ctx.SendUpdated()
42+
})
43+
44+
go s.Listen(":1337")
45+
time.Sleep(100 * time.Millisecond)
46+
}

server/integration_test/integration_test.go renamed to server/integration_test/websocket_test.go

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,11 @@ import (
88
"net/http"
99
"net/url"
1010
"testing"
11-
"time"
1211

1312
"github.com/gorilla/websocket"
14-
"github.com/meteorhacks/goddp/server"
1513
)
1614

17-
var (
18-
URL = "http://localhost:1337/websocket"
19-
ORIGIN = "http://localhost:1337"
20-
ADDR = "localhost:1337"
21-
s server.Server
22-
)
23-
24-
type MethodError struct {
25-
Error string `json:"error"`
26-
}
27-
28-
type Message struct {
29-
Msg string `json:"msg"`
30-
Session string `json:"session"`
31-
ID string `json:"id"`
32-
Result float64 `json:"result"`
33-
Error MethodError `json:"error"`
34-
}
35-
36-
func TestStartServer(t *testing.T) {
37-
s = server.New()
38-
39-
s.Method("double", func(ctx server.MethodContext) {
40-
n, ok := ctx.Params[0].(float64)
41-
42-
if !ok {
43-
ctx.SendError("invalid parameters")
44-
} else {
45-
ctx.SendResult(n * 2)
46-
}
47-
48-
ctx.SendUpdated()
49-
})
50-
51-
go s.Listen(":1337")
52-
time.Sleep(100 * time.Millisecond)
53-
}
54-
55-
func TestConnect(t *testing.T) {
15+
func Test_WS_Connect(t *testing.T) {
5616
ws, err := newClient()
5717
if err != nil {
5818
t.Error("websocket connection failed")
@@ -72,7 +32,7 @@ func TestConnect(t *testing.T) {
7232
}
7333
}
7434

75-
func TestPingWithoutId(t *testing.T) {
35+
func Test_WS_PingWithoutId(t *testing.T) {
7636
ws, err := newClient()
7737
if err != nil {
7838
t.Error("websocket connection failed")
@@ -91,7 +51,7 @@ func TestPingWithoutId(t *testing.T) {
9151
}
9252
}
9353

94-
func TestPingWithId(t *testing.T) {
54+
func Test_WS_PingWithId(t *testing.T) {
9555
ws, err := newClient()
9656
if err != nil {
9757
t.Error("websocket connection failed")
@@ -114,7 +74,7 @@ func TestPingWithId(t *testing.T) {
11474
}
11575
}
11676

117-
func TestMethodResult(t *testing.T) {
77+
func Test_WS_MethodResult(t *testing.T) {
11878
ws, err := newClient()
11979
if err != nil {
12080
t.Error("websocket connection failed")
@@ -141,7 +101,7 @@ func TestMethodResult(t *testing.T) {
141101
}
142102
}
143103

144-
func TestMethodError(t *testing.T) {
104+
func Test_WS_MethodError(t *testing.T) {
145105
ws, err := newClient()
146106
if err != nil {
147107
t.Error("websocket connection failed")
@@ -169,7 +129,7 @@ func TestMethodError(t *testing.T) {
169129
}
170130

171131
func newClient() (*websocket.Conn, error) {
172-
u, _ := url.Parse(URL)
132+
u, _ := url.Parse(WSURL)
173133
conn, err := net.Dial("tcp", ADDR)
174134

175135
if err != nil {

server/method_context.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type MethodContext struct {
1010
Updated bool
1111
}
1212

13-
func NewMethodContext(m Message, conn Connection) MethodContext {
13+
func NewMethodContext(m *Message, conn Connection) MethodContext {
1414
ctx := MethodContext{}
1515
ctx.ID = m.ID
1616
ctx.Params = m.Params
@@ -31,7 +31,7 @@ func (ctx *MethodContext) SendResult(result interface{}) error {
3131
"result": result,
3232
}
3333

34-
return ctx.Conn.WriteJSON(msg)
34+
return ctx.Conn.WriteMessage(msg)
3535
}
3636

3737
func (ctx *MethodContext) SendError(e string) error {
@@ -49,7 +49,7 @@ func (ctx *MethodContext) SendError(e string) error {
4949
},
5050
}
5151

52-
return ctx.Conn.WriteJSON(msg)
52+
return ctx.Conn.WriteMessage(msg)
5353
}
5454

5555
func (ctx *MethodContext) SendUpdated() error {
@@ -64,5 +64,5 @@ func (ctx *MethodContext) SendUpdated() error {
6464
"methods": []string{ctx.ID},
6565
}
6666

67-
return ctx.Conn.WriteJSON(msg)
67+
return ctx.Conn.WriteMessage(msg)
6868
}

0 commit comments

Comments
 (0)