Difference between revisions of "Receiving a rednet message through os.pullEvent()"
m (Use local variables instead of globals) |
|||
(One intermediate revision by one other user not shown) | |||
Line 6: | Line 6: | ||
for n,m in ipairs(rs.getSides()) do rednet.open(m) end -- Opens all rednet sides. | for n,m in ipairs(rs.getSides()) do rednet.open(m) end -- Opens all rednet sides. | ||
while true do | while true do | ||
− | event, id, text = os.pullEvent() | + | local event, id, text = os.pullEvent() |
if event == "rednet_message" then | if event == "rednet_message" then | ||
print(id .. "> " .. text) | print(id .. "> " .. text) | ||
Line 19: | Line 19: | ||
We start a loop because os.pullEvent() terminates itself after 10 seconds. | We start a loop because os.pullEvent() terminates itself after 10 seconds. | ||
− | event, id, text = os.pullEvent() | + | local event, id, text = os.pullEvent() |
We wait for an event to occur and when this happens, we store the event's info into 3 variables. | We wait for an event to occur and when this happens, we store the event's info into 3 variables. | ||
NOTE: You can give your variables whichever name you want. | NOTE: You can give your variables whichever name you want. | ||
Line 36: | Line 36: | ||
Beware that rednet is not secure on its own, with easily exploitable vulnerabilities that allow hackers to hack your networks into pieces--through attacks like wiretapping, spoofing, forgery, overloading, and many more. Be sure to add your own security when using rednet. | Beware that rednet is not secure on its own, with easily exploitable vulnerabilities that allow hackers to hack your networks into pieces--through attacks like wiretapping, spoofing, forgery, overloading, and many more. Be sure to add your own security when using rednet. | ||
− | + | [[Category:Tutorials]] |
Latest revision as of 17:56, 20 July 2017
The following method is a good method of receiving a rednet message without using rednet.receive(). It is usually used in IRC, Chat programs e.t.c. When a message arrives to a computer, a "rednet_message" event occurs.
The Code
This is a typical Listener which prints the content and the sender id of any incoming message.
for n,m in ipairs(rs.getSides()) do rednet.open(m) end -- Opens all rednet sides. while true do local event, id, text = os.pullEvent() if event == "rednet_message" then print(id .. "> " .. text) end end
Explanation
for n,m in ipairs(rs.getSides()) do rednet.open(m) end
Here we just open all sides for rednet processing.
while true do
We start a loop because os.pullEvent() terminates itself after 10 seconds.
local event, id, text = os.pullEvent()
We wait for an event to occur and when this happens, we store the event's info into 3 variables. NOTE: You can give your variables whichever name you want.
if event == "rednet_message" then
We check if the event was a rednet message.
print(id .. "> " .. text)
We print the message and the id of the sender.
end end
We close our previous statements.
Security
Beware that rednet is not secure on its own, with easily exploitable vulnerabilities that allow hackers to hack your networks into pieces--through attacks like wiretapping, spoofing, forgery, overloading, and many more. Be sure to add your own security when using rednet.