@ -46,3 +46,8 @@ func (c *Client) WriteJson(body interface{}) error {
}
return c.Write(bs)
func (c *Client) CloseWithJson(body interface{}) {
bs, _ := json.Marshal(&body)
c.CloseWithMsg(bs)
@ -115,6 +115,16 @@ func (t *Node) RegisterClient(c *Client) error {
if room, ok := t.rooms[c.RoomId]; ok {
room.rwmux.Lock()
defer room.rwmux.Unlock()
if oc, exist := room.clients[c.Id]; exist {
oc.WriteJson(&Message{
Type: "close",
Data: map[string]interface{}{
"error_code": 302,
"error_msg": "链接已被替换",
},
})
oc.Close()
room.clients[c.Id] = c
return nil