added simple event handler.
This commit is contained in:
parent
27584269dd
commit
f91c9f9554
@ -13,4 +13,12 @@ const (
|
|||||||
type Client struct {
|
type Client struct {
|
||||||
Token string
|
Token string
|
||||||
Socket gowebsocket.Socket
|
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
|
package revoltgo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sacOO7/gowebsocket"
|
"github.com/sacOO7/gowebsocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Dummy struct for parse gateway events.
|
||||||
|
type GatewayType struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) Start() {
|
func (c *Client) Start() {
|
||||||
// Create new socket
|
// Create new socket
|
||||||
c.Socket = gowebsocket.New(WS_URL)
|
c.Socket = gowebsocket.New(WS_URL)
|
||||||
@ -17,10 +23,24 @@ func (c *Client) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.Socket.OnTextMessage = func(message string, _ gowebsocket.Socket) {
|
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()
|
go c.ping()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check events
|
||||||
|
if rawData.Type == "Ready" {
|
||||||
|
c.OnReadyFunction()
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(message)
|
fmt.Println(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user