时间: 2020-09-03 00:08:26 人气: 2284 评论: 0
最近发布了一个跨平台的app开发框架Luakit(https://github.com/williamwen1986/Luakit) 。那怎么会想到做这样一个东西呢?这要先说一下我参与过的一些项目,和在项目中接触到的一些技术点和对项目开发体检了,因为Luakit是集合了几个重要技术才能做到用Lua脚本来实现跨平台app开发的。
我主要参与的项目是QQMail的IOS版。在2017年下半年,由于机缘巧合,我参与开发了企业微信的一个分支版本,appstore上叫政务微信。QQMail的历史比较悠久了,在QQMail项目里我们使用了两项技术是比较特殊的,其他项目团队接触得比较少,一个是Lua脚本化技术,一个是orm技术。而在政务微信开发过程中是企业微信团队的跨平台开发技术给我留下很深印象,下面我首先简单介绍这几项技术。
深入接触这几个框架后,我发现Lua跟chromium真是绝配,chromium提供跨平台的消息循环机制可以完美解决lua实现竞争式多线程的问题,在lua环境实现竞争式多线程(注意,不是单单线程安全)是使用lua开发的一个普遍性的难题,cocos2d-x的lua-binding也没解决这个问题,所以基于cocos2d-x lua版开发的游戏也很难做到全脚本化,因为Lua只能单线程。有了Luakit后,这类问题都有解决方案了。而lua的内存管理机制也可以很好的解决chromium用c++开发,内存管理和不适合函数式编程的最大的弊端,两者解合可以产生很好的效果。有了lua的多线程模型后,参考GYDataCenter的实现原理,我们可以实现一套lua版的orm框架,GYDataCenter只能在ios使用,现在lua版的orm框架可以具有跨平台的特性。
Luakit提供的很多强大的功能,这些功能都是可以跨平台运行的,Luakit主要包括以下功能接口
下面简单介绍多线程接口,orm接口,http请求,异步socket接口和全局通知接口。
多线程模型
如何在Lua实现竞争式多线程我会再发一篇文章专门讲讲,因为这个问题是Lua领域的普遍存在的问题,有一定的技术意义。这里我先简单带过一下实现思路,一个lua解析器本身是不具备多线程能力,甚至不是线程安全的,但是在服务器开发上已经有人尝试起多条线程然后给每条线程配置独立的Lua解析器,然后多条线程通过一定的数据通道传输数据,通过这样的方式实现真正的多线程,但是这个思路一直没有延伸到客户端开发,主要原因是因为客户端通常把真正的线程隐藏起来,无论IOS或者android,都不能轻易地接触真正的线程,但是由于chromium提供了开源的线程模型,通过修改chromium的底层源码,生成消息循环时的给每个消息循环配置独立的lua解析器,这样最大的问题就得到了解决,下面看一下Luakit 提供的多线程接口。
创建线程
-- 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")
demo code链接 https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/thread_test.lua
异步调用方法,类似IOS gcd中的 dispatch_async
-- 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)
demo code链接
https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/thread_test.lua
同步调用方法,类似IOS gcd中的 dispatch_sync
-- 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)
demo code链接 https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/thread_test.lua
orm接口
orm 模型的实现方法是参考IOS orm 开源库GYDataCenter的实现方法,GYDataCenter很依赖IOS gcd 的机制,Luakit中可以用新的lua多线程接口取代,可以做到同样的效果,下面罗列一下 orm demo code
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")
demo code链接 https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/db_test.lua
插入数据
local userTable = Table("user") local user = userTable({ username = "user1", password = "abc", time_create = os.time() }) user:save()
demo code链接 https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/db_test.lua
更新数据
local userTable = Table("user") local user = userTable.get:primaryKey({"user1"}):first() user.password = "efg"user.time_create = os.time() user:save()
select 数据
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请求接口,包括了请求队列调度控制
-- 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})
实现代码链接
https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/network/async_task_dispatcher.h
demo code链接
https://github.com/williamwen1986/Luakit/blob/master/LuaKitProject/src/Projects/LuaSrc/WeatherManager.lua
异步socket接口 Luakit 提供了非阻塞的socket调用接口
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 ( 技术沙龙 教程文章 热点综合