Search Unity

Connect Async with Mongodb

Discussion in 'Multiplayer' started by felipeszxbox, Jul 16, 2019.

  1. felipeszxbox

    felipeszxbox

    Joined:
    Jul 15, 2019
    Posts:
    3
    Hello guys im New in Unity. Im trying to do a Highscore acene, getting datas from a Database. Right now im testing with localhost with a simples datas:

    Collection User: {
    "Name": "Blá blá",
    "Age": 30,
    "score": 3000;
    }....

    when I play in the game, it takes a long time to start the scene, due to the process of connecting to the pack and making the query. I would like to know if there is any way to create asynchronous actions, I come from Nodejs and there we are accustomed to this: D

    Thanks​
     
  2. felipeszxbox

    felipeszxbox

    Joined:
    Jul 15, 2019
    Posts:
    3
    Thats my code

    void Start()
    {
    var client = new MongoClient(connectionString);
    var server = client.GetServer();
    var database = server.GetDatabase("Discord");
    var playercollection = database.GetCollection<BsonDocument>("users");
    Debug.Log("Conexao Realizada com Sucesso");

    foreach (var document in playercollection.FindAll())
    {
    Debug.Log("4. SELECT ALL DOCS: \n" + document);
    }
    }`
     
  3. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    154
    Maybe this is not the answer you were looking for but, making direct connections with a database from a game client is asking for trouble. Anyone with a basic understanding of C# decompilation would be able get the connection coordinates to your database and mess with it to their heart's content. (such as spamming it with bogus scores and such)

    If you want to post high scores to a database you need a bit more security than that. At least you need a bit of server side logic that allows you to recognize an actually logged-in user from anyone else. The same logic then would be responsible to talk to the database. This way the client doesn't contain any critical information on how to connect to the DB. Instead you would just POST the data to a web server, which would run such logic.

    The best approach would be to run a multiplayer authoritative server, so that it can validate high scores too, not just users.