[英]send message stream to view using express js and socket.io
I have the following nodejs/express js 我有以下nodejs / express js
/* GET home page. */
router.get('/', function(req, res, next) {
socket.on(channelName, message => {
//console.log(channelName, message);
});
res.render('index', {page:message, menuId:channelName});
});
How can I pass the message
it was the real time data to display in index
view? 如何传递message
以实时数据形式显示在index
视图中?
In the route method, you will need to fire/emit the event, so for example 在route方法中,您将需要触发/发出事件,例如
when a message hit this route all the listeners on the 'message' 当一条消息到达此路由时,“消息”上的所有听众
will call the Action : 将调用操作:
app.post('/messages', (req, res) => {
var message = new Message(req.body);
message.save((err) =>{
if(err)
sendStatus(500);
io.emit('message', req.body);
res.sendStatus(200);
})
})
And in the client side script tag in index.html, add the following code: 然后在index.html的客户端脚本标记中,添加以下代码:
var socket = io();
socket.on(‘message’, addMessages)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.