새로운 사용자에 대해서 레코드를 작성하는 다른 방법은 등록이 완료된 후에 서버 측 코드를 실행하는 것입니다. 등록 후크를 통해서 완료할 수 있습니다.
“register_after” 후크는 "authenticaterequest_*" 메시지 유형과 함께 사용하여 메시지가 처리된 후에 서버가 기능을 실행하도록 전달합니다. 서버에서는 등록과 로그인 메시지가 구분되지 않기 때문에 조건부 작성을 사용하여 레코드를 저장합니다.
Server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
localfunctioninitialize_user(context,payload)ifpayload.createdthen-- Only run this logic if the account that has authenticated is new.localchangeset={coins=10,-- Add 10 coins to the user's wallet.gems=5-- Add 5 gems from the user's wallet.artifacts=0-- No artifacts to start with.}localmetadata={}nk.wallet_update(context.user_id,changeset,metadata,true)endend-- change to whatever message name matches your authentication type.nk.register_req_after(initialize_user,"AuthenticateDevice")
funcInitializeUser(ctxcontext.Context,loggerLogger,db*sql.DB,nkNakamaModule,out*api.Session,in*api.AuthenticateDeviceRequest)error{ifout.Created{// Only run this logic if the account that has authenticated is new.
userID,ok:=ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)if!ok{return"",errors.New("Invalid context")}changeset:=map[string]interface{}{"coins":10,// Add 10 coins to the user's wallet.
"gems":5,// Add 5 gems from the user's wallet.
"artifacts":0,// No artifacts to start with.
}varmetadatamap[string]interface{}iferr:=nk.WalletUpdate(ctx,userID,changeset,metadata,true);err!=nil{// Handle error.
}}}// Register as after hook, this call should be in InitModule.
iferr:=initializer.RegisterAfterAuthenticateDevice(InitializeUser);err!=nil{logger.Error("Unable to register: %v",err)returnerr}
Code snippet for this language TypeScript has not been found. Please choose another language to show equivalent examples.
이 방법을 사용하면 클라이언트 연결 해제를 방지할 수 있지만 로그인 또는 등록 메시지 이후에 데이터베이스를 작성해야 합니다. 저장소 엔진에 데이터를 얼마나 자주 작성하는지에 따라서 허용될 수도 있으며, 빠른 재연결을 위해서 사용자 세션에 캐시를 적용하면 연결 해제를 최소화할 수 있습니다.