Socket.IO打破刷新(套接字超时可能?)

时间:2015-09-05 18:26:10

标签: angularjs socket.io

我使用socket.io创建了一个聊天应用程序,除了其中一个用户刷新之外,一切都运行良好。例如:

如果用户A加入房间1,用户B加入房间1,他们可以顺利通信。如果用户A刷新浏览器,他仍然可以实时向用户B发送消息,但用户B不再实时发送用户A消息。

出于某种原因,当用户刷新页面时,他不再能够实时接收消息。以下是我设置聊天系统的方法:

服务器端:

server.listen(port);
var io = require('socket.io').listen(server);
io.on('connection', function(socket){
  console.log("User Connected");

  socket.on('comment', function(data){
    //socket.broadcast.emit('comment', data);
    console.log('server sidecomment');
    socket.broadcast.to(data.chatId).emit('comment',data);
  });

  socket.on('disconnect', function(){
    console.log('user disconnected');
  });

  socket.on('join:room', function(data){
    socket.join(data.chatId);
    socket.broadcast.to(data.chatId).emit('join:room', data)
    console.log('joined room: ' + data.chatId);
  });

  socket.on('leave:room', function(data){
    socket.leave(data.chatId);
    console.log('left room: ' + data.chatId);
  })

  socket.on('typing', function(data){
    console.log('server side start typing');
    console.log(data);
    socket.broadcast.to(data.chatId).emit('typing',data);
  });

  socket.on('stoptyping', function(data){
    console.log('server side stop typing');
    socket.broadcast.to(data.chatId).emit('stoptyping');
  });



});

客户方:

        $scope.$on("$destroy", function() {
            console.log('leaving page');
            socket.emit('leave:room', {
                'chatId': post.chatId
            });
            var leaveMessage = auth.currentUser() + ' has left';
            posts.addComment(post._id, {
                body: leaveMessage,
                author: 'server'
            }).success(function(comment) {
                $scope.post.comments.push(comment);
            });
        });


        socket.on('comment', function(data) {
            console.log('client side comment');
            $scope.post.comments.push(data);
        });
        socket.on('join:room', function(data) {
            var joinedMessage = {
                body: data.author + ' has joined',
                author: 'server'
            };
            $scope.post.comments.push(joinedMessage);
        });

        $scope.whenTyping = function() {
            socket.emit('typing', {
                'chatId': post.chatId,
                'author': auth.currentUser()
            });
        }

        $scope.notTyping = function() {
            socket.emit('stoptyping', {
                'chatId': post.chatId
            });
        }

        socket.on('typing', function(data) {
            $scope.typing = data.author + ' is typing';
        });

        socket.on('stoptyping', function() {
            $scope.typing = '';
        });

我的问题是:套接字是否超时?如果是,有没有办法阻止它超时?

0 个答案:

没有答案