Luakit 1.0.3

Luakit 1.0.3

williamwen1986 维护。



Luakit 1.0.3

  • 作者
  • williamwen1986

Luakit

这是一个多平台解决方案,用 Lua 编写,您可以使用这个工具开发 iOS 或 Android 应用,真正实现一处编写,到处使用。请注意,您只能使用 Luakit 来编写业务逻辑代码,包括网络、数据库 ORM、多线程模型,但不能编写 UI 代码,Luakit 不提供 UI 开发的 API。

为什么使用 Luakit 开发应用程序?

我会说 Luakit 是开发多平台应用程序最有效的工具。以下是一些原因:

  • 跨平台,真正实现一处编写,到处使用,这个功能非常吸引人,如果您有过多平台开发的经验,跨平台架构会给人留下深刻印象。它可以大幅度提高效率。

  • 动态灵活,用 Lua 编写业务逻辑代码,您可以随时发布您的代码,真正实现敏捷开发。

  • 忘记令人烦恼的内存管理,Lua 支持自动垃圾回收。Lua 有闭包,也称为代码块。这些功能使得 Luakit 比其他 C++ 跨平台解决方案更加高效。

示例

对于一些简单的 Luakit 应用程序,请查看 源文件夹

多线程

我们提供了一些强大的 Lua 多线程 API,您可以在 Android 示例文件夹和 iOS 示例文件夹中查看 ThreadTest。请注意,它不仅提供了线程安全的多线程模型,还提供了真正的竞争型多线程模型。这是在 Lua 或 JS 中执行竞争型多线程的独特解决方案。

创建线程,示例代码

-- Parma1 is the thread type ,there are five types of thread you can create.
-- BusinessThreadUI
-- BusinessThreadDB
-- BusinessThreadLOGIC
-- BusinessThreadFILE
-- BusinessThreadIO
-- Param2 is the thread name
-- Result is new threadId which is the token you should hold to do further action
local newThreadId = lua.thread.createThread(BusinessThreadLOGIC,"newThread")

在指定线程上异步执行方法,示例代码

-- Parma1 is the threadId for which you want to perform method
-- Parma2 is the modelName
-- Parma3 is the methodName
-- The result is just like you run the below code on a specified thread async
-- require(modelName).methodName("params", 1.1, {1,2,3}, function (p)
-- end)
lua.thread.postToThread(threadId,modelName,methodName,"params", 1.1, {1,2,3}, function (p)
	-- do something here
end)

在指定线程上同步执行方法,示例代码

-- Parma1 is the threadId for which you want to perform method
-- Parma2 is the modelName
-- Parma3 is the methodName
-- The result is just like you run the below code on a specified thread sync
-- local result = require(modelName).methodName("params", 1.1, {1,2,3}, function (p)
-- end)
local result = lua.thread.postToThreadSync(threadId,modelName,methodName,"params", 1.1, {1,2,3}, function (p)
	-- do something here
end)

ORM

Luakit提供了一个具有以下特性的ORM解决方案

  • 面向对象的接口
  • 自动创建和更新表模式。
  • 具有内部缓存
  • 定时自动事务
  • 线程安全

定义您的模型,示例代码

-- Add the define table to dbData.lua
-- Luakit provide 7 colum types
-- IntegerField to sqlite integer 
-- RealField to sqlite real 
-- BlobField to sqlite blob 
-- CharField to sqlite varchar 
-- TextField to sqlite text 
-- BooleandField to sqlite bool
-- DateTimeField to sqlite integer
user = {
	__dbname__ = "test.db",
	__tablename__ = "user",
	username = {"CharField",{max_length = 100, unique = true, primary_key = true}},
	password = {"CharField",{max_length = 50, unique = true}},
	age = {"IntegerField",{null = true}},
	job = {"CharField",{max_length = 50, null = true}},
	des = {"TextField",{null = true}},
	time_create = {"DateTimeField",{null = true}}
	},
-- when you use, you can do just like below
local Table = require('orm.class.table')
local userTable = Table("user")

插入数据,示例代码

local userTable = Table("user")
local user = userTable({
	username = "user1",
	password = "abc",
	time_create = os.time()
})
user:save()

更新数据,示例代码

local userTable = Table("user")
local user = userTable.get:primaryKey({"user1"}):first()
user.password = "efg"
user.time_create = os.time()
user:save()

删除数据,示例代码

