更新时间:2023-02-16 23:24:14
由于你的平均堆栈上,在节点标准的建议是使用的 Socket.IO API。
Since you're on the MEAN stack, the standard recommendation in Node would be to use the Socket.IO API.
他们提供了双向通信的下面的例子(这将有利于您的推送消息很容易):
They provide the following example of two way messaging (which would facilitate your push messages very easily):
客户端
<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>
服务器
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(80);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
它会使用WebSockets在可能的情况,并退回到AJAX长轮询或Flash轮询在浏览器中在没有WebSocket的支持。
It will use websockets where possible, and fallback to AJAX long polling or Flash polling in browsers where there is no websocket support.
至于有角整合,这里的一对 Socket.IO和角:
As for integrating with Angular, here's a good blog post on Socket.IO and Angular:
我会写关于如何整合Socket.IO添加实时
功能到AngularJS应用。在本教程中,我将
通过写一个即时消息应用程序走。
I'll be writing about how to integrate Socket.IO to add real-time features to an AngularJS application. In this tutorial, I'm going to walk through writing a instant messaging app.