Skip to content

Commit 03e4a7e

Browse files
committed
[fix] Terminology update
Switched from "message" to "topic" for better clarity.
1 parent 26f2ac5 commit 03e4a7e

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

socket.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ func (s *Socket) Close() error {
125125
}
126126

127127
// Send is a helper method to send a message to current socket
128-
func (s *Socket) Send(message string, payload []byte) error {
129-
return s.subp.SendToSocket(s, message, payload)
128+
func (s *Socket) Send(topic string, payload []byte) error {
129+
return s.subp.SendToSocket(s, topic, payload)
130130
}
131131

132132
// SendToClient is a helper method to send a message to all sockets of the same client
133-
func (s *Socket) SendToClient(message string, payload []byte) error {
134-
return s.subp.SendToClient(s.clientID, message, payload)
133+
func (s *Socket) SendToClient(topic string, payload []byte) error {
134+
return s.subp.SendToClient(s.clientID, topic, payload)
135135
}

subprotocol.go

+22-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
)
88

99
var (
10-
ErrUnregisteredSender = errors.New("websocket: unregistered sender")
1110
ErrMessageTooLong = errors.New("websocket: message too long")
1211
)
1312

@@ -37,11 +36,11 @@ func newSubProtocol(name string) *Subprotocol {
3736
// Handle defines the handler for a message. Handlers are not called
3837
// concurrently and will block further messages from being handled on the same
3938
// 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))
4342
}
44-
s.handlers[message] = handler
43+
s.handlers[topic] = handler
4544
}
4645

4746
func (s *Subprotocol) newConnection(r *http.Request, socket *Socket) {
@@ -55,43 +54,43 @@ func (s *Subprotocol) newConnection(r *http.Request, socket *Socket) {
5554
}
5655
}
5756

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) {
5958
var msgLen uint8
60-
if l := len(message); l < 256 {
59+
if l := len(topic); l < 256 {
6160
msgLen = uint8(l)
6261
} else {
6362
return nil, ErrMessageTooLong
6463
}
6564

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)
7067
msg[0] = msgLen
68+
copy(msg[1:], topic)
69+
copy(msg[msgLen+1:], payload)
7170

7271
return payload, nil
7372
}
7473

75-
func (s *Subprotocol) decodeMessage(msg []byte) (message string, payload []byte) {
74+
func (s *Subprotocol) decodeMessage(msg []byte) (topic string, payload []byte) {
7675
msgLen := msg[0]
7776
return string(msg[1:msgLen+1]), msg[msgLen+1:]
7877
}
7978

8079
func (s *Subprotocol) handleMessage(socket *Socket, msg []byte) {
81-
message, payload := s.decodeMessage(msg)
80+
topic, payload := s.decodeMessage(msg)
8281

83-
if handler, ok := s.handlers[message]; ok {
82+
if handler, ok := s.handlers[topic]; ok {
8483
handler(socket, payload)
8584
return
8685
}
8786

8887
if s.FallbackHandler != nil {
89-
s.FallbackHandler(socket, message, payload)
88+
s.FallbackHandler(socket, topic, payload)
9089
}
9190
}
9291

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)
9594
if err != nil {
9695
return err
9796
}
@@ -100,8 +99,8 @@ func (s *Subprotocol) SendToSocket(socket *Socket, message string, payload []byt
10099
return nil
101100
}
102101

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)
105104
if err != nil {
106105
return err
107106
}
@@ -115,8 +114,8 @@ func (s *Subprotocol) SendToClient(id string, message string, payload []byte) er
115114
return nil
116115
}
117116

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)
120119
if err != nil {
121120
return err
122121
}
@@ -132,8 +131,8 @@ func (s *Subprotocol) SendToRoom(name string, message string, payload []byte) er
132131
return nil
133132
}
134133

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)
137136
if err != nil {
138137
return err
139138
}

0 commit comments

Comments
 (0)