作者:手机用户2502931993 | 来源:互联网 | 2023-09-13 17:12
HowdoIemitamessagetoallusersinaprivatechatsharingaconversation_idusingnode.jsands
How do I emit a message to all users in a private chat sharing a conversation_id using node.js and socket.io?
如何使用node.js和socket.io在共享conversation_id的私人聊天中向所有用户发送消息?
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
cOnversations= {};
app.get('/', function(req, res) {
res.sendfile('/');
});
io.sockets.on('connection', function (socket) {
socket.on('send message', function (data) {
var conversation_id = data.conversation_id;
if (conversation_id in conversations) {
console.log (conversation_id + ' is already in the conversations object');
// emit the message [data.message] to all connected users in the conversation
} else {
socket.conversation_id = data;
conversations[socket.conversation_id] = socket;
conversations[conversation_id] = data.conversation_id;
console.log ('adding ' + conversation_id + ' to conversations.');
// emit the message [data.message] to all connected users in the conversation
}
})
});
server.listen(8080);
2 个解决方案