public
Last active

web sockets -> backbone app adapter

  • Download Gist
1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
HIT = new Backbone.Marionette.Application();
// ... other settings here
 
 
 
 
// Socket.io Adapter
// -----------------
// This adapter forwards all socket.io events
// to the application's event aggregator. The
// intention of this is to decouple the specific
// websockets implementation from the application's
// use of it.
 
var socket;
 
var initialize = function() {
socket = io.connect(HIT.socketUrl);
 
socket.on("connect", function() {
SocketAdapter.connected = true;
HIT.vent.trigger("socket:connected");
});
 
socket.on("someData", function(data){
HIT.vent.trigger("someData", data);
});
 
socket.on("disconnect", function() {
SocketAdapter.connected = false;
HIT.vent.trigger("socket:disconnected");
});
 
socket.on("error", function(err){
console.log("ERROR: ", err);
});
 
};
 
HIT.addInitializer(initialize);
 
 
// Handle the event, start the app and the socket adapter
 
HIT.vent.on("someData", function(data){
alert("some data came through!");
});
 
HIT.start();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.