forked from pschlump/socketio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
98 lines (82 loc) · 2.14 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package websocket
import (
"io"
"net/http"
"github.com/SnowpMakes/socketio/engineio/message"
"github.com/SnowpMakes/socketio/engineio/parser"
"github.com/SnowpMakes/socketio/engineio/transport"
"github.com/gorilla/websocket"
)
type Server struct {
callback transport.Callback
conn *websocket.Conn
}
/*
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { // Origin - PJS - Check
return true
// allow all connections by default - to do a test - a public site will need to have a "check-origin" policy
// that is based on users inputing the site that they will be using - that will get "pushed" and that will be valid for
// the origin.
//
// See data/go-chat-model.sql
//
},
}
*/
func NewServer(w http.ResponseWriter, r *http.Request, callback transport.Callback) (transport.Server, error) {
conn, err := websocket.Upgrade(w, r, nil, 10240, 10240) // Origin is NIL parameter?? PJS
if err != nil {
return nil, err
}
ret := &Server{
callback: callback,
conn: conn,
}
go ret.serveHTTP(w, r)
return ret, nil
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
}
func (s *Server) NextWriter(msgType message.MessageType, packetType parser.PacketType) (io.WriteCloser, error) {
wsType, newEncoder := websocket.TextMessage, parser.NewStringEncoder
if msgType == message.MessageBinary {
wsType, newEncoder = websocket.BinaryMessage, parser.NewBinaryEncoder
}
w, err := s.conn.NextWriter(wsType)
if err != nil {
return nil, err
}
ret, err := newEncoder(w, packetType)
if err != nil {
return nil, err
}
return ret, nil
}
func (s *Server) Close() error {
return s.conn.Close()
}
func (s *Server) serveHTTP(w http.ResponseWriter, r *http.Request) {
defer s.callback.OnClose(s)
for {
t, r, err := s.conn.NextReader()
if err != nil {
s.conn.Close()
return
}
switch t {
case websocket.TextMessage:
fallthrough
case websocket.BinaryMessage:
decoder, err := parser.NewDecoder(r)
if err != nil {
return
}
s.callback.OnPacket(decoder)
decoder.Close()
}
}
}