TwitchBlade

client example

Apr 16th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component     = require("component")
  2. local event         = require("event")
  3. local serialization = require("serialization")
  4. local term          = require("term")
  5. local text          = require("text")
  6. local thread        = require("thread")
  7.  
  8. local listenPort = 55
  9.  
  10. function messageReceived(event, localAddress, removeAddress, port, distance, message)
  11.   if distance > 0 then
  12.     print("Message received: " .. message)
  13.     local t = serialization.unserialize(message)
  14.     for k in pairs(t) do
  15.       print(k .. ": " .. tostring(t[k] or ""))
  16.     end
  17.   end
  18. end
  19.  
  20. function main()
  21.   -- Attach event listener to modem
  22.   component.modem.open(listenPort)
  23.   event.listen("modem_message", messageReceived)
  24.  
  25.   -- Read user input
  26.   while true do
  27.     userInput = text.trim(term.read())
  28.     if userInput == "exit" or userInput == "quit" then
  29.       break
  30.     end
  31.   end
  32. end
  33.  
  34. -- Wait for main thread to exit, or interrupt signal (^C)
  35. thread.waitForAny({
  36.   thread.create(main),
  37.   thread.create(function() event.pull("interrupted") end)
  38. })
  39.  
  40. term.clear()
  41. os.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment