LUA客户端用LUA 5.1编写的Nakama服务器。
Nakama是一款旨在为现代游戏和应用程序供电的开源服务器。功能包括用户帐户,聊天,社交,媒人,实时多人游戏等等。
该客户端在服务器上实现了完整的API和套接字选项。它用LUA 5.1编写,与基于LUA的游戏引擎兼容。
完整的文档可在此处提供。
在与客户端连接之前,您需要设置服务器和数据库。最简单的方法是使用Docker,但请查看其他选项的服务器文档。
安装并运行服务器。遵循以下说明。
将客户端添加到您的项目中。
game.project 。客户现在将出现在您项目的nakama文件夹中。将依赖项添加到您的项目中。在偏差的项目中,您需要将以下依赖项之一添加到game.project:
使用连接凭据来初始化Nakama客户端。
local defold = require " nakama.engine.defold "
local nakama = require " nakama.nakama "
local config = {
host = " 127.0.0.1 " ,
port = 7350 ,
use_ssl = false ,
username = " defaultkey " ,
password = " " ,
engine = defold ,
timeout = 10 , -- connection timeout in seconds
}
local client = nakama . create_client ( config )(可选)Nakama使用基本64解码用于会话令牌以及匹配数据的基本64编码和解码。默认的base64编码器和解码器写在LUA中。为了提高base64编码和解码步骤的性能,可以使用C中写入C的base64编码器。在Defold Projects中,您需要将以下依赖关系添加到Game.Project.Project:
客户端有许多方法可以在服务器中执行各种功能或与服务器打开实时套接字连接。
有多种使用服务器身份验证的方法。如果这些凭据不存在,则可以创建身份验证。通过Google Play游戏,Facebook,Game Center和Facebook,Facebook,Facebook,Facebook,Facebook,Facebook,Facebook,Facebook,Facebook,。
local client = nakama . create_client ( config )
local email = " [email protected] "
local password = " batsignal "
local session = client . authenticate_email ( email , password )
pprint ( session )注意:请参见下面的请求部分,以同步运行此片段(a)。
经过身份验证后,服务器响应验证令牌(JWT),可用于身份验证API请求。令牌包含有用的属性,并将其供应到session表中。
local client = nakama . create_client ( config )
local session = client . authenticate_email ( email , password )
print ( session . created )
print ( session . token ) -- raw JWT token
print ( session . expires )
print ( session . user_id )
print ( session . username )
print ( session . refresh_token ) -- raw JWT token for use when refreshing the session
print ( session . refresh_token_expires )
print ( session . refresh_token_user_id )
print ( session . refresh_token_username )
-- Use the token to authenticate future API requests
nakama . set_bearer_token ( client , session . token )
-- Use the refresh token to refresh the authentication token
nakama . session_refresh ( client , session . refresh_token )建议从会话中存储验证令牌,并在启动时检查是否过期。如果令牌已过期,则必须重新验证。如果令牌即将到期,则必须刷新。令牌的到期时间可以作为服务器中的设置更改。您可以使用session.store(session)存储会话,然后使用session.restore()将其恢复。
local nakama_session = require " nakama.session "
local client = nakama . create_client ( config )
-- restore a session
local session = nakama_session . restore ()
if session and nakama_session . is_token_expired_soon ( session ) and not nakama . is_refresh_token_expired ( session ) then
print ( " Session has expired or is about to expire. Refreshing. " )
session = nakama . session_refresh ( client , session . refresh_token )
nakama_session . store ( session )
elseif not session or nakama_session . is_refresh_token_expired ( session ) then
print ( " Session does not exist or it has expired. Must reauthenticate. " )
session = client . authenticate_email ( " [email protected] " , " foobar123 " , nil , true , " britzl " )
nakama_session . store ( session )
end
client . set_bearer_token ( session . token )客户端为游戏服务器的各种功能提供了许多内置API。可以使用使用回调函数返回结果(即异步)或产量的方法访问这些方法(即接收结果(即同步,必须在Lua coroutine中运行)。
local client = nakama . create_client ( config )
-- using a callback
client . get_account ( function ( account )
print ( account . user . id );
print ( account . user . username );
print ( account . wallet );
end )
-- if run from within a coroutine
local account = client . get_account ()
print ( account . user . id );
print ( account . user . username );
print ( account . wallet );Nakama客户端为创建和启动Coroutine提供了便利功能,以同步运行多个请求:
nakama . sync ( function ()
local account = client . get_account ()
local result = client . update_account ( request )
end )Nakama具有全局和每次重试配置,以控制如何重试API调用。
local retries = require " nakama.util.retries "
-- use a global retry policy with 5 attempts with 1 second intervals
local config = {
host = " 127.0.0.1 " ,
port = 7350 ,
username = " defaultkey " ,
password = " " ,
retry_policy = retries . fixed ( 5 , 1 ),
engine = defold ,
}
local client = nakama . create_client ( config )
-- use a retry policy specifically for this request
-- 5 retries at intervals increasing by 1 second between attempts (eg 1s, 2s, 3s, 4s, 5s)
nakama . list_friends ( client , 10 , 0 , " " , retries . incremental ( 5 , 1 ))创建一个取消令牌,并通过请求在请求完成之前取消该令牌。
-- use a global retry policy with 5 attempts with 1 second intervals
local config = {
host = " 127.0.0.1 " ,
port = 7350 ,
username = " defaultkey " ,
password = " " ,
retry_policy = retries . fixed ( 5 , 1 ),
engine = defold ,
}
local client = nakama . create_client ( config )
-- create a cancellation token
local token = nakama . cancellation_token ()
-- start a request and proivide the cancellation token
nakama . list_friends ( client , 10 , 0 , " " , nil , callback , token )
-- immediately cancel the request without waiting for the request callback to be invoked
nakama . cancel ( token )您可以通过实时Websocket连接连接到服务器,以发送和接收聊天消息,获取通知并将其匹配到多人游戏中。
您首先需要为服务器创建实时套接字:
local client = nakama . create_client ( config )
-- create socket
local socket = client . create_socket ()
nakama . sync ( function ()
-- connect
local ok , err = socket . connect ()
end )然后继续加入聊天频道并发送消息:
-- send channel join message
local channel_id = " pineapple-pizza-lovers-room "
local result = socket . channel_join ( socket , 1 , channel_id , false , false )
-- send channel messages
local result = socket . channel_message_send ( channel_id , " Pineapple doesn't belong on a pizza! " )客户端套接字具有事件侦听器,这些侦听器被调用从服务器接收到的各种事件。例子:
socket . on_disconnect ( function ( message )
print ( " Disconnected! " )
end )可用的听众:
on_disconnect处理何时与服务器断开客户端时的事件。on_channel_presence_eventon_match_presence_eventon_match_dataon_matchon_matchmaker_matchedon_notificationson_party_presence_eventon_partyon_party_dataon_party_join_requeston_status_presence_eventon_statuson_stream_dataon_erroron_channel_messageon_channel_messageNakama支持匹配消息的data属性中的任何二进制内容。无论您使用哪种数据类型,服务器都仅接受基本64编码的数据,因此请确保您不发布普通文本数据甚至JSON,否则Nakama Server会声称数据畸形并断开您的客户端(设置服务器登录以debug以检测这些事件)。
如果使用nakama.create_match_data_message()创建消息,则Nakama将自动base64编码匹配数据。 Nakama还将在调用on_matchdata侦听器之前自动解码任何接收到的匹配数据。
local json = require " nakama.util.json "
local match_id = " ... "
local op_code = 1
local data = json . encode ({
dest_x = 1.0 ,
dest_y = 0.1 ,
})
-- send a match data message. The data will be automatically base64 encoded.
socket . match_data ( match_id , op_code , data )在中继的多人游戏中,您将收到其他客户的消息。客户端已将消息数据发送给on_matchdata侦听器之前,客户端已将消息数据解码。如果数据是JSON编码的,如上所述,您需要自己解码:
socket . on_matchdata ( function ( message )
local match_data = message . match_data
local data = json . decode ( match_data . data )
pprint ( data ) -- gameplay coordinates from the example above
end )默认情况下,服务器在权威匹配中启动的消息将作为有效的JSON。
LUA客户lua 5.1撰写的Satori客户端。
Satori是一种用于为可行分析,A/B测试和远程配置提供动力的游戏的LiveOps服务器。使用Satori Defold客户端与Satori从Defold游戏中与Satori通信。
使用Satori仪表板上的API键创建Satori客户端。
local config = {
host = " myhost.com " ,
api_key = " my-api-key " ,
use_ssl = true ,
port = 443 ,
retry_policy = retries . incremental ( 5 , 1 ),
engine = defold ,
}
local client = satori . create_client ( config )然后进行身份验证以获取您的会话:
satori . sync ( function ()
local uuid = defold . uuid ()
local result = client . authenticate ( nil , nil , uuid )
if not result . token then
error ( " Unable to login " )
return
end
client . set_bearer_token ( result . token )
end )使用客户端您可以获得任何实验或功能标志,用户属于。
satori . sync ( function ()
local experiments = satori . get_experiments ( client )
pprint ( experiments )
local flags = satori . get_flags ( client )
pprint ( flags )
end )开发路线图作为GitHub问题进行管理,并欢迎拉动请求。如果您有兴趣增强代码,请打开一个问题,讨论更改或在社区论坛中进行讨论。
单位测试可以在tests文件夹中找到。使用望远镜(支持LUA 5.3+)运行它们:
./tsc -f test/test_nakama.lua test/test_satori.lua test/test_socket.lua test/test_session.lua
API文档由LDOC生成,并部署到GitHub页面。
更改API注释时,重新运行LDOC并提交docs/*中的更改。
注意: nakama/nakama.lua的评论必须在codegen/main.go中发表。
运行LDOC:
# in the project root, generate nakama.lua
# requires go and https://github.com/heroiclabs/nakama to be checked out
go run codegen/main.go -output nakama/nakama.lua ../nakama/apigrpc/apigrpc.swagger.json
# install ldoc (mac)
brew install luarocks
luarocks install ldoc
# run ldoc
doc . -d docs
请参阅CodeGen文件夹中的说明。
将Nakama和Satori调整为另一个基于LUA的引擎的客户端在配置Nakama客户端时提供另一个引擎模块应该很容易:
-- nakama
local myengine = require " nakama.engine.myengine "
local nakama = require " nakama.nakama "
local nakama_config = {
engine = myengine ,
}
local nakama_client = nakama . create_client ( nakama_config )
-- satori
local myengine = require " nakama.engine.myengine "
local satori = require " satori.satori "
local satori_config = {
engine = myengine ,
}
local satori_client = satori . create_client ( satori_config )发动机模块必须提供以下功能:
http(config, url_path, query_params, method, post_data, cancellation_token, callback) - 做http请求。
config配置表传递给nakama.create()或satori.create()url_path附加到基本URI的路径query_params用作URL查询参数的键值对method - “ get”,“ post”post_data要发布的数据cancellation_token检查cancellation_token.cancelled是正确的callback - 用结果调用函数(响应) socket_create(config, on_message) - 创建套接字。必须返回插座实例(具有引擎特定套接字状态的表)。
config配置表传递给nakama.create()或`satori.create()on_message从服务器发送消息时要调用的函数socket_connect(socket, callback) - 连接套接字。
socket - 插座实例从socket_create()callback - 用结果调用函数(好,err) socket_send(socket, message) - 在套接字上发送消息。
socket - 插座实例从socket_create()message - 发送消息uuid() - 创建一个UUID
该项目是根据Apache-2许可证获得许可的。