Skip to content

Commit

Permalink
feat:websocket增加ping和identify
Browse files Browse the repository at this point in the history
  • Loading branch information
dezhiShen authored and dezhiShen committed Oct 15, 2023
1 parent e22ca57 commit 9b559f5
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions pkg/client/event_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,36 @@ func NewWebsocketEventChannel(conf config.SatoriEventConfig) (EventTemplate, err
return result, nil
}

func (cli *WebsocketEventChannel) sendIDENTIFY() {
log.Info("send IDENTIFY")
err := cli.Conn.WriteJSON(map[string]interface{}{
"op": IDENTIFY,
"body": map[string]string{
"token": cli.accessToken,
},
})
if err != nil {
log.Errorf("IDENTIFY发送失败:%v", err)
}
}

func (cli *WebsocketEventChannel) startHeartbeat() {
for {
cli.sendPING()
time.Sleep(10 * time.Second)
}
}

func (cli *WebsocketEventChannel) sendPING() {
log.Info("send IDENTIFY")
err := cli.Conn.WriteJSON(map[string]interface{}{
"op": PING,
})
if err != nil {
log.Errorf("PING发送失败:%v", err)
}
}

func (cli *WebsocketEventChannel) StartListen(ctx context.Context, callback func(message []byte) error) error {
url := cli.addr
if cli.accessToken != "" {
Expand All @@ -46,16 +76,9 @@ func (cli *WebsocketEventChannel) StartListen(ctx context.Context, callback func
return err
}
log.Info("建立连接成功!")
log.Info("send IDENTIFY")
err = cli.Conn.WriteJSON(map[string]interface{}{
"op": 3,
"body": map[string]string{
"token": cli.accessToken,
},
})
if err != nil {
log.Errorf("IDENTIFY发送失败:%v", err)
}
go cli.sendIDENTIFY()
// loop to send ping
go cli.startHeartbeat()
defer cli.Conn.Close()
for {
select {
Expand Down

0 comments on commit 9b559f5

Please sign in to comment.