From 9a17498886b7a03555b2c5586c52346ec01fc45a Mon Sep 17 00:00:00 2001 From: mikuta0407 Date: Tue, 26 Nov 2024 16:34:29 +0900 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8B=95=E5=86=8D=E6=8E=A5=E7=B6=9A?= =?UTF-8?q?=E3=80=81PING=E3=81=B8=E3=81=AE=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- misskey/stream.go | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/misskey/stream.go b/misskey/stream.go index 97c1d1b..9cfd215 100644 --- a/misskey/stream.go +++ b/misskey/stream.go @@ -10,6 +10,7 @@ import ( "github.com/buger/jsonparser" "github.com/google/uuid" + "github.com/gorilla/websocket" "github.com/sacOO7/gowebsocket" ) @@ -45,12 +46,31 @@ func (c *Client) GetStream(mode string) error { } socket.OnDisconnected = func(err error, socket gowebsocket.Socket) { - log.Println("Disconnected from server ") - return + log.Println("Disconnected from server.") + socket.Connect() + initialConnect(socket, mode) + } + + socket.OnPingReceived = func(data string, socket gowebsocket.Socket) { + socket.SendBinary([]byte{websocket.PongMessage}) } socket.Connect() + initialConnect(socket, mode) + + for { + select { + case <-interrupt: + log.Println("interrupt") + socket.Close() + return nil + } + + } +} + +func initialConnect(socket gowebsocket.Socket, mode string) error { uu, err := uuid.NewRandom() if err != nil { return err @@ -73,16 +93,7 @@ func (c *Client) GetStream(mode string) error { return errors.New("Please select mode in local/home/global") } socket.SendText(channelText) - - for { - select { - case <-interrupt: - log.Println("interrupt") - socket.Close() - return nil - } - } - + return nil } func printNote(message string) {