mirror of
https://github.com/AynaLivePlayer/blivedm-go.git
synced 2025-12-06 19:32:49 +08:00
✨ 重连自动尝试更换host
This commit is contained in:
@@ -18,6 +18,7 @@ type Client struct {
|
|||||||
realRoomID string
|
realRoomID string
|
||||||
token string
|
token string
|
||||||
host string
|
host string
|
||||||
|
hostList []string
|
||||||
eventHandlers *eventHandlers
|
eventHandlers *eventHandlers
|
||||||
customEventHandlers *customEventHandlers
|
customEventHandlers *customEventHandlers
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
@@ -36,6 +37,7 @@ func NewClient(roomID string) *Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Connect() error {
|
func (c *Client) Connect() error {
|
||||||
|
retryCount := 0
|
||||||
rid, _ := strconv.Atoi(c.roomID)
|
rid, _ := strconv.Atoi(c.roomID)
|
||||||
if rid <= 1000 && c.realRoomID == "" {
|
if rid <= 1000 && c.realRoomID == "" {
|
||||||
realID, err := api.GetRoomRealID(c.roomID)
|
realID, err := api.GetRoomRealID(c.roomID)
|
||||||
@@ -50,13 +52,18 @@ func (c *Client) Connect() error {
|
|||||||
if c.host == "" {
|
if c.host == "" {
|
||||||
info, err := api.GetDanmuInfo(c.realRoomID)
|
info, err := api.GetDanmuInfo(c.realRoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
c.hostList = []string{"broadcastlv.chat.bilibili.com"}
|
||||||
|
} else {
|
||||||
|
for _, h := range info.Data.HostList {
|
||||||
|
c.hostList = append(c.hostList, h.Host)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c.host = fmt.Sprintf("wss://%s/sub", info.Data.HostList[0].Host)
|
|
||||||
c.token = info.Data.Token
|
c.token = info.Data.Token
|
||||||
}
|
}
|
||||||
retry:
|
retry:
|
||||||
conn, res, err := websocket.DefaultDialer.Dial(c.host, nil)
|
c.host = c.hostList[retryCount%len(c.hostList)]
|
||||||
|
retryCount++
|
||||||
|
conn, res, err := websocket.DefaultDialer.Dial(fmt.Sprintf("wss://%s/sub", c.host), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("connect dial failed, retry...")
|
log.Error("connect dial failed, retry...")
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
@@ -117,8 +124,9 @@ func (c *Client) SetHost(host string) {
|
|||||||
c.host = host
|
c.host = host
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UseDefaultHost 使用默认 host broadcastlv.chat.bilibili.com
|
||||||
func (c *Client) UseDefaultHost() {
|
func (c *Client) UseDefaultHost() {
|
||||||
c.SetHost("wss://broadcastlv.chat.bilibili.com/sub")
|
c.SetHost("broadcastlv.chat.bilibili.com")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) heartBeat() {
|
func (c *Client) heartBeat() {
|
||||||
|
|||||||
Reference in New Issue
Block a user