How to keep the opencomputer connexion alive after a server reboot
Hi,
Frist of, your lib is amazing, I have build a discord bot using it, with two way communication, and I'm building a full base monitoring / managing with grafana and a node server. I have also made a Nodejs Typescript client library.
I'm am wordering, how do you keep the connexion alive on the opencomputer ? The ping seams buggy since it fails every time, even tho the server responded to the ping. The only hacky way is to send a message every 10 secondes to a "pings" chanels, but I don't know how to restart the connexion on disconection.
Do you have a use case ?
My code
local component = require("component")
local event = require("event")
local stem = require('./stem')
local server = stem.connect('some.stem.server')
server:subscribe('mc-message-in')
local cb = component.getPrimary("command_block")
local char_space = string.byte(" ")
local running = true
function unknownEvent(a, b, c, d)
-- do nothing if the event wasn't relevant
end
local myEventHandlers = setmetatable({}, { __index = function() return unknownEvent end })
function myEventHandlers.key_up(adress, char, code, playerName)
if (char == char_space) then
running = false
end
end
function encodeMessageIntoStr (user,message)
return '' .. user .. ':' .. message
end
function myEventHandlers.chat_message(adress, user, message)
server:send('mc-message-out', encodeMessageIntoStr(user, message))
end
function myEventHandlers.stem_message(channel_id, message)
print('Stem', channel_id, message)
if (channel_id == 'mc-message-in') then
print('Sending message')
cb.setCommand('/say ' .. message)
cb.executeCommand()
end
end
-- The main event handler as function to separate eventID from the remaining arguments
function handleEvent(eventID, ...)
print('New event : ' .. eventID)
if (eventID) then -- can be nil if no event was pulled for some time
myEventHandlers[eventID](...) -- call the appropriate event handler with all remaining arguments
end
end
function keepAlive()
server:send('pings', 'listener')
end
local timer = event.timer(10, keepAlive, math.huge)
-- main event loop which processes all events, or sleeps if there is nothing to do
while running do
handleEvent(event.pull()) -- sleeps until an event is available, then process it
end
event.cancel(timer)
server:disconnect()
Edited by Nicolas Beaussart