Skip to content

Commit

Permalink
Added rudimentary message system from session instance to session ins…
Browse files Browse the repository at this point in the history
…tance.
  • Loading branch information
Christine Emrich committed Nov 4, 2013
1 parent 8663f77 commit c7a1fa3
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 2 deletions.
13 changes: 13 additions & 0 deletions multi/client/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,23 @@ define(function(require, exports, module) {
player.updateAttributesFromServer(data.attributes);
}
});

socket.on('message', function (data) {
session.dispatchEvent(data.type, data);
});
};

util.inherits(Session, EventDispatcher);

/**
* Sends the given message to all other instances of this session.
* @param {string} type type of message that should be send
* @param {object} [data] message data that should be send
*/
Session.prototype.message = function (type, data) {
this.socket.emit('message', { type: type, data: data });
};

/**
* Unpacks a session object send over a socket connection.
* @returns {module:client/session~Session}
Expand Down
6 changes: 6 additions & 0 deletions multi/server/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ var Player = function (socket) {
}
}
});

// the corresponding session sends us a message
// this should be mirrored to the other session instances
this.socket.on('message', function (event) {
player.dispatchEvent('message', event);
});
};

/* class methods */
Expand Down
4 changes: 4 additions & 0 deletions multi/server/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ var Session = function (io, options) {

util.inherits(Session, EventDispatcher);

Session.prototype.onPlayerMessage = function (event) {
this.sendToPlayers('message', { type: event.type, data: event.data });
};

/**
* Relays a given event to all players currently connected
Expand Down Expand Up @@ -120,6 +123,7 @@ Session.prototype.addPlayer = function (player) {
session.removePlayer(player);
});
player.on('attributesChanged', this.onPlayerAttributesChanged);
player.on('message', this.onPlayerMessage.bind(this));
this.dispatchEvent('playerAdded', { player: player });
};

Expand Down
2 changes: 1 addition & 1 deletion public/css/symbols.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ button {
background-color: rgb(0, 169, 235);
color: white;
border: none;
border: 1px solid white;
border: 1px solid rgba(0,0,0,0);
}

button:after {
Expand Down
13 changes: 13 additions & 0 deletions public/js/lib/multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,23 @@ define('session',['require','exports','module','../shared/eventDispatcher','./pl
player.updateAttributesFromServer(data.attributes);
}
});

socket.on('message', function (data) {
session.dispatchEvent(data.type, data);
});
};

util.inherits(Session, EventDispatcher);

/**
* Sends the given message to all other instances of this session.
* @param {string} type type of message that should be send
* @param {object} [data] message data that should be send
*/
Session.prototype.message = function (type, data) {
this.socket.emit('message', { type: type, data: data });
};

/**
* Unpacks a session object send over a socket connection.
* @returns {module:client/session~Session}
Expand Down
7 changes: 7 additions & 0 deletions public/js/symbols/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ define(function(require, exports, module) {
session.on('destroyed', function () {
alert('Opps - you have no connection. Try a reload when your connection returns.');
});
session.on('start', sound.onStart);

$('html').click(changeColor);
}

function onStartGameClick(event) {
event.stopPropagation();
session.message('start');
}

function changeColor() {
var color = multi.color.random();
session.myself.attributes.color = color;
Expand All @@ -65,6 +71,7 @@ define(function(require, exports, module) {
sound = soundModule;
$('#join .join').click(onJoinSessionClick);
$('#join .icon').click(onIconClick);
$('#created .start').click(onStartGameClick);
$('#intro').hide();
$('#join').show();
}
Expand Down
1 change: 1 addition & 0 deletions public/js/symbols/presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ define(function(require, exports, module) {
session.on('destroyed', function () {
alert('Opps - you have no connection. Try a reload when your connection returns.');
});
session.on('start', sound.onStart);

$('#new').show();
$('#loading').hide();
Expand Down
1 change: 1 addition & 0 deletions public/js/symbols/sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ define(['exports', '../lib/audio', '../lib/jsfx', '../lib/jsfxlib'], function (e
exports.onSymbol = function() { play('beat'); };
exports.onPlayerJoin = function() { play('ring'); };
exports.onPlayerDisconnect = function() { play('lost'); };
exports.onStart = function() { play('beat'); };

return exports;
});
26 changes: 25 additions & 1 deletion tests/client/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ requirejs(['multi', 'http://localhost/socket.io/socket.io.js'], function (multiM
ok(joinedSession, "session can be joined");
ok(joinedSession.myself, "session has own player added");
equal(joinedSession.token, createdSession.token, "session tokens are equal");ok(joinedSession.players[createdSession.myself.id], "old player added to new session");
multi.off('sessionJoined');
start();
});
createdSession.on('playerJoined', function (event) {
Expand All @@ -52,6 +51,31 @@ requirejs(['multi', 'http://localhost/socket.io/socket.io.js'], function (multiM
});
});

asyncTest("test mirroring session messages", function () {
expect(4);

multi.createSession().then(function (session) {
var data = { test: 42, foo: 'bar' };
var createdSession = session;

multi.joinSession(createdSession.token).then(function (session) {

createdSession.on('ping', function (event) {
ok(true, "ping message received");
createdSession.message('pong', data);
});
session.on('pong', function (event) {
ok(true, "pong message received");
equal(event.type, "pong", "message type is correct");
deepEqual(event.data, data, "message data is correct");
start();
});

session.message('ping');
});
});
});

// all modules & tests loaded, so begin testing
QUnit.start();

Expand Down
2 changes: 2 additions & 0 deletions views/games/symbols.jade
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ block content
#created
img(src='../img/icons/star.svg')
h2 You did it!
p Tap to change your player color.
button.start Start game

#loading
.inner
Expand Down

0 comments on commit c7a1fa3

Please sign in to comment.