local userTable = Table("user")
local user = userTable.get:primaryKey({"user1"}):first()
user:delete()

批量更新,示例代码

local userTable = Table("user")
userTable.get:where({age__gt = 40}):update({age = 45})

批量删除,示例代码

local userTable = Table("user")
userTable.get:where({age__gt = 40}):delete()

选择,示例代码

local userTable = Table("user")
local users = userTable.get:all()
print("select all -----------")
local user = userTable.get:first()
print("select first -----------")
users = userTable.get:limit(3):offset(2):all()
print("select limit offset -----------")
users = userTable.get:order_by({desc('age'), asc('username')}):all()
print("select order_by -----------")
users = userTable.get:where({ age__lt = 30,
	age__lte = 30,
	age__gt = 10,
	age__gte = 10,
	username__in = {"first", "second", "creator"},
	password__notin = {"testpasswd", "new", "hello"},
	username__null = false
	}):all()
print("select where -----------")
users = userTable.get:where({"scrt_tw",30},"password = ? AND age < ?"):all()
print("select where customs -----------")
users = userTable.get:primaryKey({"first","randomusername"}):all()
print("select primaryKey -----------")

连接,示例代码

local userTable = Table("user")
local newsTable = Table("news")
local user_group = newsTable.get:join(userTable):all()
print("join foreign_key")
user_group = newsTable.get:join(userTable,"news.create_user_id = user.username AND user.age < ?", {20}):all()
print("join where ")
user_group = newsTable.get:join(userTable,nil,nil,nil,{create_user_id = "username", title = "username"}):all()
print("join matchColumns ")

HTTP请求

Luakit提供了一个HTTP请求接口,它包含一个内部分配器,包含一个FIFO队列,这里是源代码示例代码

-- url , the request url
-- isPost, boolean value represent post or get
-- uploadContent, string value represent the post data
-- uploadPath,  string value represent the file path to post
-- downloadPath, string value to tell where to save the response
-- headers, tables to tell the http header
-- socketWatcherTimeout, int value represent the socketTimeout
-- onResponse, function value represent the response callback
-- onProgress, function value represent the onProgress callback
lua.http.request({ url  = "http://tj.nineton.cn/Heart/index/all?city=CHSH000000",
	onResponse = function (response)
	end})

异步套接字

Luakit提供了一个非阻塞的套接字连接接口,示例代码

local socket = lua.asyncSocket.create("127.0.0.1",4001)

socket.connectCallback = function (rv)
    if rv >= 0 then
        print("Connected")
        socket:read()
    end
end
    
socket.readCallback = function (str)
    print(str)
    timer = lua.timer.createTimer(0)
    timer:start(2000,function ()
        socket:write(str)
    end)
    socket:read()
end

socket.writeCallback = function (rv)
    print("write" .. rv)
end

socket:connect()

通知

Luakit提供了一个通知系统,通过该系统,通知可以通过Lua环境和本地环境进行传输

注册并发布Lua通知,示例代码

lua.notification.createListener(function (l)
	local listener = l
	listener:AddObserver(3,
	    function (data)
	        print("lua Observer")
	        if data then
	            for k,v in pairs(data) do
	                print("lua Observer"..k..v)
	            end
	        end
	    end
	)
end);

lua.notification.postNotification(3,
{
    lua1 = "lua123",
    lua2 = "lua234"
})

在Android中注册并发布通知,示例代码

LuaNotificationListener  listener = new LuaNotificationListener();
INotificationObserver  observer = new INotificationObserver() {
    @Override
    public void onObserve(int type, Object info) {
        HashMap<String, Integer> map = (HashMap<String, Integer>)info;
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            Log.i("business", "android onObserve");
            Log.i("business", entry.getKey());
            Log.i("business",""+entry.getValue());
        }
    }
};
listener.addObserver(3, observer);

HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("row", new Integer(2));
NotificationHelper.postNotification(3, map);

在iOS中注册并发布通知,示例代码

_notification_observer.reset(new NotificationProxyObserver(self));
_notification_observer->AddObserver(3);
- (void)onNotification:(int)type data:(id)data
{
    NSLog(@"object-c onNotification type = %d data = %@", type , data);
}

post_notification(3, @{@"row":@(2)});

设置

在Android中设置Luakit

在iOS中设置Luakit

更多

有任何问题?请发送电子邮件至[email protected]