Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- Created by IntelliJ IDEA.
- -- User: russellh
- -- Date: 9/19/16
- -- Time: 10:07 PM
- -- To change this template use File | Settings | File Templates.
- --
- --local http_request = require "http.request"
- --local headers, stream = assert(http_request.new_from_uri("http://example.com"):go())
- --local body = assert(stream:get_body_as_string())
- --if headers:get ":status" ~= "200" then
- -- error(body)
- --end
- --print(body)
- local cqueues = require "cqueues"
- local websocket = require "http.websocket"
- cq = cqueues.new()
- local ws = websocket.new_from_uri("ws://localhost:2012")
- assert(ws:connect())
- cq:wrap(function()
- repeat
- local response = ws:receive()
- print(response)
- until not response or response:upper() == "QUIT"
- end)
- cq:wrap(function()
- while true do
- io.stdout:write("Input> ")
- cqueues.poll({pollfd=0; events="r"}) -- wait until data ready to read on stdin
- local data = io.stdin:read"*l" -- blockingly read a line. shouldn't block if tty is in line buffered mode.
- if data == nil or data:upper() == "STOP" then
- ws:close()
- break
- end
- assert(ws:send(data)) -- echo it back?
- end
- end)
- cq:wrap(function()
- while true do
- local msg = [[authCommand {"Username":"Russell", "Password":"testing"}]]
- assert(ws:send(msg))
- print(msg)
- cqueues.sleep(3)
- end
- end)
- assert(cq:loop())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement