forked from sleeyax/aternos-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (81 loc) · 2.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// This example demonstrates how to start and stop a server over websockets.
// It's a little more advanced but the most performant and controllable way to use this package.
package main
import (
"context"
"encoding/json"
aternos "github.com/ganiskowicz/aternos-api"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
// session & server id are set by environment variables.
session := os.Getenv("session")
server := os.Getenv("server")
if session == "" || server == "" {
log.Fatalln("not all environment variables are set!")
}
// Create new Aternos api instance, providing the required authentication cookies.
api := aternos.New(&aternos.Options{
Cookies: []*http.Cookie{
{
Name: "ATERNOS_LANGUAGE",
Value: "en",
},
{
Name: "ATERNOS_SESSION",
Value: session,
},
{
Name: "ATERNOS_SERVER",
Value: server,
},
},
})
// Connect to the Aternos websocket server.
wss, err := api.ConnectWebSocket()
if err != nil {
log.Fatal(err)
}
defer wss.Close() // closes the connection when the main function ends
ctx, cancel := context.WithCancel(context.Background())
interruptSignal := make(chan os.Signal, 1)
signal.Notify(interruptSignal, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
log.Println("Started websocket connection.")
for {
select {
case msg := <-wss.Message:
switch msg.Type {
case "ready":
// Start the server over HTTP.
if err = api.StartServer(); err != nil {
log.Println(err)
return
}
// Run a goroutine in the background that sends a bunch of keep-alive requests at default intervals.
go wss.SendHearthBeats(ctx)
case "status":
// Current server status, containing a bunch of other useful info such as IP address/Dyn IP to connect to, amount of active players, detected problems etc.
var info aternos.ServerInfo
json.Unmarshal(msg.MessageBytes, &info)
log.Printf("Server status: %s\n", info.StatusLabel)
if info.Status == aternos.Online {
log.Println("Name:", info.Name)
log.Println("Dyn IP:", info.DynIP)
log.Println("Address:", info.Address)
log.Println("Port:", info.Port)
}
}
// Stop the server, close the connection & quit the app when CTRL + C is pressed.
case <-interruptSignal:
if err = api.StopServer(); err != nil {
log.Println(err)
}
cancel() // stop sending heartbeats
return
}
}
}