rednet.receive

Waits for a Rednet message to be received by an opened modem, or until timeout seconds elapse. Takes another optional parameter, which limits messages able to be received to those broadcasted or sent with a matching protocol.

rednet.receive
Function
Syntax
rednet.receive(
  • protocol? : string
  • timeout? : number
)

Returns number senderID, any message, string protocol
API rednet
Source Rednet API (source)

 Note: This function call requires a Modem peripheral to be attached to the computer. See Networking for more information.

 Note: Rednet is not secure against interception or spoofing. See Network security#Rednet for more information.

ExampleReceive any message with no timeout
Since we have no arguments, rednet.receive will wait for the next message from any protocol. If a rednet message was received with the text "Hello world!", the the output would be as follows:
Code
<nowiki>
local id, message, protocol = rednet.receive()
print(message)
    </nowiki>
Output
"Hello world!"
ExampleReceive any message with a timeout
Since we have our timeout specified, rednet.receive will wait for the next message from any protocol, or until the timeout is up. If a rednet message was received with the text "Now there's a timeout!" before the timeout is up, the the output would be as follows:
Code
<nowiki>
local id, message, protocol = rednet.receive(30)
print(message)
    </nowiki>
Output
"Now there's a timeout!"
ExampleReceive messages of a certain protocol with a timeout
Since we have our timeout and protocol specified, rednet.receive will wait for the next message from a specific protocol, or until the timeout is up. If a rednet message was received with the text "Now there's a protocol!" and protocol "ComputerCraft" before the timeout is up, the the output would be as follows:
Code
<nowiki>
local id, message, protocol = rednet.receive("ComputerCraft", 30)
print(message)
    </nowiki>
Output
"Now there's a protocol!"
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.