Skip to content

Commit

Permalink
dispatch onJoin signal if client already has 'colyseusid' cookie. col…
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Jun 16, 2017
1 parent f09ecde commit 7c81434
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/lib
out.log
node_modules
npm-debug.log
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist
src
16 changes: 11 additions & 5 deletions dist/colyseus.js

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Tahoma, Geneva, sans-serif; }
</style>
<script src="colyseus.js"></script>
</head>
<body>
<strong>Messages</strong><br>

<form id="form">
<input type="text" id="input" value="" />
<input type="submit" value="send" />
</form>

<div id="messages"></div>

<script>
var host = window.document.location.host.replace(/:.*/, '');
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)
// }

client.onOpen.add(function() {
console.log("onOpen")

room = client.join('chat');
room.onJoin.add(function() {
console.log('joined!');
})

room.onUpdate.add(function(data) {
console.log("chat update: ", data)
});
});

document.getElementById('form').onsubmit = function(e) {
e.preventDefault()

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

// room.send(document.getElementById('input').value);
document.getElementById('input').value = null
}
</script>

</body>
</html>
4 changes: 2 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.0-alpha.1",
"version": "0.7.0-alpha.2",
"description": "Multiplayer Game Client for the Browser",
"keywords": [
"multiplayer",
Expand Down Expand Up @@ -32,7 +32,7 @@
"msgpack-lite": "^0.1.20",
"signals.js": "^1.0.0",
"tiny-emitter": "^1.1.0",
"websocket.js": "^0.1.10"
"websocket.js": "^0.1.12"
},
"devDependencies": {
"@types/chai": "^3.4.34",
Expand Down
15 changes: 10 additions & 5 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ export class Client {
protected rooms: {[id: string]: Room} = {};

constructor (url: string) {
let colyseusid = cookie.getItem('colyseusid');
if (colyseusid) {
this.id = colyseusid;
}

this.connection = new Connection(url);
this.connection.onmessage = this.onMessageCallback.bind(this);
this.connection.onclose = (e) => this.onClose.dispatch();
this.connection.onerror = (e) => this.onError.dispatch();

// check for id on cookie
this.connection.onopen = () => {
console.log("onopen!");
let colyseusid = cookie.getItem('colyseusid');
if (colyseusid) {
this.id = colyseusid;
this.onOpen.dispatch();
}
}
}

join<T> (roomName: string, options: any = {}): Room<T> {
Expand Down
2 changes: 2 additions & 0 deletions src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class Connection extends WebSocketClient {
}

onOpenCallback (event) {
super.onOpenCallback();

if (this._enqueuedCalls.length > 0) {
for (let i=0; i<this._enqueuedCalls.length; i++) {
let [ method, args ] = this._enqueuedCalls[i];
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2826,9 +2826,9 @@ webpack@^2.1.0-beta.25:
webpack-sources "^0.1.0"
yargs "^6.0.0"

websocket.js@^0.1.10:
version "0.1.10"
resolved "https://registry.yarnpkg.com/websocket.js/-/websocket.js-0.1.10.tgz#6bad57a24d0210561018294b49234d21fa9fd7ee"
websocket.js@^0.1.12:
version "0.1.12"
resolved "https://registry.yarnpkg.com/websocket.js/-/websocket.js-0.1.12.tgz#46c980787c57ebc8edcf44a0263e5d639367b85b"
dependencies:
backoff "^2.4.1"

Expand Down

0 comments on commit 7c81434

Please sign in to comment.