You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
51 lines
1.1 KiB
package eventsub |
|
|
|
import ( |
|
"encoding/json" |
|
"fmt" |
|
|
|
"github.com/gorilla/websocket" |
|
) |
|
|
|
type WelcomeMessage struct { |
|
Metadata struct { |
|
MessageType string `json:"message_type"` |
|
MessageID string `json:"message_id"` |
|
} `json:"metadata"` |
|
Payload struct { |
|
Session struct { |
|
ID string `json:"id"` |
|
Status string `json:"status"` |
|
} `json:"session"` |
|
} `json:"payload"` |
|
} |
|
|
|
func Connect() (*websocket.Conn, error) { |
|
conn, _, err := websocket.DefaultDialer.Dial("wss://eventsub.wss.twitch.tv/ws", nil) |
|
return conn, err |
|
} |
|
|
|
func HandleMessages(conn *websocket.Conn) { |
|
for { |
|
_, message, err := conn.ReadMessage() |
|
if err != nil { |
|
fmt.Printf("Read error: %v", err) |
|
return |
|
} |
|
|
|
var msg WelcomeMessage |
|
if err := json.Unmarshal(message, &msg); err != nil { |
|
fmt.Printf("JSON error: %v", err) |
|
continue |
|
} |
|
|
|
switch msg.Metadata.MessageType { |
|
case "session_welcome": |
|
sessionID := msg.Payload.Session.ID |
|
fmt.Printf("Session ID: %s", sessionID) |
|
// Use this session ID to create subscriptions |
|
case "notification": |
|
// Handle actual events here |
|
} |
|
} |
|
}
|
|
|