下载bson-lua到本地
参考:
sudo apt-get install lua
sudo apt-get install luarocks
sudo luarocks install lzmq
sudo luarocks install lua-llthreads2
1)实现rep_bson.lua
-
--require "zhelpers"
-
local zmq = require "lzmq" -- 获取zmq的安装包
-
local bson = require "bson" -- get bson package
-
local context = zmq.context() -- 创建一个ZMQ 上下文 ,
-
local publisher, err = context:socket{zmq.REP, bind = "tcp://*:5025"} -- 调用套接字 服务端将套接字绑定在端口5025
-
--zassert(publisher, err) -- 开始等待响应 发出应答 如此循环
-
-
-- 客户端是发送请求 并等待服务端的应答
-
epoch = os.time({year=1970, month=1, day=1, hour=0})
-
bsondoc = bson.encode{username="maroc",
-
info={first="todd",
-
last="coram",
-
age=46,
-
signature1=bson.binary("\000\001\002\003",bson.B_GENERIC),
-
signature2=bson.binary("\000\001\002\003"),
-
saved=true,
-
now=bson.utc_datetime(),
-
past=bson.utc_datetime(epoch * 1000),
-
colors={"Red","Green","Blue"}
-
}}
-
-
local y = 0
-
while y>=0 do
-
--local x = "This is a zmq test!"
-
local x = bsondoc
-
y = y + 1
-
--local ret = zassert(publisher:recv())
-
local ret = publisher:recv()
-
print(y.."rep recv"..ret)
-
-- sleep(1)
-
publisher:send(bsondoc)
-
--zassert(publisher:send(x))
-
-- print(y..":"..x)
-
end
2)实现req_bson.lua
-
--require "zhelpers"
-
local zmq = require "lzmq"
-
local bson = require "bson" -- get bson package
-
local context = zmq.context();
-
local requester,err = context:socket{zmq.REQ,connect = "tcp://localhost:5025"}
-
-
-
function print_table(t)
-
function printTableHelper(t, spacing)
-
for k,v in pairs(t) do
-
print(spacing..tostring(k), v)
-
if type(v) == "table" then
-
printTableHelper(v, spacing.."\t")
-
end
-
end
-
end
-
printTableHelper(t, "");
-
end
-
-
-
--zassert(requester,err)
-
for i = 1,10 do
-
-- print("hello world")
-
--zassert(requester:send("hello"));
-
requester:send("hello")
-
local ret = requester:recv()
-
luatab, doc = bson.decode(ret)
-
print("req recv==="..ret)
-
print(doc)
-
print_table(bson.decode(ret))
-
--print(luatab)
-
--print_table(luatab)
-
print()
-
end
3)运行:
#lua5.1 rep_bson.lua
#lua5.1 req_bson.lua
阅读(2342) | 评论(0) | 转发(0) |