diff --git a/hdws/main_2 b/hdws/main_2 deleted file mode 100644 index f410640..0000000 Binary files a/hdws/main_2 and /dev/null differ diff --git a/hdws/ws/client.go b/hdws/ws/client.go index e092440..3783b52 100644 --- a/hdws/ws/client.go +++ b/hdws/ws/client.go @@ -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) +} diff --git a/hdws/ws/node.go b/hdws/ws/node.go index 2fac40b..08acdcb 100644 --- a/hdws/ws/node.go +++ b/hdws/ws/node.go @@ -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 }