GitHub Documentation Patreon donate button

Multiple Clients in the same Cocos application



  • Hello there! :)

    I started using Colyseus a few weeks ago and so far everything is running smoothly. I am writing a game with a local and an online multiplayer. For local multiplayer I wanted to start a local Colyseus server and connect several clients to it. I wrote a test in javascript and everything worked fine, but I wrote the game in Cocos2D and I am stuck with the client implementation. As long as I am only connecting one client with one room, it works. But as soon as I want to open multiple rooms with the same client, only one room connects and all the others fail to connect. I've also written a test with multiple clients, each with one room. That seems to work, but my understanding of Colyseus is, that I just need to create one client. Is this wrong and I actually need to create multiple clients, one for every room, or is my implementation wrong?

    My implementation is as follows:

    GameScene.cpp:

    Client* client = new Client("ws://localhost:2567");
    ... //game code

    initPlayers(){
    auto player1 = Player(client, position, points, whatever);
    }

    Player.cpp:

    Client* gameClient;
    Room<State>* room;

    Player(Client* client, Vec2 position, int points, whatever){
    this->gameClient = client;
    ...
    this->onConnectToServer();
    }

    onConnectToServer(){
    ...//defining options
    client->joinOrCreate("lobby", options, [=](std::string err, Room<State>* _room){
    ...//basically the code from the example project
    }
    }

    This does NOT work with more than one players. The first player connects, all the others don't. I just get "Error -1 in request" for every connection other than the first one. Now, if I just create a new Client in every Player object it works fine. Is this the way to do it, or is that a bad hacky way that just works? :D

    Thanks for every help in advance!


 

GitHub Documentation Patreon donate button

© 2020 Endel Dreyer