I try to use H5 client to test the demo. Firstly, client create the room, then click the button to join the room.
client.js
const client = new Colyseus.Client(ws_origin_url);
client.create('chat').then(room =>{
console.log('room has been created');
});
playButton.addEventListener('click', ()=>{
client.join("chat", { mode: "chit-chat" }).then(room => {
console.log("joined successfully", JSON.stringify(room));
}).catch(e => {
console.error("join error", e);
});
})
chatRoom.js
import { Room } from "colyseus";
export class ChatRoom extends Room {
private maxClient = 4;
constructor() {
super();
}
onCreate(options) {
console.log("chat room created", JSON.stringify(options));
}
onJoin(client, options){
console.log(`client: ${JSON.stringify(client)} has joined this room ${JSON.stringify(options)}` );
}
}
when i request the web page, the console shows.
chat room created {"mode":"chit-chat"}
client: {"sessionId":"BdiirrQTY","readyState":1} has joined this room {}
This means that both onCreate
and onJoin
are triggered in chatRoom.js
. but client hasn't join
yet , as i haven't click the playButton
.
once i clicked the playButton
, the client join the room. in server console, it shows
client: {"sessionId":"u01i6ij3l","readyState":1} has joined this room {"mode":"chit-chat"}
pls notice that the sessionID has changed. Although it is the same client in the same web route.
In Sum, two questions:
-
client only creates the room, but server
onCreate
andonJoin
both are triggered. Is it reasonable? i assume that when the client creates room, onlyonCreate
is triggered; when the client joins the room, thenonJoin
triggered. -
The same client, same route, but sessionID is different before and after
join
the room. why is sessionID changed? can we make it no change because it is the same client?
Very appreciate if anyone can answer.