7
7
)
8
8
9
9
var (
10
- ErrUnregisteredSender = errors .New ("websocket: unregistered sender" )
11
10
ErrMessageTooLong = errors .New ("websocket: message too long" )
12
11
)
13
12
@@ -37,11 +36,11 @@ func newSubProtocol(name string) *Subprotocol {
37
36
// Handle defines the handler for a message. Handlers are not called
38
37
// concurrently and will block further messages from being handled on the same
39
38
// Socket to ensure message ordering.
40
- func (s * Subprotocol ) Handle (message string , handler MessageHandler ) {
41
- if _ , ok := s .handlers [message ]; ok {
42
- panic (fmt .Sprintf ("websocket: duplicate handler registered (%s)" , message ))
39
+ func (s * Subprotocol ) Handle (topic string , handler MessageHandler ) {
40
+ if _ , ok := s .handlers [topic ]; ok {
41
+ panic (fmt .Sprintf ("websocket: duplicate handler registered (%s)" , topic ))
43
42
}
44
- s .handlers [message ] = handler
43
+ s .handlers [topic ] = handler
45
44
}
46
45
47
46
func (s * Subprotocol ) newConnection (r * http.Request , socket * Socket ) {
@@ -55,43 +54,43 @@ func (s *Subprotocol) newConnection(r *http.Request, socket *Socket) {
55
54
}
56
55
}
57
56
58
- func (s * Subprotocol ) encodeMessage (message string , payload []byte ) (msg []byte , err error ) {
57
+ func (s * Subprotocol ) encodeMessage (topic string , payload []byte ) (msg []byte , err error ) {
59
58
var msgLen uint8
60
- if l := len (message ); l < 256 {
59
+ if l := len (topic ); l < 256 {
61
60
msgLen = uint8 (l )
62
61
} else {
63
62
return nil , ErrMessageTooLong
64
63
}
65
64
66
- // Prepend message length and message name to payload
67
- msg = make ([]byte , len (payload )+ len (message )+ 1 )
68
- copy (msg [msgLen + 1 :], payload )
69
- copy (msg [1 :], message )
65
+ // Prepend topic length and topic name to payload
66
+ msg = make ([]byte , len (payload )+ len (topic )+ 1 )
70
67
msg [0 ] = msgLen
68
+ copy (msg [1 :], topic )
69
+ copy (msg [msgLen + 1 :], payload )
71
70
72
71
return payload , nil
73
72
}
74
73
75
- func (s * Subprotocol ) decodeMessage (msg []byte ) (message string , payload []byte ) {
74
+ func (s * Subprotocol ) decodeMessage (msg []byte ) (topic string , payload []byte ) {
76
75
msgLen := msg [0 ]
77
76
return string (msg [1 :msgLen + 1 ]), msg [msgLen + 1 :]
78
77
}
79
78
80
79
func (s * Subprotocol ) handleMessage (socket * Socket , msg []byte ) {
81
- message , payload := s .decodeMessage (msg )
80
+ topic , payload := s .decodeMessage (msg )
82
81
83
- if handler , ok := s .handlers [message ]; ok {
82
+ if handler , ok := s .handlers [topic ]; ok {
84
83
handler (socket , payload )
85
84
return
86
85
}
87
86
88
87
if s .FallbackHandler != nil {
89
- s .FallbackHandler (socket , message , payload )
88
+ s .FallbackHandler (socket , topic , payload )
90
89
}
91
90
}
92
91
93
- func (s * Subprotocol ) SendToSocket (socket * Socket , message string , payload []byte ) error {
94
- msg , err := s .encodeMessage (message , payload )
92
+ func (s * Subprotocol ) SendToSocket (socket * Socket , topic string , payload []byte ) error {
93
+ msg , err := s .encodeMessage (topic , payload )
95
94
if err != nil {
96
95
return err
97
96
}
@@ -100,8 +99,8 @@ func (s *Subprotocol) SendToSocket(socket *Socket, message string, payload []byt
100
99
return nil
101
100
}
102
101
103
- func (s * Subprotocol ) SendToClient (id string , message string , payload []byte ) error {
104
- msg , err := s .encodeMessage (message , payload )
102
+ func (s * Subprotocol ) SendToClient (id string , topic string , payload []byte ) error {
103
+ msg , err := s .encodeMessage (topic , payload )
105
104
if err != nil {
106
105
return err
107
106
}
@@ -115,8 +114,8 @@ func (s *Subprotocol) SendToClient(id string, message string, payload []byte) er
115
114
return nil
116
115
}
117
116
118
- func (s * Subprotocol ) SendToRoom (name string , message string , payload []byte ) error {
119
- msg , err := s .encodeMessage (message , payload )
117
+ func (s * Subprotocol ) SendToRoom (name string , topic string , payload []byte ) error {
118
+ msg , err := s .encodeMessage (topic , payload )
120
119
if err != nil {
121
120
return err
122
121
}
@@ -132,8 +131,8 @@ func (s *Subprotocol) SendToRoom(name string, message string, payload []byte) er
132
131
return nil
133
132
}
134
133
135
- func (s * Subprotocol ) Broadcast (message string , payload []byte ) error {
136
- msg , err := s .encodeMessage (message , payload )
134
+ func (s * Subprotocol ) Broadcast (topic string , payload []byte ) error {
135
+ msg , err := s .encodeMessage (topic , payload )
137
136
if err != nil {
138
137
return err
139
138
}
0 commit comments