Skip to content

Commit

Permalink
compatibility with latest @colyseus/core
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Nov 20, 2023
1 parent d08441d commit 830aca9
Show file tree
Hide file tree
Showing 9 changed files with 3,233 additions and 2,097 deletions.
5,284 changes: 3,206 additions & 2,078 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@colyseus/playground",
"version": "0.15.4",
"version": "0.15.6",
"dependencies": {
"@colyseus/core": "^0.15.0"
},
Expand Down
25 changes: 15 additions & 10 deletions src-backend/colyseus.ext.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { Server } from '@colyseus/core';
import http from "http";
import { Server, Room, Client, ClientState } from '@colyseus/core';

const originalDefine = Server.prototype.define;
export const allRoomNames: string[] = [];

// @ts-ignore
Server.prototype.define = function(name, handler, options) {
allRoomNames.push(name);
const original_onJoin = Room.prototype._onJoin;

Room.prototype._onJoin = async function(client: Client, req?: http.IncomingMessage) {
const result = await original_onJoin.apply(this, arguments);

const definition = originalDefine.call(this, name, handler, options);
if (client.state === ClientState.JOINING) {
client.send("__playground_message_types", Object.keys(this['onMessageHandlers']));
}

// notify client about all registered message handlers
definition.on("join", (room, client) => {
client.send("__playground_message_types", Object.keys(room['onMessageHandlers']));
});
return result;
}

return definition
// @ts-ignore
Server.prototype.define = function(name, handler, options) {
allRoomNames.push(name);
return originalDefine.call(this, name, handler, options);
};
5 changes: 3 additions & 2 deletions src/components/JoinRoomForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ export function JoinRoomForm ({

// skip if reconnecting on devMode (previous room events are successfuly re-used.)
// when using .reconnect() events need to be bound again
if (existingConnection && !needRebindEvents) {
return;
if (existingConnection) {
if (!needRebindEvents) { return; }
existingConnection.isConnected = true;
}

// get existing Connection for sessionId, or create a new one
Expand Down
2 changes: 1 addition & 1 deletion src/components/StateView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function StateView({
const [state, setState] = useState(room.state && room.state.toJSON());
const hasState = (room.state !== null);

console.log("RENDER STATE VIEW (bind onStateChange)");
// console.log("RENDER STATE VIEW (bind onStateChange)");
room.onStateChange((state) => setState(state.toJSON()));

return <JsonView src={state} enableClipboard={false} />;
Expand Down
2 changes: 1 addition & 1 deletion src/sections/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function Playground() {
if (global.connections.indexOf(connection) !== -1) {
// reconnected! (via devMode or .reconnect())
connection.isConnected = true;
setConnections(global.connections);
setConnections([...global.connections]);

} else {
// new connection
Expand Down
4 changes: 2 additions & 2 deletions src/utils/ColyseusSDKExt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ Room.prototype['onMessageCallback'] = function(event: MessageEvent) {

function getEventType(code: number) {
// TODO: fix nomenclature on SDK itself
let eventType = Protocol[code].replace("ROOM_", "");
let eventType = Protocol[code]?.replace("ROOM_", "");
if (eventType === "DATA") {
eventType = "MESSAGE";
}
return eventType;
}
}
5 changes: 3 additions & 2 deletions src/utils/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export type Connection = {
events: LimitedArray;
};

export const endpoint = `${window.location.protocol}//${window.location.host}${window.location.pathname.replace(/\/+$/, '')}`;
export const client = new Client();
// export const endpoint = `${window.location.protocol}//${window.location.host}${window.location.pathname.replace(/\/+$/, '')}`;
export const endpoint = "http://localhost:2567"
export const client = new Client(endpoint);

export const global = { connections: [] as Connection[], };

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"noImplicitAny": false,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
Expand Down

0 comments on commit 830aca9

Please sign in to comment.