我使用 nodejs 的命名空间开发了一个应用程序,其中客户端(client1)将数据发送到服务器,服务器将相同的数据发送到另一个客户端(client2)。一切正常,除了只有在我刷新页面时才会在 client2 中呈现数据。如何动态更新数据?
代码如下: server::
var io = require('socket.io').listen(8007);
var chat = io
.of('/chat')
.on('connection', function (socket) {
socket.on('message', function(message){
console.log("Received message:: " + message + " - from client " + socket.id);
var news = io
.of('/news')
.on('connection', function (socket1) {
socket1.send("received::" + message + " - from client " + socket.id);
});
});
});
客户端1是::
<script>
var count=0;
function connect() {
var chat = io.connect('http://localhost:8007/chat');
chat.send('chat message:::' + count);
count++;
}
</script>
接收客户是::
<script>
var news = io.connect('http://localhost:8007/news');
news.on('message', function(data){
document.getElementById('message').innerHTML = "\n Server says: " + data;
});
</script>