Skip to content
This repository has been archived by the owner on Nov 7, 2020. It is now read-only.

add generic message handler to transport #428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion public/js/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var logger = new logging.Logger("transport");
var Transport = function(roomId) {
this._setState("CONNECTING");
this.stateModels = {};
this.messages = _.extend({}, Backbone.Events);

this.sock = new SockJS(
document.location.protocol + "//" +
Expand Down Expand Up @@ -44,6 +45,9 @@ var Transport = function(roomId) {
case "join-err":
this._setState("JOIN-ERROR");
break;
case "message":
this.handleMessage(msg.args);
break;
case "state":
this.handleStateChange(msg.args);
break;
Expand Down Expand Up @@ -101,10 +105,24 @@ _.extend(Transport.prototype, Backbone.Events, {
registerModel: function(name, model) {
this.stateModels[name] = model;
},
unregisterModel: function(name, moel) {
unregisterModel: function(name) {
delete this.stateModels[name];
},

// 'handleMessage' allows for handling generic messages.
// Consumers can listen for specific message types on
// Transport.messages.
handleMessage: function(args) {
// 'args' should look like:
// {
// type: 'some-message-type',
// data: {
// foo: bar,
// }
// }
this.messages.trigger(args.type, args.data);
},

// `handleStateChange' takes a set of basic operations (modeled after
// sharejs's operations, as we might move to that eventually) to mutate the
// state of backbone models.
Expand Down