Created
October 29, 2017 16:22
-
-
Save novabyte/197a72611286c975d0b48d7315081599 to your computer and use it in GitHub Desktop.
Push storage write changes as events to connected users with Nakama server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Copyright 2017 The Nakama Authors | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
]]-- | |
local nk = require("nakama") | |
local function push_score(context, payload) | |
local json = nk.json_decode(nk.base64_decode(payload.storageWrite.data[1].value)) | |
local user_ids = { context.UserId } -- use the storage payload loop over user_ids | |
local content = { | |
score = json.score | |
} | |
local notifications = {} | |
for _, user_id in ipairs(user_ids) | |
do | |
local notification = { | |
UserId = user_id, | |
SenderId = nil, | |
Subject = ("%q updated their score!"):format(context.UserHandle), | |
Content = content, | |
Code = 101, | |
ExpiresAt = 1000 * 60 * 60 * 24 * 7, -- ignored because message is non-persistent. | |
Persistent = false | |
} | |
table.insert(notifications, notification) | |
end | |
nk.notifications_send_id(notifications) | |
end | |
nk.register_after(push_score, "TStorageWrite") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2017 The Nakama Authors | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
using Nakama; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Text; | |
using UnityEngine; | |
public class StoragePusher : MonoBehaviour { | |
private INClient _client; | |
private INSession _session; | |
private static Action<INError> errorHandler = delegate(INError err) { | |
Debug.LogErrorFormat("Error: code '{0}' with '{1}'.", err.Code, err.Message); | |
}; | |
public StoragePusher() { | |
_client = NClient.Default("defaultkey"); | |
_client.OnNotification = (INNotification n) => { | |
Debug.LogFormat("Received code '{0}' and subject '{1}'.", n.Code, n.Subject); | |
var content = Encoding.UTF8.GetString(n.Content); | |
Debug.LogFormat("Received id '{0}' and content '{1}'.", n.Id, content); | |
}; | |
} | |
private void Awake() { | |
var id = SystemInfo.deviceUniqueIdentifier; | |
var request = NAuthenticateMessage.Device(id); | |
Action<INSession> sessionHandler = delegate(INSession session) { | |
Debug.LogFormat("Session: '{0}'.", session.Token); | |
_client.Connect(session); | |
}; | |
_client.Login(request, sessionHandler, (INError err) => { | |
if (err.Code == ErrorCode.UserNotFound) { | |
_client.Register(request, sessionHandler, errorHandler); | |
} else { | |
errorHandler(err); | |
} | |
}); | |
} | |
private void Update() { | |
if (Input.GetKeyDown(KeyCode.Space)) { | |
PushStorageWrite(); | |
} | |
} | |
private void OnDestroy() { | |
_client.Disconnect(); | |
} | |
private void PushStorageWrite() { | |
byte[] score = Encoding.UTF8.GetBytes("{\"score\": 50}"); | |
var message = new NStorageWriteMessage.Builder() | |
.Write("mygame", "scores", "score", score) | |
.Build(); | |
_client.Send(message, (INResultSet<INStorageKey> list) => { | |
foreach (var record in list.Results) { | |
var version = Encoding.UTF8.GetString(record.Version); | |
Debug.LogFormat("Stored record has version '{0}'", version); | |
} | |
}, errorHandler); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment