SHARE
TWEET

stem-test

a guest Aug 25th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local event = require('event')
  2. local stem = require('stem')
  3. local side = require('sides')
  4. local com = require('component')
  5. local redstone = com.isAvailable('redstone') and com.redstone or nil
  6.  
  7. local channel = "test"
  8. local layout = [===[
  9.   document.body.innerHTML = "\
  10.    <style>button:active { background: #fac700; }</style>\
  11.    <button onclick=\"sendMessage(\'toggle\')\">Toggle Light</button>\
  12.  ";
  13.   document.body.style.alignItems = "center";
  14. ]===]
  15. local lampSide = side.top
  16. local lampTurnedOn = false
  17.  
  18. print("Connecting to the #" .. channel .. " STEM channel...")
  19. local server = stem.connect('stem.fomalhaut.me')
  20. server:subscribe(channel)
  21. print("Done.")
  22. print("Press [q] to quit.")
  23.  
  24. while true do
  25.   local name, a, b = event.pull()
  26.   if name == "stem_message" then
  27.     local message = b
  28.     if message == "connect" then
  29.       print("Someone wants to connect - sending the layout...")
  30.       server:send(channel, layout)
  31.     elseif message == "toggle" then
  32.       if redstone ~= nil then
  33.         if not lampTurnedOn then
  34.           redstone.setOutput(lampSide, 16)
  35.           lampTurnedOn = true
  36.         else
  37.           redstone.setOutput(lampSide, 0)
  38.           lampTurnedOn = false
  39.         end
  40.       end
  41.     end
  42.   elseif name == "key_down" then
  43.     local char = b
  44.     if char == 113 then break end
  45.   end
  46. end
  47.  
  48. server:disconnect()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top