luarocks 学习笔记

记录使用luarocks cqueues时碰到的一些问题:

1. cqueues socket 与 redis 通信

 

function myfunc()
print("start")
local redis = require("cqueues.socket").connect("127.0.0.1", "6379")
redis:setmode("b", "nb")
redis:connect(5) -- timeout 5 seconds
local data = "*3 $3 set $5 mystr $3 333 " -- set mystr 333
--local data = "ping "
redis:write(data)
print(redis:read())
print("finish")
end

 

 

2. cqueues socket 实现 I/O多路复用

function myfunc()

local cqueues = require("cqueues").new()
local server = require("cqueues.socket").listen({
host = "0.0.0.0",
port = "3456",
reuseaddr = true,
reuseport = true
})

cqueues:wrap(function()
for cli in server:clients() do

print(cli:peername())

local cq = require("cqueues").new()
cqueues:wrap(function()
cq:loop()
end)
cq:wrap(function()
local buf=""
repeat
buf = cli:read()
local host, port = select(2, cli:peername())
print("recv from "..host..":"..port.." buf = ",buf)
until #buf == 0
end)

end
end)

assert(cqueues:loop())
end

3. classy 模块使用 与 lua使用table索引调用函数和普通调用的区别

_G.class = require("classy")
local mytestFile=class("mytestFile")

function mytestFile:__init()
self.aa = 99
end

function mytestFile.func1(self, ss)  -- 使用点的话需要显式传self
print(self.aa)
print("i am here ",ss)
end
-- 使用冒号定义函数时,实际上隐藏了一个形参的声明,这个形参会截获调用函数时的第一个实参并把它赋值给self
function mytestFile:func2(ss)      
print(self.aa)
print("i am func2 ",ss)
end

return mytestFile

但是在使用 kk["func2"]这种方式调用函数时,同样也需要显式传 自己 进去



 

原文地址:https://www.cnblogs.com/jiguang321/p/15021963.html