我使用socket.io记录来自服务器的数据,我有一个要求,用户应该可以通过在UI端使用停止/播放按钮断开连接并重新连接socket.io,所以这里的问题是当我断开套接字工作,但当我试图重新连接它不再创建连接。如何在没有页面刷新的情况下重新连接socket.io?
Factory.js
angular.module('loggingApp').factory('socket', function ($rootScope) {
'use strict';
var server = 'http://localhost:3000';
var socket = io.connect(server,{'forceNew': true });
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);
}
});
})
},
disconnect: function () {
socket.disconnect();
},
reconnect: function () {
console.log('socket.reconnect()');
//socket.io.reconnect();
socket = io.connect(server,{'forceNew': true });
},
};
});
ctrl.js
$scope.playLogs = function(){
console.log('inside start function');
socket.reconnect();
}
$scope.stopLogs = function(){
console.log('inside stop funtion');
socket.disconnect();
}