Advertisement
osmarks

ccwsterm

Apr 1st, 2018
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. local default = "wss://osmarks.ml/ws/"
  2.  
  3. local ws, error, url
  4.  
  5. function connect()
  6. ws, error = http.websocket(url)
  7. if error then
  8. print(error)
  9. else
  10. print "Connected"
  11. end
  12. end
  13.  
  14. repeat
  15. write "URL> "
  16. url = read()
  17.  
  18. if url == "" then
  19. print("Defaulted to", default)
  20. url = default
  21. end
  22.  
  23. connect()
  24. until ws
  25.  
  26. function readWS()
  27. while true do
  28. print(ws.receive())
  29. end
  30. end
  31.  
  32. function send(msg)
  33. local ok = pcall(ws.send, msg)
  34.  
  35. if not ok then
  36. print "Disconnected"
  37. connect()
  38. send(msg)
  39. end
  40. end
  41.  
  42. function writeWS()
  43. while true do
  44. sleep(0.2) -- so response can arrive
  45. write "|> "
  46. local input = read()
  47. send(input)
  48. end
  49. end
  50.  
  51. parallel.waitForAll(writeWS, readWS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement