你可以设置socket.io来监听你的服务器
/server/config/system/bootstrap.js
需要socket.io模块
var express = require('express'),
appPath = process.cwd(),
io = require('socket.io');
现在设置 socket.io 来监听您的应用程序
// Express settings
var app = express();
require(appPath + '/server/config/express')(app, passport, db);
io = io(app.listen(3000));
return io;
然后,您需要通过 bootstrapDependency() 函数将 socket.io 对象注入到您的应用程序中。
function bootstrapDependencies() {
...
// Register socket.io dependency
mean.register('io', function() {
return io;
});
}
Mean.uses 该项目进行依赖注入https://www.npmjs.org/package/dependable https://www.npmjs.org/package/dependable
最后,您需要配置您的应用程序以侦听每个套接字连接
可能您想在主应用程序的路由器上执行这些操作
/server/routes/index.js
连接处理程序示例
var io = require('meanio').io;
io.on('connection', function (socket) {
// emit data to the clients
socket.emit('news', { hello: 'world' });
// event listeners
socket.on('my other event', function (data) {
// call your controller function here
Controller.action(data);
});
});
更重要的是,不要忘记在客户端设置 socket.io。
// on '/server/views/includes/foot.html'
<script src='/socket.io/socket.io.js'></script>
<script>
var socket = io();
</script>