Dinhero21

Websockets test

Jun 14th, 2022 (edited)
1,266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.60 KB | None | 0 0
  1. shell.run('pastebin get 4nRg9CHU json')
  2. os.loadAPI('json')
  3.  
  4. io.write('Who are you? ')
  5.  
  6. local username = io.read()
  7.  
  8. local socket, e = http.websocket("wss://a295-189-54-244-176.sa.ngrok.io")
  9.  
  10. local function receive ()
  11.   local message = socket.receive()
  12.   local data = json.decode(message)
  13.  
  14.   print('<' .. data.author.username .. '>', data.content)
  15. end
  16.  
  17. local function send ()
  18.   term.clearLine()
  19.  
  20.   local content = io.read()
  21.  
  22.   socket.send(json.encode({ content = content, username = username }))
  23. end
  24.  
  25. if socket then
  26.   while true do
  27.     parallel.waitForAny(receive, send)
  28.   end
  29. end
  30.  
  31. error(e)
Advertisement
Add Comment
Please, Sign In to add comment