Joining specific rooms, playing against friends

Hello!

How would you go about letting people join specific rooms? For example, suppose I want to play against my friends; how would I join their room? I saw Federkun's workaround here, but it would be nice if this feature was included in Colyseus.

If this hasn't been implemented, is there any way I can help? :)

Hey @ScottyFillups,

Whilst it's on the framework's roadmap to have public room listing, it's currently possible to share the room id with friends, which allow them to connect directly to it. (Docs: http://colyseus.io/docs/client-room/#properties)

You'd need to handle this case on your own, though.

You have a couple of options, such as:

  1. Letting the user set a password for the private room upon creation, and prevent clients from joining without the password on requestJoin.
class MyRoom extends Room {
  onInit (options) {
    this.password = options.password;
  }

  requestJoin (options) {
    return (this.password && options.password == this.password);
  }

}
  1. Registering a new handler "private" for private rooms. Fresh clients could join only the "public" by default, while the "private" rooms could have the id shared with friends. On this setup there's a possibility that users can "hack" the JavaScript file, allowing to join the "private" rooms by default instead, but that's unlikely to happen.
gameServer.register("public", MyRoom);
gameServer.register("private", MyRoom);

Hope this helps! Cheers!

One thing I've forgotten to mention is how to connect to the "private" room from the client-side. You'd have this call in your client:

import { Client } from "colyseus.js";

const client = new Client("ws://...");
client.join("private", { password: "someUniqueCode" })

@endel said in Joining specific rooms, playing against friends:

One thing I've forgotten to mention is how to connect to the "private" room from the client-side. You'd have this call in your client:

import { Client } from "colyseus.js";

const client = new Client("ws://...");
client.join("private", { password: "someUniqueCode" })

I have a problem with private room. If same password and this room is fully connected=> new Room'll be created with this same password. I don't want this, that I warning for user "This room is fully, plz...."

Do you have any solutions for this problem?

Hi @DinhNguyen,

The requestJoin() method has a isNew as a second argument, you may check for it whenever requesting to join a room with a password.

Example:

requestJoin(options, isNew) {
  if (options.password) {
    // this forces to accept only if a room already exists
    return this.password === options.password && !isNew;

  } else {
    // always dissallow joining without a password
    // (you may want to change this)
    return false; 
  }
}

I haven't tested this code, you may need to modify it slighly

FYI, latest API supports this now. Simply pass the room handle or roomId.
https://docs.colyseus.io/client/client/#join-roomnameorid-string-options-any