heroiclabs / nakama-unity

Unity client for Nakama server.
https://heroiclabs.com/docs/unity-client-guide
Other
419 stars 75 forks source link

Nakama Unity Storage Engine Not Work #53

Closed igavva closed 6 years ago

igavva commented 6 years ago

My script is nakama site examples:

using UnityEngine; using Nakama; using System;

public class ServerTest : MonoBehaviour { INClient client; INSession session;

void Start()
{
    client = new NClient.Builder("defaultkey")
    .Host("127.0.0.1")
    .Port(7350)
    .SSL(false)
    .Build();

    var deviceid = "998c7f46-d78a-11e7-90fd-37965f77ee64";
    var request = NAuthenticateMessage.Device(deviceid);
    client.Login(request, (INSession session) =>
    {
        Debug.Log("Player Logged in successfully");
        this.session = session;
        Storage();

    }, (INError error) =>
    {
        Debug.LogErrorFormat("ID login '{0}' failed: {1}", deviceid, error);

        if (error.Code == ErrorCode.UserNotFound)
        {
            client.Register(request, (INSession session) =>
            {
                Debug.Log("Player Register in successfully");
                this.session = session;
                Storage();

            }, (INError err) =>
            {
                Debug.LogErrorFormat("ID login '{0}' failed: {1}", deviceid, err);

            });
        }
    });

}

public void Storage()
{
    Debug.Log("Storage init");

    var json = "{\"coins\": 100, \"gems\": 10, \"artifacts\": 0}";

    var message = new NStorageUpdateMessage.Builder()
        .Update("myapp", "wallets", "wallet", new NStorageUpdateMessage.StorageUpdateBuilder()
            .Init("/foo", json)
            .Incr("/foo/coins", 10)
            .Incr("/foo/gems", 50)
            .Build())
        .Build();
    client.Send(message, (INResultSet<INStorageKey> list) =>
    {
        Debug.Log("Storage complite");
        foreach (var record in list.Results)
        {
            var version = record.Version;
            Debug.LogFormat("Stored record has version '{0}'", version);
        }
    }, (INError error) =>
    {
        Debug.LogErrorFormat("Error: code '{0}' with '{1}'.", error.Code, error.Message);
    });
}

}

Step1: Player Logged in successfully Step2: Storage init Step3: Nothing happens

Nakama v 1.3.0 http://c2n.me/3Q0i9px.png Nakama Unity Plugin v0.10.2 Cockroach 1.1.3 Unity 2017.2.0f3

zyro commented 6 years ago

@igavva You must connect the client after login/registration but before sending any messages. Call client.Connect(); before both of your Storage(); calls.

igavva commented 6 years ago

Thanks! It work)