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許可證獲得許可的。