Skip to content

Commit

Permalink
send requestId back to client on JOIN_ROOM. colyseus/colyseus.js#14
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Oct 14, 2017
1 parent dba8557 commit 921dd16
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/Protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ export function decode (message: any) {
}

export function send (client: Client, message: any[]) {
// [Protocol.JOIN_ERROR, roomId, err]
client.send(msgpack.encode(message), { binary: true });
}
14 changes: 6 additions & 8 deletions src/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { EventEmitter } from "events";
import { createTimeline, Timeline } from "@gamestdio/timeline";

import { Client } from "./index";
import { Protocol } from "./Protocol";
import { Protocol, send } from "./Protocol";
import { logError, spliceOne, toJSON } from "./Utils";

import { debugPatch } from "./Debug";
Expand Down Expand Up @@ -102,7 +102,7 @@ export abstract class Room<T=any> extends EventEmitter {
}

public send (client: Client, data: any): void {
client.send( msgpack.encode( [Protocol.ROOM_DATA, this.roomId, data] ), { binary: true }, logError.bind(this) );
send(client, [ Protocol.ROOM_DATA, this.roomId, data ]);
}

public broadcast (data: any): boolean {
Expand Down Expand Up @@ -132,15 +132,13 @@ export abstract class Room<T=any> extends EventEmitter {
}

protected sendState (client: Client): void {
client.send( msgpack.encode( [
send(client, [
Protocol.ROOM_STATE,
this.roomId,
this._previousState,
this.clock.currentTime,
this.clock.elapsedTime,
] ), {
binary: true
}, logError.bind(this) );
]);
}

private broadcastPatch (): boolean {
Expand Down Expand Up @@ -177,7 +175,7 @@ export abstract class Room<T=any> extends EventEmitter {
this.clients.push( client );

// confirm room id that matches the room name requested to join
client.send( msgpack.encode( [Protocol.JOIN_ROOM, client.sessionId] ), { binary: true }, logError.bind(this) );
send(client, [ Protocol.JOIN_ROOM, client.sessionId ]);

// send current state when new client joins the room
if (this.state) {
Expand Down Expand Up @@ -207,7 +205,7 @@ export abstract class Room<T=any> extends EventEmitter {
// process after calling `client.close()` here
//
if (!isDisconnect) {
client.send( msgpack.encode( [Protocol.LEAVE_ROOM, this.roomId] ), { binary: true }, logError.bind(this) );
send(client, [ Protocol.LEAVE_ROOM, this.roomId ]);
}

// custom cleanup method & clear intervals
Expand Down
2 changes: 1 addition & 1 deletion src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class Server {
this.matchMaker.onJoinRoomRequest(roomName, joinOptions, true, (err: string, room: Room<any>) => {
let joinRoomResponse = (err)
? [ Protocol.JOIN_ERROR, roomName, err ]
: [ Protocol.JOIN_ROOM, room.roomId ];
: [ Protocol.JOIN_ROOM, room.roomId, joinOptions.requestId ];

send(client, joinRoomResponse);
});
Expand Down
6 changes: 3 additions & 3 deletions src/cluster/Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as msgpack from "msgpack-lite";
import * as parseURL from "url-parse";

import { Server as WebSocketServer } from "uws";
import { Protocol } from "../Protocol";
import { Protocol, send } from "../Protocol";
import { MatchMaker } from "../MatchMaker";
import { Client, Room, generateId } from "../";

Expand All @@ -18,7 +18,7 @@ export function setUserId (client: Client) {
client.id = url.query['colyseusid'] || generateId();

if (!url.query['colyseusid']) {
client.send( msgpack.encode([ Protocol.USER_ID, client.id ]), { binary: true } );
send(client, [ Protocol.USER_ID, client.id ]);
}
}

Expand Down Expand Up @@ -103,7 +103,7 @@ export function setupWorker (server: net.Server, matchMaker: MatchMaker) {
matchMaker.onJoinRoomRequest(roomNameOrId, joinOptions, allowCreateRoom, (err: string, room: Room<any>) => {
let joinRoomResponse = (err)
? [ Protocol.JOIN_ERROR, roomNameOrId, err ]
: [ Protocol.JOIN_ROOM, room.roomId ];
: [ Protocol.JOIN_ROOM, room.roomId, joinOptions.requestId ];

// send response back to match-making process.
getMatchMakingProcess(matchMakingPid => {
Expand Down

0 comments on commit 921dd16

Please sign in to comment.