Skip to content

Commit 7407772

Browse files
committed
stop send message signal channel
1 parent 0e46746 commit 7407772

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

client.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ type ClientFace interface {
2929
Rooms() map[string]bool // get all rooms joined by the client
3030
Ping() map[int64]bool // get ping
3131
Delay() int64 // obtain a time delay that reflects the quality of the connection between the two ends
32-
Out() chan string // get the message send channel
32+
Out() chan string // message send channel
33+
StopOut() chan bool // stop send message signal channel
3334
SetPing(map[int64]bool) // set ping
3435
SetDelay(int64) // set delay
3536
SetRemoteAddr(net.Addr) // set remoteAddr
@@ -41,6 +42,7 @@ type Client struct {
4142
acceptor *Acceptor // event processing function register
4243
rooms *sync.Map // map[string]bool all rooms joined by the client, used to quickly join and leave the rooms
4344
out chan string // message send channel
45+
stopOut chan bool // stop send message signal channel
4446
ping map[int64]bool // ping
4547
delay int64 // delay
4648
}
@@ -50,6 +52,7 @@ func (c *Client) Init(a *Acceptor) {
5052
c.acceptor = a
5153
// set a capacity N for the data transmission pipeline as a buffer. if the client has not received it, the pipeline will always keep the latest N
5254
c.out = make(chan string, 500)
55+
c.stopOut = make(chan bool)
5356
c.rooms = new(sync.Map)
5457
c.ping = make(map[int64]bool)
5558
}
@@ -87,6 +90,10 @@ func (c *Client) Out() chan string {
8790
return c.out
8891
}
8992

93+
func (c *Client) StopOut() chan bool {
94+
return c.stopOut
95+
}
96+
9097
func (c *Client) SetPing(v map[int64]bool) {
9198
c.ping = v
9299
}
@@ -113,7 +120,16 @@ func (c *Client) Emit(event string, args interface{}, id string) {
113120
log.Println("[GoSocket][Emit] encode error:", err, event, args, id, c.Id(), c.RemoteAddr())
114121
return
115122
}
116-
c.out <- msg
123+
select {
124+
case <-c.stopOut:
125+
// close(c.out)
126+
// The channel of c.out will close itself when there is no goroutine reference
127+
// so, no need to close(c.out) here
128+
log.Println("receive the stop signal, the socket was closed", c.Id(), c.RemoteAddr())
129+
return
130+
case c.out <- msg:
131+
log.Println("msg send", msg, c.Id(), c.RemoteAddr())
132+
}
117133
}
118134

119135
func (c *Client) EmitByInitiator(i *Initiator, event string, args interface{}, id string) {

room.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ func (r *rooms) RemoveAll(c *Client) {
8181
}
8282
return true
8383
})
84-
// Finally close the send channel
85-
close(c.out)
8684
}
8785

8886
// broadcast message to room

tcpsocket/client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ func (c *Client) write() {
6262
defer func() {
6363
ticker.Stop()
6464
c.conn.Close()
65-
// the data transmission channel (c.Out) is not closed here,
66-
// it will be handled uniformly by read method
65+
// Give a signal to the sender(Emit)
66+
// Here is the consumer program of the channel c.Out()
67+
// Can not close c.Out() here
68+
// c.Out() channel must be close by it's sender
69+
close(c.StopOut())
6770
}()
6871
for {
6972
select {

websocket/client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ func (c *Client) write() {
9292
defer func() {
9393
ticker.Stop()
9494
c.conn.Close()
95-
// the data transmission channel (c.Out) is not closed here,
96-
// it will be handled uniformly by read method
95+
close(c.StopOut())
96+
// Give a signal to the sender(Emit)
97+
// Here is the consumer program of the channel c.Out()
98+
// Can not close c.Out() here
99+
// c.Out() channel must be close by it's sender
97100
}()
98101
for {
99102
select {

0 commit comments

Comments
 (0)