As soon as you run this program, a new shell will be started. This shell can be used normally. Now you can add daemons to the background.
daemon.loadDaemon("My Daemon", function()
-- Code to do stuff.
end)
daemon.loadDaemon(stringName, func) is how you load a daemon. You create a function that you want to run in the background, and load it. In the example above, I declared the function within the loadDaemon call, but you can create the function before hand also.
This is particularly useful for APIs that need tasks to keep running.
--MyApiFile
if daemon then
local lastMessage
function getLastMessage()
return lastMessage
end
function myDaemon()
while true do
local id, msg = rednet.receive()
lastMessage = msg
end
end
daemon.loadDaemon("My Daemon", myDaemon)
else
print("This API requires the Daemon Server by ElvishJerricco")
end
Now you can call MyApiFile.getLastMessage() to retrieve the most recent rednet message. This is obviously a very basic example but it works and it demonstrates just what the daemon server can do.
To stop the daemon server, run the program again with the argument -stop or call daemon.stopServer(). Also, running <filename> -list will list all running daemons by their IDs and names. Using <filename> -kill <ID> will kill the daemon with the specified ID. Using <filename> -s will launch the daemon server and run startup when the new shell starts.
Note: no matter what you name this file, the API for loadDaemon() is ALWAYS called daemon. So no matter what, the code is daemon.loadDaemon(stringName, func).
2nd Note: I strongly urge you to use os.pullEventRaw instead of os.pullEvent from within your daemons. Because of the way termination works, it's very tricky to predict how a daemon will react to this, if at all.
pastebin get MtkSVDWH startd
Changelog
Spoiler












