最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disconnect and reconnect socket.io from client? - Stack Overflow

programmeradmin0浏览0评论

I have log messages that are being emitted from server to client and that are ing consistently from server and logging to client, Now I have additional functionality to stop and play logs to give some controls to user, So I am thinking on stop to disconnect socket.io connection and on play start socket.io connection again, First i am trying with stop to disconnect connection but i could not emit message to server ,any idea what is wrong with below code or any better solution to achieve these task ? main.html

<button type="button" class="btn btn-success" ng-click="stopLogs()">stop</button>

ctrl.js

$scope.stopLogs = function(){
        socket.emit('end');
    }

angularSOcketFactory.js

angular.module('App').factory('socket', function ($rootScope) {
    'use strict';
        var socket = io.connect('http://localhost:3000');
        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);
                        }
                    });
                })
            }
        };

});

server.js

var io = require('socket.io')(server);
  io.sockets.on('connection',function(){
   // Producer.startProducer();
    ditconsumer.start(function(value){
        io.emit('ditConsumer',value)
    });
    io.sockets.on('end', function() {
        socket.disconnect();
        console.log('it will disconnet connection');
    });
});

I have log messages that are being emitted from server to client and that are ing consistently from server and logging to client, Now I have additional functionality to stop and play logs to give some controls to user, So I am thinking on stop to disconnect socket.io connection and on play start socket.io connection again, First i am trying with stop to disconnect connection but i could not emit message to server ,any idea what is wrong with below code or any better solution to achieve these task ? main.html

<button type="button" class="btn btn-success" ng-click="stopLogs()">stop</button>

ctrl.js

$scope.stopLogs = function(){
        socket.emit('end');
    }

angularSOcketFactory.js

angular.module('App').factory('socket', function ($rootScope) {
    'use strict';
        var socket = io.connect('http://localhost:3000');
        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);
                        }
                    });
                })
            }
        };

});

server.js

var io = require('socket.io')(server);
  io.sockets.on('connection',function(){
   // Producer.startProducer();
    ditconsumer.start(function(value){
        io.emit('ditConsumer',value)
    });
    io.sockets.on('end', function() {
        socket.disconnect();
        console.log('it will disconnet connection');
    });
});
Share Improve this question edited Jul 15, 2016 at 20:35 hussain asked Jul 15, 2016 at 20:19 hussainhussain 7,12321 gold badges87 silver badges164 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

This code is wrong:

io.sockets.on('end',function () {
console.log('this will disconnet socket connection');
 io.sockets.disconnect();
});

You need to apply that event listener to one specific socket and then you need to disconnect one specific socket.

You don't show the general context of your server code, but it could be done like this:

io.on('connection', function(socket) {
    socket.on('end', function() {
        socket.disconnect();
    });
});

You could also just have the client disconnect directly rather than asking the server to do it for you.

发布评论

评论列表(0)

  1. 暂无评论