-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebSocket.lua
95 lines (76 loc) · 2.72 KB
/
WebSocket.lua
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
Instance.properties = properties({
{ name="Port", type="Int", value=38031, range = { min = 1, max = 65535 }, onUpdate = "onPortUpdate" },
{ name = "Status", type = "Text", value = "Disconnected", ui = { readonly = true } },
{ name = "TriggerAlert", type = "PropertyGroup", items = {
{ name = "AlertTitle", type = "Text" },
{ name = "AlertValue", type = "Text" },
{ name="Trigger", type="Action" },
} },
{ name = "Alerts", type = "ObjectSet", set_types = { type = "PolyPopObject", index = "WebSocket.WebSocketAlert" } }
})
function Instance:onInit()
self:connect()
end
function Instance:send(cmd, data)
if not self.webSocket or not self.webSocket:isConnected() then
return
end
if data then
return self.webSocket:send(cmd .. ":" .. tostring(self:getUniqueID()) .. ":" .. tostring(data))
end
return self.webSocket:send(cmd .. ":" .. tostring(self:getUniqueID()))
end
function Instance:connect()
getAnimator():createTimer(self, self.attemptConnection, seconds(5), true)
end
function Instance:attemptConnection()
self:disconnect()
local host = getNetwork():getHost("localhost")
self.webSocket = host:openWebSocket("ws://127.0.0.1:" .. tostring(self.properties.Port))
self.webSocket:setAutoReconnect(true)
-- WebSocket event listeners
self.webSocket:addEventListener("onMessage", self, self.onMessage)
self.webSocket:addEventListener("onConnected", self, self._onWsConnected)
self.webSocket:addEventListener("onDisconnected", self, self._onWsDisconnected)
end
function Instance:_onWsConnected()
getAnimator():stopTimer(self, self.attemptConnection)
self.properties.Status = "Connected"
end
function Instance:_onWsDisconnected()
self.properties.Status = "Disconnected"
self:connect()
end
function Instance:onMessage(msg)
local payload = json.decode(msg)
if payload.type == 'ALERT' then
-- local data = json.decode(obj.data)
-- Find the alert with the matching title
local alertCount = self.properties.Alerts:getKit():getObjectCount()
for i = 1, alertCount do
local Alert = self.properties.Alerts:getKit():getObjectByIndex(i)
if (Alert.properties.AlertName == payload.title) then
Alert:onMessage(payload.variables)
end
end
end
end
function Instance:onPortUpdate()
self:connect()
end
function Instance:Trigger()
local alertName = self.properties.TriggerAlert.AlertName.value
local alertText = self.properties.TriggerAlert.AlertText.value
if alertName == "" then
return print("Alert must have a name! Value can be blank.")
end
self:send(json.encode({type = "TRIGGER", name = alertName, data = alertText}))
end
function Instance:disconnect()
if (self.webSocket and self.webSocket:isConnected()) then
self:send("disconnect", "unload")
if (self.webSocket) then
self.webSocket:disconnect()
end
end
end