-
Notifications
You must be signed in to change notification settings - Fork 7
/
cohttp.lua
34 lines (28 loc) · 856 Bytes
/
cohttp.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
if _VERSION == "Lua 5.1" then
-- Lua 5.1 does not support yield accross
-- C function so we use `try.co` module to
-- replace default implementation of LuaSocket
-- protect functionality.
local socket = require "socket"
local try = require "try.co"
socket.newtry = try.new
socket.protect = try.protect
socket.try = try.new()
end
local uv = require "lluv"
local ut = require "lluv.utils"
local socket = require "lluv.luasocket"
local http = require "socket.http"
local function http_request(url)
local co = coroutine.running()
print("HTTP Request: ", http.request{
url = url;
create = function()
-- for Lua >= 5.2 you can just use `socket.tcp`
return socket.tcp():attach(co)
end;
})
end
ut.corun(http_request, "http://google.ru")
ut.corun(http_request, "http://google.ru")
uv.run()