diff options
author | FxQnLr <[email protected]> | 2024-04-29 19:36:48 +0200 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-04-29 19:36:48 +0200 |
commit | ab76e442ab4fb4d41ccbc211f9ac05e19aaefd4b (patch) | |
tree | 20c5b52d8e2bb3a7ad00f6932d1a591bd22091fb | |
parent | 9233e47a7afa1bee87c53b2bff22bfc5a862c435 (diff) | |
download | funsaac-ab76e442ab4fb4d41ccbc211f9ac05e19aaefd4b.tar funsaac-ab76e442ab4fb4d41ccbc211f9ac05e19aaefd4b.tar.gz funsaac-ab76e442ab4fb4d41ccbc211f9ac05e19aaefd4b.zip |
connection, counter, messages and device scan
-rw-r--r-- | main.lua | 37 | ||||
-rw-r--r-- | messages.lua | 46 |
2 files changed, 70 insertions, 13 deletions
@@ -1,24 +1,35 @@ | |||
1 | ---@diagnostic disable: duplicate-set-field | ||
2 | |||
1 | local funsaac = RegisterMod("funsaac", 1) | 3 | local funsaac = RegisterMod("funsaac", 1) |
2 | local json = require("json") | 4 | require("messages") |
5 | |||
6 | local client = require("websocket").new("127.0.0.1", 12345, "/") | ||
3 | 7 | ||
4 | local requestServerInfo = { | 8 | CONNECTED = false |
5 | Id = 1, | ||
6 | ClientName = "Funsaac v.0.0.1", | ||
7 | MessageVersion = 3 | ||
8 | } | ||
9 | 9 | ||
10 | local client = require("websocket").new("127.0.0.1", 12345) | ||
11 | function client:onmessage(message) | 10 | function client:onmessage(message) |
12 | print(message) | 11 | HandleResponse(ResponseContent(message)) |
12 | if CONNECTED == true then | ||
13 | self:send(GetMessage(Messages.StartScanning)) | ||
14 | CONNECTED = false | ||
15 | end | ||
13 | end | 16 | end |
17 | |||
14 | function client:onopen() | 18 | function client:onopen() |
15 | local msg = '[{"RequestServerInfo": ' .. json.encode(requestServerInfo) .. "}]" | 19 | self:send(GetMessage(Messages.RequestServerInfo)) |
16 | print(msg) | ||
17 | self:send(msg) | ||
18 | end | 20 | end |
19 | 21 | ||
20 | local function onUpdate() | 22 | |
23 | local render_tick = 0 | ||
24 | local s = 0; | ||
25 | local function onRender() | ||
21 | client:update() | 26 | client:update() |
27 | |||
28 | render_tick = render_tick + 1 | ||
29 | if render_tick >= 60 then | ||
30 | render_tick = 0 | ||
31 | s = s + 1 | ||
32 | end | ||
22 | end | 33 | end |
23 | 34 | ||
24 | funsaac:AddCallback(ModCallbacks.MC_POST_UPDATE, onUpdate) \ No newline at end of file | 35 | funsaac:AddCallback(ModCallbacks.MC_POST_RENDER, onRender) \ No newline at end of file |
diff --git a/messages.lua b/messages.lua new file mode 100644 index 0000000..69576e1 --- /dev/null +++ b/messages.lua | |||
@@ -0,0 +1,46 @@ | |||
1 | local json = require("json") | ||
2 | |||
3 | Messages = { | ||
4 | -- Handshake | ||
5 | RequestServerInfo = { | ||
6 | RequestServerInfo = { | ||
7 | Id = 1, | ||
8 | ClientName = "Funsaac v0.0.2", | ||
9 | MessageVersion = 3 | ||
10 | } | ||
11 | }, | ||
12 | |||
13 | -- Enumeration | ||
14 | |||
15 | StartScanning = { | ||
16 | StartScanning = { | ||
17 | Id = 1 | ||
18 | } | ||
19 | } | ||
20 | } | ||
21 | |||
22 | local cnt = 1; | ||
23 | |||
24 | function GetMessage(msg) | ||
25 | local message = msg | ||
26 | message[next(msg)]["Id"] = cnt | ||
27 | cnt = cnt + 1 | ||
28 | return "[" .. json.encode(message) .. "]" | ||
29 | end | ||
30 | |||
31 | function ResponseContent(message) | ||
32 | local msg = json.decode(message)[1] | ||
33 | local type = next(msg) | ||
34 | return type, msg[type] | ||
35 | end | ||
36 | |||
37 | function HandleResponse(type, content) | ||
38 | if type == "ServerInfo" then | ||
39 | print("Connected to Server: " .. content["ServerName"]) | ||
40 | CONNECTED = true | ||
41 | elseif type == "Ok" then | ||
42 | print("Id: " .. content["Id"]) | ||
43 | elseif type == "DeviceAdded" then | ||
44 | print("DeviceAdded: " .. content["DeviceName"]) | ||
45 | end | ||
46 | end \ No newline at end of file | ||