-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceive-messages.go
133 lines (115 loc) · 3.18 KB
/
receive-messages.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"github.com/gorilla/websocket"
"os"
"encoding/json"
"fmt"
)
type Config struct {
Credentials struct {
Client string `json:"client"`
Password string `json:"password"`
} `json:"credentials"`
Host string `json:"host"`
GosumemoryURL string `json:"gosumemory_url"`
StreamCompanionURL string `json:"stream_companion_url"`
}
type AuthResponse struct {
Error struct {
Num int `json:"num"`
Str string `json:"str"`
} `json:"error"`
}
type ErrorResponse struct {
Error string `json:"error"`
}
type AuthError struct {
Message string
}
func (e AuthError) Error() string {
return fmt.Sprintf("auth: %v", e.Message)
}
func CheckAuthResponse(message []byte) (error) {
var json_data AuthResponse
err := json.Unmarshal(message, &json_data)
if err != nil {
return err
}
if (json_data.Error.Num != 0) {
return AuthError{json_data.Error.Str}
}
return nil
}
// TODO: set default server Url here too
func SetDefaultValuesToConfiguration(config *Config) {
if config.StreamCompanionURL == "" {
config.StreamCompanionURL = "http://localhost:20727/json"
}
if config.GosumemoryURL == "" {
config.GosumemoryURL = "http://localhost:24050/json"
}
}
func LoadConfiguration(file string) (Config, error) {
var config Config
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
return config, err
}
jsonParser := json.NewDecoder(configFile)
err = jsonParser.Decode(&config)
SetDefaultValuesToConfiguration(&config)
return config, err
}
func handle_command(command string, urls []string) ([]byte, error) {
if (command == "np") {
return getOsuData(urls), nil
}
return nil, nil
}
func connection_handler(url string, osu_urls []string, credentials []byte) (error) {
// Establish a WebSocket connection
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
return err
}
defer conn.Close()
// Send the authentication payload to the server
err = conn.WriteMessage(websocket.TextMessage, credentials)
if err != nil {
return err
}
_, message, err := conn.ReadMessage()
if err != nil {
return err
}
err = CheckAuthResponse(message)
if err != nil {
return err
}
// Handle incoming WebSocket messages
for {
_, message, err := conn.ReadMessage()
if err != nil {
return err
}
// Process the received message
response, err := handle_command(string(message), osu_urls)
// This currently disconnects client from server, not sure if that's desirable outcome
if err != nil {
error_response := &ErrorResponse{err.Error()}
bytes_response, _ := json.Marshal(error_response)
conn.WriteMessage(
websocket.TextMessage,
bytes_response,
)
return err
}
if response != nil {
err = conn.WriteMessage(websocket.TextMessage, response)
if err != nil {
return err
}
}
}
}