Node.js NodeJS和socket.io聊天自定义

Node.js NodeJS和socket.io聊天自定义,node.js,socket.io,Node.js,Socket.io,我有一个使用NodeJS、express和socket.io的基本聊天系统。现在我想让服务器每隔5秒将日期插入聊天流。由于这不是由用户发起的,因此我在基本请求方面遇到了问题。我对NodeJS还不熟悉,也许这只是我不懂的语法问题。无论如何,使用当前代码,只有在有人通过发送聊天信息后才能插入日期。我希望这在服务器端自动发生。如果没有人聊天,日期仍将每隔5秒发送给客户。我的问题很可能源于评论部分的标题:如何在这里设置周期计时器。。。相反,我试图在底部插入它,它说-//***这一部分发送通知。。。我是否

我有一个使用NodeJS、express和socket.io的基本聊天系统。现在我想让服务器每隔5秒将日期插入聊天流。由于这不是由用户发起的,因此我在基本请求方面遇到了问题。我对NodeJS还不熟悉,也许这只是我不懂的语法问题。无论如何,使用当前代码,只有在有人通过发送聊天信息后才能插入日期。我希望这在服务器端自动发生。如果没有人聊天,日期仍将每隔5秒发送给客户。我的问题很可能源于评论部分的标题:如何在这里设置周期计时器。。。相反,我试图在底部插入它,它说-//***这一部分发送通知。。。我是否有不同的功能结构?提前谢谢

Server.js

var express = require('express'),
app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);

// listen for new web clients:
server.listen(8080);

app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});         

app.get('/sio/socket.io.js', function (req, res) {
res.sendfile('/root/nodejs/node-v0.10.0/node_modules/socket.io/lib/socket.io.js');
});     

//How do I get my periodic timer in here so it can send the date every 5 seconds?
io.sockets.on('connection', function (socket) {
   socket.on('sendMessage', function (data) {
     socket.broadcast.emit('message', data);
     socket.emit('message', { text: '<strong>'+data.text+'</strong>' });   
   });   
});

// Periodic Running
var coolerInterval = function(func, interval, triggerOnceEvery) {
var startTime = new Date().getTime(),
    nextTick = startTime,
    count = 0;
triggerOnceEvery = triggerOnceEvery || 1;

var internalInterval = function() {
    nextTick += interval;
    count++;
    if(count == triggerOnceEvery) {
        func();
        count = 0;
    }
    setTimeout(internalInterval, nextTick - new Date().getTime());
};
internalInterval();
};

coolerInterval(function() {
showdate = new Date().getTime();
console.log( showdate );
  //Go ahead and send a notification to everyone.

  //***This section sends out the notification that a pick was made
    io.sockets.on('connection', function (socket) {
      socket.on('sendMessage', function (data) {
        socket.broadcast.emit('message', showdate);   
      });  
    });
  //***End sending out notification.    

}, 1000, 5);
//End Periodic
以下是浏览器-index.html中的html

<html> 
   <body>
    <script src="/socket.io/socket.io.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script>
          $(document).ready(function () {
            var socket = io.connect('http://dev.mysite.com:8080');
            socket.on('message', function (data) {
              $('#chat').append(data.text + '<br />');
            });

            $('#send').click(function () {
              socket.emit('sendMessage', { text: $('#text').val() });
              $('text').val('');
            });

          });
        </script>

        <div id="chat" style="width: 500px; height: 300px; border: 1px solid black">

        </div>    

        <input type="text" name="text" id="text">
        <input type="button" name="send" id="send" value="send">
      </body>
    </html> 

这比你做的要简单得多。您可以将Interval设置为5秒,然后调用io.sockets.emit,这将向所有连接的套接字发送消息

setInterval(function() {
    io.sockets.emit('message', (new Date()).getTime());
}, 5000);

在第18行执行此操作,并删除下面的所有内容。

这比您所做的要简单得多。您可以将Interval设置为5秒,然后调用io.sockets.emit,这将向所有连接的套接字发送消息

setInterval(function() {
    io.sockets.emit('message', (new Date()).getTime());
}, 5000);

在第18行执行此操作,并删除下面的所有内容。

io.sockets.emit'this',{will'bereceivebyeveryone'};发出'this',{will:'bereceivebyeveryone'};对这其实很简单。现在,每5秒就会出现一条消息。奇怪的是,上面写的是“未定义”而不是“日期”。我只是试图输出单词testing,但它仍然是未定义的。setIntervalfunction{io.sockets.emit'message','testing';},5000;哦,我想是我对nodejs阵列的新认识导致了这个问题。我把它改为setIntervalfunction{io.sockets.emit'message',{text:'testing'};},5000;现在它工作得很好。谢谢对这其实很简单。现在,每5秒就会出现一条消息。奇怪的是,上面写的是“未定义”而不是“日期”。我只是试图输出单词testing,但它仍然是未定义的。setIntervalfunction{io.sockets.emit'message','testing';},5000;哦,我想是我对nodejs阵列的新认识导致了这个问题。我把它改为setIntervalfunction{io.sockets.emit'message',{text:'testing'};},5000;现在它工作得很好。谢谢