it hard time of me with socket,my connection is establish successfully,but at line of subscribe it is not working. is there any thing, i am missing. i checked everything,i didn't get my mistake
My client side code controller is :
var socket = getSocket($scope);
var _startSocket = function() {
socket.emit('subscribe', 'roomOne');
socket.on('tx', function(tx) {
$scope.txs.unshift(tx);
if (parseInt($scope.txs.length, 10) >= parseInt(TRANSACTION_DISPLAYED, 10)) {
$scope.txs = $scope.txs.splice(0, TRANSACTION_DISPLAYED);
}
});
Service is
var ScopedSocket = function(socket, $rootScope) {
this.socket = socket;
this.$rootScope = $rootScope;
this.listeners = [];
};
ScopedSocket.prototype.removeAllListeners = function(opts) {
if (!opts) opts = {};
for (var i = 0; i < this.listeners.length; i++) {
var details = this.listeners[i];
if (opts.skipConnect && details.event === 'connect') {
continue;
}
this.socket.removeListener(details.event, details.fn);
}
this.listeners = [];
};
ScopedSocket.prototype.on = function(event, callback) {
var socket = this.socket;
var $rootScope = this.$rootScope;
var wrapped_callback = function() {
var args = arguments;
$rootScope.$apply(function() {
callback.apply(socket, args);
});
};
socket.on(event, wrapped_callback);
this.listeners.push({
event: event,
fn: wrapped_callback
});
};
ScopedSocket.prototype.emit = function(event, data, callback) {
var socket = this.socket;
var $rootScope = this.$rootScope;
socket.emit(event, data, function() {
console.log("event"+event);
console.log("data"+data);
var args = arguments;
$rootScope.$apply(function() {
if (callback) {
callback.apply(socket, args);
}
});
});
};
angular.module('explorer.socket').factory('getSocket',
function($rootScope) {
var socket = io.connect('http://localhost:3001/socket.io/', {
'reconnect': true,
'reconnection delay': 500,
});
return function(scope) {
var scopedSocket = new ScopedSocket(socket, $rootScope);
scope.$on('$destroy', function() {
scopedSocket.removeAllListeners();
});
socket.on('connect', function() {
scopedSocket.removeAllListeners({
skipConnect: true
});
});
return scopedSocket;
};
});
Server code
module.exports.init = function(io_ext) {
ios = io_ext;
if (ios) {
console.log("socket"+ios);
// when a new socket connects
ios.sockets.on('connection', function(socket) {
console.log("socketconnection"+socket.id);
logger.verbose('New connection from ' + socket.id);
// when it subscribes, make it join the according room
socket.on('subscribe', function(topic) {
console.log("subscribe"+JSON.stringif(topic));
logger.debug('subscribe to ' + topic);
socket.join(topic);
socket.emit('subscribed');
});
// disconnect handler
socket.on('disconnect', function() {
logger.verbose('disconnected ' + socket.id);
});
});
}
return ios;
};
Aucun commentaire:
Enregistrer un commentaire