fixed websocket close bug.

This commit is contained in:
5elenay 2021-08-20 19:26:12 +03:00
parent 267e3baa93
commit 3adfcd339a

View File

@ -2,6 +2,7 @@ package revoltgo
import (
"fmt"
"time"
"github.com/sacOO7/gowebsocket"
)
@ -13,10 +14,31 @@ func (c *Client) Start() {
c.Socket = gowebsocket.New(WS_URL)
// Send auth when connected
c.Socket.OnConnected = func(socket gowebsocket.Socket) {
c.Socket.OnConnected = func(_ gowebsocket.Socket) {
c.Socket.SendText(fmt.Sprintf("{\"type\": \"Authenticate\", \"token\": \"%s\"}", c.Token))
}
c.Socket.OnTextMessage = func(message string, _ gowebsocket.Socket) {
if message == "{\"type\":\"Authenticated\"}" {
go c.ping()
}
fmt.Println(message)
}
// Start connection
c.Socket.Connect()
}
// Destroy the websocket.
func (c *Client) Destroy() {
c.Socket.Close()
}
// Ping websocket.
func (c *Client) ping() {
for {
time.Sleep(30 * time.Second)
c.Socket.SendText(fmt.Sprintf("{\"type\":\"Ping\",\"time\":%d}", time.Now().Unix()))
}
}