Skip to content

Commit 952fec0

Browse files
committed
Refactored code base
1 parent 4f708b5 commit 952fec0

File tree

4 files changed

+54
-109
lines changed

4 files changed

+54
-109
lines changed

main.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@ package main
22

33
import (
44
"./server"
5-
"fmt"
6-
"net/http"
75
)
86

97
func main() {
108
server := server.New()
11-
129
server.Method("hello", func(p []interface{}) (interface{}, error) {
1310
return "result", nil
1411
})
1512

16-
http.HandleFunc("/websocket", server.Handler)
17-
http.ListenAndServe(":9000", nil)
13+
server.Listen(":9000")
1814
}

server/server.go

Lines changed: 22 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -4,110 +4,39 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"math/rand"
87
"net/http"
98

109
"github.com/gorilla/websocket"
1110
)
1211

13-
type MethodHandler func([]interface{}) (interface{}, error)
14-
1512
type Server struct {
16-
ws *websocket.Conn
17-
methods map[string]MethodHandler
18-
}
19-
20-
type Message struct {
21-
Msg string `json:"msg"`
22-
Session string `json:"session"`
23-
Version string `json:"version"`
24-
Support []string `json:"support"`
25-
Id string `json:"id"`
26-
Method string `json:"method"`
27-
Params []interface{} `json:"params"`
28-
Result string `json:"result"`
29-
Methods []string `json:"methods"`
30-
}
31-
32-
// type ConnectMessage struct {
33-
// Msg string `json:"msg"`
34-
// Session string `json:"session"`
35-
// Version string `json:"version"`
36-
// Support []string `json:"support"`
37-
// }
38-
39-
// type ConnectedMessage struct {
40-
// Msg string `json:"msg"`
41-
// Session string `json:"session"`
42-
// }
43-
44-
// type PingWithIdMessage struct {
45-
// Msg string `json:"msg"`
46-
// Id string `json:"id"`
47-
// }
48-
49-
// type PingWithoutIdMessage struct {
50-
// Msg string `json:"msg"`
51-
// }
52-
53-
// type PongWithIdMessage struct {
54-
// Msg string `json:"msg"`
55-
// Id string `json:"id"`
56-
// }
57-
58-
// type PongWithoutIdMessage struct {
59-
// Msg string `json:"msg"`
60-
// }
61-
62-
// type MethodMessage struct {
63-
// Msg string `json:"msg"`
64-
// Id string `json:"id"`
65-
// Method string `json:"method"`
66-
// Params []interface{} `json:"params"`
67-
// }
68-
69-
// type ResultWithErrorMessage struct {
70-
// Msg string `json:"msg"`
71-
// Id string `json:"id"`
72-
// Error Error `json:"error"`
73-
// }
74-
75-
// type ResultWithoutErrorMessage struct {
76-
// Msg string `json:"msg"`
77-
// Id string `json:"id"`
78-
// Result string `json:"result"`
79-
// }
80-
81-
// type UpdatedMessage struct {
82-
// Msg string `json:"msg"`
83-
// Methods []string `json:"methods"`
84-
// }
85-
86-
// type Error struct {
87-
// error string
88-
// reason string
89-
// details string
90-
// }
91-
92-
var upgrader = websocket.Upgrader{
93-
ReadBufferSize: 1024,
94-
WriteBufferSize: 1024,
13+
methods map[string]MethodHandler
14+
upgrader websocket.Upgrader
9515
}
9616

9717
func New() Server {
9818
server := Server{}
9919
server.methods = make(map[string]MethodHandler)
20+
server.upgrader = websocket.Upgrader{
21+
ReadBufferSize: 1024,
22+
WriteBufferSize: 1024,
23+
}
24+
10025
return server
10126
}
10227

10328
func (s *Server) Method(n string, h MethodHandler) {
10429
s.methods[n] = h
10530
}
10631

