added simple event handler.
This commit is contained in:
parent
27584269dd
commit
f91c9f9554
@ -13,4 +13,12 @@ const (
|
||||
type Client struct {
|
||||
Token string
|
||||
Socket gowebsocket.Socket
|
||||
|
||||
// Functions
|
||||
OnReadyFunction func()
|
||||
}
|
||||
|
||||
// On Ready event will run when websocket connection is started and bot is ready to work.
|
||||
func (c *Client) OnReady(fn func()) {
|
||||
c.OnReadyFunction = fn
|
||||
}
|
||||
|
22
websocket.go
22
websocket.go
@ -1,12 +1,18 @@
|
||||
package revoltgo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/sacOO7/gowebsocket"
|
||||
)
|
||||
|
||||
// Dummy struct for parse gateway events.
|
||||
type GatewayType struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
func (c *Client) Start() {
|
||||
// Create new socket
|
||||
c.Socket = gowebsocket.New(WS_URL)
|
||||
@ -17,10 +23,24 @@ func (c *Client) Start() {
|
||||
}
|
||||
|
||||
c.Socket.OnTextMessage = func(message string, _ gowebsocket.Socket) {
|
||||
if message == "{\"type\":\"Authenticated\"}" {
|
||||
// Parse data
|
||||
rawData := &GatewayType{}
|
||||
err := json.Unmarshal([]byte(message), rawData)
|
||||
|
||||
if err != nil {
|
||||
c.Destroy()
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if rawData.Type == "Authenticated" {
|
||||
go c.ping()
|
||||
}
|
||||
|
||||
// Check events
|
||||
if rawData.Type == "Ready" {
|
||||
c.OnReadyFunction()
|
||||
}
|
||||
|
||||
fmt.Println(message)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user