了解下 WebSocket
协议及socket.io。nodejs和socket.io是不错的组合,对于不支持 WebSocket
协议的也做了轮询的fallback,使用很简单(来自于socket.io官网):
后端:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
前端:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>