|
@@ -35,10 +35,25 @@ function WebSocketClient:disconnect()
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
-function WebSocketClient:send(command, data)
|
|
|
|
|
|
+function WebSocketClient:send(data)
|
|
|
|
+ data = data or {}
|
|
|
|
+
|
|
|
|
+ local commandAPI = data.command or 'error'
|
|
|
|
+ local dataAPI = data.body or nil
|
|
|
|
+ local finalData = nil
|
|
|
|
+
|
|
|
|
+ if type(dataAPI) == 'table' then
|
|
|
|
+ finalData = cjson.encode(dataAPI)
|
|
|
|
+ else
|
|
|
|
+ finalData = dataAPI or 'Empty'
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+ -- print(commandAPI)
|
|
|
|
+ -- print(dataAPI)
|
|
|
|
+
|
|
local message = {
|
|
local message = {
|
|
- event = command,
|
|
|
|
- data = data
|
|
|
|
|
|
+ event = commandAPI,
|
|
|
|
+ data = finalData
|
|
}
|
|
}
|
|
|
|
|
|
local message_json = cjson.encode(message)
|
|
local message_json = cjson.encode(message)
|
|
@@ -71,6 +86,19 @@ end
|
|
|
|
|
|
function WebSocketClient:on_disconnected()
|
|
function WebSocketClient:on_disconnected()
|
|
print("Disconnected from the server.")
|
|
print("Disconnected from the server.")
|
|
|
|
+ while true do
|
|
|
|
+ print("Attempting to reconnect...")
|
|
|
|
+ local success, err = pcall(self.connect, self)
|
|
|
|
+ if success then
|
|
|
|
+ print("Reconnected to the server.")
|
|
|
|
+ copas.addthread(function() self:receive() end)
|
|
|
|
+ break
|
|
|
|
+ else
|
|
|
|
+ print("Failed to reconnect, error: " .. tostring(err))
|
|
|
|
+ print("Retrying in 5 seconds...")
|
|
|
|
+ copas.sleep(5) -- Wait for 5 seconds before attempting to reconnect
|
|
|
|
+ end
|
|
|
|
+ end
|
|
end
|
|
end
|
|
|
|
|
|
function WebSocketClient:on_received(data)
|
|
function WebSocketClient:on_received(data)
|