@@ -29,7 +29,8 @@ type ClientFace interface {
29
29
Rooms () map [string ]bool // get all rooms joined by the client
30
30
Ping () map [int64 ]bool // get ping
31
31
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
33
34
SetPing (map [int64 ]bool ) // set ping
34
35
SetDelay (int64 ) // set delay
35
36
SetRemoteAddr (net.Addr ) // set remoteAddr
@@ -41,6 +42,7 @@ type Client struct {
41
42
acceptor * Acceptor // event processing function register
42
43
rooms * sync.Map // map[string]bool all rooms joined by the client, used to quickly join and leave the rooms
43
44
out chan string // message send channel
45
+ stopOut chan bool // stop send message signal channel
44
46
ping map [int64 ]bool // ping
45
47
delay int64 // delay
46
48
}
@@ -50,6 +52,7 @@ func (c *Client) Init(a *Acceptor) {
50
52
c .acceptor = a
51
53
// 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
52
54
c .out = make (chan string , 500 )
55
+ c .stopOut = make (chan bool )
53
56
c .rooms = new (sync.Map )
54
57
c .ping = make (map [int64 ]bool )
55
58
}
@@ -87,6 +90,10 @@ func (c *Client) Out() chan string {
87
90
return c .out
88
91
}
89
92
93
+ func (c * Client ) StopOut () chan bool {
94
+ return c .stopOut
95
+ }
96
+
90
97
func (c * Client ) SetPing (v map [int64 ]bool ) {
91
98
c .ping = v
92
99
}
@@ -113,7 +120,16 @@ func (c *Client) Emit(event string, args interface{}, id string) {
113
120
log .Println ("[GoSocket][Emit] encode error:" , err , event , args , id , c .Id (), c .RemoteAddr ())
114
121
return
115
122
}
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
+ }
117
133
}
118
134
119
135
func (c * Client ) EmitByInitiator (i * Initiator , event string , args interface {}, id string ) {
0 commit comments