Hi Prad,
I did not write it that way. Here is what I did in the service:
.factory('socket', ['$rootScope', '$location', function($rootScope, $location){
function init() {
var scServer = new SockJS("/sockserver1");
// On successful connection
scServer.onopen = function (event) {
//do some stuff
};
// On message received
scServer.onmessage = function (event) {
scServer.payload = event.data;
$rootScope.$broadcast('incomingData', event);
};
// On socket close
scServer.onclose = function (event) {
scServer = null;
//do some stuff
}
init();
console.log('scServer created');
return scServer;
}]);
In the controller or directive where you want to get the data:
//inject the socket
.directive('myDirective',['socket', function(socket){
scope.$on('incomingData',function (e) {
var m = JSON.parse(socket.payload);
You will be able to access your data from variable 'm';
I hope this will help you.
Best Regards
Paul
Pradyut Pokuri於 2013年9月20日星期五UTC+8上午2時58分18秒寫道: