added simple event handler.
This commit is contained in:
		
							
								
								
									
										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) | ||||
| 	} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user