fixed websocket close bug.
This commit is contained in:
parent
267e3baa93
commit
3adfcd339a
24
websocket.go
24
websocket.go
@ -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()))
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user