我正在使用socket.io做我的第一个应用程序,我想向任何用户发送消息。我知道广播功能,但我想向任何人发送消息。我已经找到并且有一种方法:使用io.sockets.emit,但我不知道在哪里编写该代码因为我在node.js上使用express而且我在我的函数中接收了一个socket(当前用户)。
谢谢。
答案 0 :(得分:1)
以下是聊天的示例。 完整代码为https://github.com/bwcho75/node.js_study/tree/master/node_chatting
希望它对你有所帮助。 附:它使用房间。所以,如果你想向所有用户发送消息。 将io.sockets.in(room).emit更改为io.sockets.emit
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var app = express();
app.use(express.bodyParser());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
var httpServer =http.createServer(app).listen(3000, function(req,res){
console.log('Socket IO server has been started');
});
// upgrade http server to socket.io server
var io = require('socket.io').listen(httpServer);
var count = 0;
var rooms = new Array() ;
app.get('/:room',function(req,res){
console.log('room name is :'+req.params.room);
res.render('index',{room:req.params.room});
});
io.sockets.on('connection',function(socket){
socket.on('joinroom',function(data){
socket.join(data.room);
socket.set('room', data.room,function() {
var room = data.room;
var nickname = '손님-'+count;
socket.emit('new', {nickname: nickname});
// Create Room
if(rooms[room] == undefined ){
console.log('room create :'+room);
rooms[room] = new Object();
rooms[room].nicknames = new Object();
rooms[room].socket_ids = new Object();
}
// Store current user's nickname and socket.id to MAP
rooms[room].socket_ids[nickname] = socket.id
rooms[room].nicknames[socket.id] = nickname;
// broad cast join message
data = {msg : nickname +' 님이 입장하셨습니다.'};
io.sockets.in(room).emit('broadcast_msg',data);
// broadcast changed user list in the room
io.sockets.in(room).emit('userlist',{users:Object.keys(rooms[room].socket_ids)});
count++;
});
});
socket.on('changename',function(data){
socket.get('room',function(err,room){
var nickname = data.nickname;
// if user changes name get previous nickname from nicknames MAP
var previous_nickname = rooms[room].nicknames[socket.id];
if(previous_nickname != undefined){
delete rooms[room].socket_ids[previous_nickname];
}
rooms[room].socket_ids[nickname] = socket.id
rooms[room].nicknames[socket.id] = nickname;
data = {msg : previous_nickname +' 님이 '+nickname+'으로 대화명을 변경하셨습니다.'};
io.sockets.in(room).emit('broadcast_msg', data);
// send changed user nickname lists to clients
io.sockets.in(room).emit('userlist',{users:Object.keys(rooms[room].socket_ids)});
});
});
答案 1 :(得分:0)
当然:,简单地说,
将套接字连接保存在app.js或server.js文件中。
只需按照代码:
var http = require('http').Server(app);
var io = require('socket.io')(http);
现在开始连接,如:
io.on('connection', function(socket){
console.log("A user connected");
}):
每当您运行服务器文件时,都会打印“已连接的用户”。表示一个客户端已连接到服务器。
断开与服务器的连接: io.on('connection',function(socket){ console.log(“用户连接”);
socket.on('disconnect',function(){
console.log("a user disconnected");
});
}):
现在您已准备好向所有连接到服务器的人发送消息:
socket.on('chat message', function(data){
socket.emit('chat message', msg);
});
如果您要向所有排除发件人发送邮件:
socket.on('chat message', function(data){
io.broadcast.emit('chat message', msg);
});
更多参考:Send response to all clients except sender (Socket.io)
我希望你对它有所了解。!!祝你好运