当您使用多个标签时,如何避免socket.io中的重复事件?

时间:2016-08-09 20:38:13

标签: javascript angularjs sockets socket.io

当我从服务器ditConsumer收到基于标志的消息时我正在调用一个按预期工作的函数sendMessageToFile(),这里的问题是当我导航到其他页面并再次返回时所以这里是消息socket.on正在侦听两次并执行所有变量和方法两次。如果我来回三次它会听三次,我会很感激这里的帮助,它看起来像多个标签上的socket.io连接问题。

ctrll.js

      socket.on('ditConsumer',function (data) {
            console.log('SEND MESSAGE FLAG',sendMessageFlag)
            console.log('MESSAGE FROM SERVER',data);
            var obj = {
                file:$scope.filename,
                data:data
            }
            $scope.event.push(data);
           // socket.emit('messageToFile',obj);
            if(sendMessageFlag === true) {
               return  sendMessageToFile(obj);
            }
        });

   function sendMessageToFile (data){
         if(data.file) {
             socket.emit('startrecording', data);
             $scope.disabledRecBtn = true;
             $scope.disabledStopBtn = false;
             $scope.showMessage = true;
         }
    }

socketFactory.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);
                    }
                });
            })
        }
    };

});

serverIo.js

var sio = require('socket.io');
var ditconsumer = require('./consumers/ditconsumer');
var logsRecording = require('./records/logsRecording');
var io = null;
exports.io = function () {
    return io;
};
exports.initialize = function(server) {
    io = sio(server);
    io.on('connection', function(socket) {
        // Producer.startProducer();
        ditconsumer.start(function(value){
         io.emit('ditConsumer',value);
         });
        socket.on('createlogfile', function(params) {
            logsRecording.userLogs(function(err,filename) {
                if (err){
                    console.log(err);
                } else {
                    socket.emit('filename', filename);
                }
            });

        });
        socket.on('startrecording', function(obj) {
            logsRecording.recordLogs(obj);
        });
        socket.on('stopRecording',function (filename) {
            console.log('stop recording data',filename);
            logsRecording.deleteFile(filename);
        });
    });
};

1 个答案:

答案 0 :(得分:1)

一种方法是......

// add an event_id to the event message on server-side
io.sockets.emit('my_event', {msg:'hi', event_id: '1'});

// on client side keep track of handled events
var acknowledged = [];

// handle event
io.on('my_event', function (msg) {

    // only continue if the event hasn't been handled before
    if(!~acknowledged.indexOf(msg.event_id)){

        // add to array of acknowledged events
        acknowledged.unshift(msg.event_id);

        // prevent array from growing to large
        if(acknowledged.length > 20){
            acknowledged.length = 20;
        }

        // handle once per event
    }
});

您可能还想利用socket.io会议室并为每个连接创建唯一标识符,或创建类似uuid的内容并将其存储在浏览器的本地存储空间中;读取或创建它并在连接到io时提供它。然后,当您发送活动时,您可以定位特定的房间。

或做类似的事情......