Skip to content

Commit

Permalink
send requestId to REQUEST_JOIN. closes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Oct 14, 2017
1 parent 4484ba0 commit 583fa03
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
17 changes: 11 additions & 6 deletions dist/colyseus.js

Large diffs are not rendered by default.

11 changes: 1 addition & 10 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@
var client = new Colyseus.Client('ws://' + host + ':8080');
var room;

// client.onopen = function() {
// console.log('onopen!');
// client.send("hey");
// }
//
// client.onmessage = function(data) {
// console.log("onmessage", data)
// }

function request () {
var http = new XMLHttpRequest();
var params = '{"lorem":"ipsum","name":"binny"}';
Expand Down Expand Up @@ -66,7 +57,7 @@
document.getElementById('form').onsubmit = function(e) {
e.preventDefault()

client.send(document.getElementById('input').value);
room.send(document.getElementById('input').value);

// room.send(document.getElementById('input').value);
document.getElementById('input').value = null
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "colyseus.js",
"version": "0.7.3",
"version": "0.7.4",
"description": "Multiplayer Game Client for the Browser",
"keywords": [
"multiplayer",
Expand All @@ -20,7 +20,9 @@
"build": "tsc && npm run build-dist",
"watch": "tsc -w",
"prepublish": "npm run build",
"test": "npm run build && mocha test/"
"test": "npm run build && mocha test/",
"postinstall": "npm run -s donate",
"donate": "echo \"\u001b[35m\u001b[1mLove Colyseus? Support its development by becoming a backer:\u001b[22m\u001b[39m\n > \u001b[34mhttps://opencollective.com/colyseus/donate\u001b[39m\""
},
"engines": {
"node": ">= 5.x"
Expand Down
20 changes: 14 additions & 6 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ export class Client {
public onError: Signal = new Signal();

protected connection: Connection;
protected room: Room;

protected rooms: {[id: string]: Room} = {};
protected connectingRooms: {[id: string]: Room} = {};
protected joinRequestId = 0;

protected hostname: string;
protected storage: Storage = window.localStorage;
Expand Down Expand Up @@ -52,11 +54,13 @@ export class Client {
}

join<T> (roomName: string, options: any = {}): Room<T> {
this.room = new Room<T>(roomName);
options.requestId = ++this.joinRequestId;

this.connectingRooms[ options.requestId ] = new Room<T>(roomName);

this.connection.send([Protocol.JOIN_ROOM, roomName, options]);

return this.room;
return this.connectingRooms[ options.requestId ];
}

/**
Expand All @@ -73,12 +77,16 @@ export class Client {
this.onOpen.dispatch();

} else if (code == Protocol.JOIN_ROOM) {
let room = this.room;
let requestId = message[2];
let room = this.connectingRooms[ requestId ];

this.rooms[room.id] = room;

room.id = message[1];
room.connect(new Connection(`${ this.hostname }/${ this.room.id }?colyseusid=${ this.id }`));
room.connect(new Connection(`${ this.hostname }/${ room.id }?colyseusid=${ this.id }`));
room.onLeave.add(() => delete this.rooms[room.id]);

this.rooms[room.id] = room;
delete this.connectingRooms[ requestId ];

} else if (code == Protocol.JOIN_ERROR) {
console.error("server error:", message[2]);
Expand Down

0 comments on commit 583fa03

Please sign in to comment.