Advertisement
_dinsdale

Untitled

Sep 26th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. --
  2. -- Created by IntelliJ IDEA.
  3. -- User: russellh
  4. -- Date: 9/19/16
  5. -- Time: 10:07 PM
  6. -- To change this template use File | Settings | File Templates.
  7. --
  8. --local http_request = require "http.request"
  9. --local headers, stream = assert(http_request.new_from_uri("http://example.com"):go())
  10. --local body = assert(stream:get_body_as_string())
  11. --if headers:get ":status" ~= "200" then
  12. -- error(body)
  13. --end
  14. --print(body)
  15.  
  16. local cqueues = require "cqueues"
  17. local websocket = require "http.websocket"
  18.  
  19. cq = cqueues.new()
  20.  
  21. local ws = websocket.new_from_uri("ws://localhost:2012")
  22. assert(ws:connect())
  23.  
  24. cq:wrap(function()
  25. repeat
  26. local response = ws:receive()
  27. print(response)
  28. until not response or response:upper() == "QUIT"
  29. end)
  30.  
  31.  
  32. cq:wrap(function()
  33. while true do
  34. io.stdout:write("Input> ")
  35. cqueues.poll({pollfd=0; events="r"}) -- wait until data ready to read on stdin
  36. local data = io.stdin:read"*l" -- blockingly read a line. shouldn't block if tty is in line buffered mode.
  37. if data == nil or data:upper() == "STOP" then
  38. ws:close()
  39. break
  40. end
  41. assert(ws:send(data)) -- echo it back?
  42. end
  43. end)
  44.  
  45. cq:wrap(function()
  46. while true do
  47. local msg = [[authCommand {"Username":"Russell", "Password":"testing"}]]
  48. assert(ws:send(msg))
  49. print(msg)
  50. cqueues.sleep(3)
  51. end
  52. end)
  53.  
  54. assert(cq:loop())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement