我是nodejs和socket io的新手,但我真的想了解它是如何工作的。我已经完成了#34;聊天教程"来自socket io网站。但是,我试图做一些事情" new"这里。每次建立新连接时,我都想通过附加一个带有随机数的新选项卡向所有客户端广播一个事件。然而,唯一发生的事情是,它会附加一次(在我自己的连接上)但对其他人没有任何意义吗?这是不可能的,还是我只是做错了?
服务器,
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use("/assets/stylesheets", express.static(__dirname + '/assets/stylesheets'));
app.use("/assets/javascripts", express.static(__dirname + '/assets/javascripts'));
app.use("/assets/demo/avatars", express.static(__dirname + '/assets/demo/avatars'));
app.use("/assets/fonts/font-awesome/", express.static(__dirname + '/assets/fonts/font-awesome/'));
app.get('/dashboard', function(req, res){
res.sendFile(__dirname + '/dashboard.html');
});
io.on('connection', function(socket) {
socket.on('new user', function(username) {
socket.emit('new connection', { nickname: username });
console.log('new connection ' + username);
});
});
http.listen(3000,function() {
console.log('Listening *:3000');
});
客户端,
<script type="text/javascript">
var randomNumber = Math.floor((Math.random() * 10) + 1);
var socket = io();
socket.on('connect', function() {
socket.emit('new user', randomNumber);
});
socket.on('new connection', function(data) {
var nickname = data.nickname;
$('<li class="active"> <a href="#' + nickname + '" data-toggle="tab">' + nickname + '<span class="label label-success">12</span></a> </li>').appendTo("#uidemo-tabs-default-demo");
});
</script>
答案 0 :(得分:0)
您必须使用socket.broadcast.emit
广播..
socket.on('new user', function(username) {
socket.broadcast.emit('new connection', { nickname: username });
console.log('new connection ' + username);
});
但是,这不会将消息发送到您自己的连接,而是发送到所有其他连接。如果您想将消息发送给包括您自己在内的所有连接,
socket.on('new user', function(username) {
socket.emit('new connection', { nickname: username });
socket.broadcast.emit('new connection', { nickname: username });
console.log('new connection ' + username);
});
答案 1 :(得分:0)
您需要保留服务器上所有打开连接的列表,然后在发生新连接时,将消息发送到所有其他连接。
这是修改后的代码
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var connections = [];
app.use("/assets/stylesheets", express.static(__dirname + '/assets/stylesheets'));
app.use("/assets/javascripts", express.static(__dirname + '/assets/javascripts'));
app.use("/assets/demo/avatars", express.static(__dirname + '/assets/demo/avatars'));
app.use("/assets/fonts/font-awesome/", express.static(__dirname + '/assets/fonts/font-awesome/'));
app.get('/dashboard', function(req, res){
res.sendFile(__dirname + '/dashboard.html');
});
io.on('connection', function(socket) {
socket.on('new user', function(username) {
connections.forEach(function (socket) {
try {
socket.emit('new connection', { nickname: username });
} catch (err) {
console.log('connection probably closed: ', err);
}
});
console.log('new connection ' + username);
connections.push(socket);
});
});
http.listen(3000,function() {
console.log('Listening *:3000');
});
但是,请注意,这仅在Node.js服务器只有一个实例(即您没有使用cluster
模块)时才有效,需要更多工作才能使其在集群中工作如果它必须在冗余基础设施中工作,甚至更多。