Created
March 6, 2016 14:49
-
-
Save kizdolf/a336225d0a3900afab02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Declare app level module which depends on views, and components | |
angular.module('appName', [ | |
... | |
'appName.socket' | |
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('appName.ctrlName', ['ngRoute']) | |
.controller('ctrlName', ['appName.socket',... , | |
function(socket, ...) { | |
socket.on('event', function(data){ | |
//play with data and scope | |
$scope.varName = data.stuff; | |
}); | |
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('appName.socket',[]) | |
.factory('appName.socket', function ($rootScope) { | |
var host = "ip, localhost by default."; | |
var port = 8080; //or whatever uou use. | |
var socket = io.connect(host + ':' + port); //connect() will go to hostname by default | |
return { | |
on: function (eventName, callback) { | |
socket.on(eventName, function () { | |
var args = arguments; | |
$rootScope.$apply(function () { | |
callback.apply(socket, args); | |
}); | |
}); | |
}, | |
emit: function (eventName, data, callback) { | |
socket.emit(eventName, data, function () { | |
var args = arguments; | |
$rootScope.$apply(function () { | |
if (callback) { | |
callback.apply(socket, args); | |
} | |
}); | |
}); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment