GitHub | Documentation | Become a Patron

Help ! Creating Card Game , Create new (table) room in Unity



  • i'm creating multiplayer card game. i want make a private table (room) , but when i'm connecting to server it all connect to same room.

    my code:

    public string serverName = "localhost";
    public string port = "3553";
    public string roomName = "chat";
    

    public IEnumerator Start()
    {
    String uri = "ws://" + serverName + ":" + port;
    Debug.Log(uri);
    client = new Client(uri);

        client.OnOpen += OnOpenHandler;
        client.OnClose += (object sender, EventArgs e) => Debug.Log("CONNECTION CLOSED");
    
        Debug.Log("Let's connect the client!");
        yield return StartCoroutine(client.Connect());
        room = client.Join(roomName);
    }
    
    public void Join()//button event
    {
        StartCoroutine(ConnectToRoom(rname.text));
        rname.text = "";
    }
    
    public void Create()//button event
    {
        StartCoroutine(ConnectToRoom(rname.text));
        rname.text = "";
    }
    
    IEnumerator ConnectToRoom(String Room_Name)
    {
        Debug.Log("Let's join the room!");
    
        //if Client Dose not Set room name;
        if (Room_Name.Equals(""))
        {
            Room_Name = "NO ROOM NAME";
        }
    
        //Client Connect to Existing room
        if (!isExistingRoom)
        {
            room = client.Join(roomName, new Dictionary<string, object>()
            {
                { "create", true },
                { "name", Room_Name }
            });
        }
        else
        {
            room = client.Join(Room_Name);
            isExistingRoom = false;
        }
    
        room.OnReadyToConnect += (sender, e) =>
        {
            Debug.Log("Ready to connect to room!");
            StartCoroutine(room.Connect());
        };
        room.OnJoin += OnRoomJoined;
        room.OnStateChange += OnStateChangeHandler;
    
        room.Listen("players/:id", this.OnPlayerChange);//players array
        room.Listen("players/:id/:axis", this.OnPlayerMove);//x and y coridinates
        room.Listen("messages/:number", this.OnMessageAdded);//message array
        room.Listen(this.OnChangeFallback);
    
        room.OnMessage += OnMessage;
    
        while (true)
        {
            client.Recv();
            yield return 0;
        }
    }