32+
func (s *Server) Listen(ipPort string) {
33+
http.HandleFunc("/websocket", s.handler)
34+
http.ListenAndServe(ipPort, nil)
35+
}
36+
10737
// create websocket connection from http handler and runs the websocket handler
108-
func (s *Server) Handler(w http.ResponseWriter, r *http.Request) {
109-
ws, err := upgrader.Upgrade(w, r, nil)
110-
s.ws = ws
38+
func (s *Server) handler(w http.ResponseWriter, r *http.Request) {
39+
ws, err := s.upgrader.Upgrade(w, r, nil)
11140

11241
if err != nil {
11342
fmt.Println("Error: could not creating websocket connection")
@@ -127,29 +56,29 @@ func (s *Server) Handler(w http.ResponseWriter, r *http.Request) {
12756

12857
switch {
12958
case msg.Msg == "ping":
130-
go s.HandlePing(ws, &msg)
59+
go s.handlePing(ws, &msg)
13160
case msg.Msg == "connect":
132-
go s.HandleConnect(ws, &msg)
61+
go s.handleConnect(ws, &msg)
13362
case msg.Msg == "method":
134-
go s.HandleMethod(ws, &msg)
63+
go s.handleMethod(ws, &msg)
13564
default:
13665
fmt.Println("Error: unknown ddp message", msg)
13766
}
13867
}
13968
}
14069

141-
func (s *Server) HandleConnect(c *websocket.Conn, m *Message) {
70+
func (s *Server) handleConnect(c *websocket.Conn, m *Message) {
14271
err := c.WriteJSON(map[string]string{
14372
"msg": "connected",
144-
"session": randId(17),
73+
"session": randomId(17),
14574
})
14675

14776
if err != nil {
14877
fmt.Println(err)
14978
}
15079
}
15180

152-
func (s *Server) HandlePing(c *websocket.Conn, m *Message) {
81+
func (s *Server) handlePing(c *websocket.Conn, m *Message) {
15382
if m.Id != "" {
15483
err := c.WriteJSON(map[string]string{
15584
"msg": "pong",
@@ -170,7 +99,7 @@ func (s *Server) HandlePing(c *websocket.Conn, m *Message) {
17099
}
171100
}
172101

173-
func (s *Server) HandleMethod(c *websocket.Conn, m *Message) {
102+
func (s *Server) handleMethod(c *websocket.Conn, m *Message) {
174103
res, _ := s.methods[m.Method](m.Params)
175104
err := c.WriteJSON(map[string]interface{}{
176105
"msg": "result",
@@ -203,21 +132,10 @@ func readMessage(ws *websocket.Conn) (Message, error) {
203132

204133
if t != 1 {
205134
// ignore binary data
206-
err = errors.New("Error: websocket data is binary")
135+
err = errors.New("Error: DDP does not supports binary streams yet.")
207136
return msg, err
208137
}
209138

210139
err = json.Unmarshal(str, &msg)
211140
return msg, err
212141
}
213-
214-
// TODO: move this to another package
215-
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
216-
217-
func randId(n int) string {
218-
b := make([]rune, n)
219-
for i := range b {
220-
b[i] = letters[rand.Intn(len(letters))]
221-
}
222-
return string(b)
223-
}

server/types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package server
2+
3+
type MethodHandler func([]interface{}) (interface{}, error)
4+
5+
// This has the all the possible messgae a DDP message can have
6+
type Message struct {
7+
Msg string `json:"msg"`
8+
Session string `json:"session"`
9+
Version string `json:"version"`
10+
Support []string `json:"support"`
11+
Id string `json:"id"`
12+
Method string `json:"method"`
13+
Params []interface{} `json:"params"`
14+
Result string `json:"result"`
15+
Methods []string `json:"methods"`
16+
}

server/utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package server
2+
3+
import (
4+
"math/rand"
5+
)
6+
7+
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
8+
9+
func randomId(n int) string {
10+
b := make([]rune, n)
11+
for i := range b {
12+
b[i] = letters[rand.Intn(len(letters))]
13+
}
14+
return string(b)
15+
}

0 commit comments

Comments
 (0)