我对node.js和socket.io比较新。在构建示例聊天室应用程序时,我遇到的情况是,当我刷新浏览器n次,然后发送消息时,消息将被广播n次到所有客户端。如何让它不是多次广播而只是一次?这是服务器端和客户端代码。
qaserver.js
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server, { log : false });
server.listen(4040);
var redis = require('redis');
var r43 = redis.createClient('6379', '127.0.0.1');
r43.select("43");
// Set the view directory to /views
app.set("views", __dirname + "/views");
app.use(express.static(__dirname + '/public'));
// Let's use the Jade templating language
app.set("view engine", "jade");
app.get("/", function(request, response) {
response.end("Welcome to the homepage!");
});
app.get("/qaclient", function(request, response) {
response.render("qaclient", { message: "Welcome to QA Forum" });
io.sockets.once('connection', function (socket) {
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(){
// store the username in the socket session for this client
socket.join('test_room');
socket.room='test_room';
console.log("------------- List of connected clients on adduser -------------------");
var clients = io.sockets.clients('test_room'); //
console.log(clients);
//socket.username = username;
socket.emit("welcome_fn",socket.room);
// echo to client they've connected
});
socket.on('message', function (data) {
//io.sockets.in(socket.room).emit('broadcast_message', data.name,data.message);
console.log("------------- List of connected clients on message broadcast -------------------");
var clients = io.sockets.clients('test_room'); //
console.log(clients);
socket.broadcast.to('test_room').emit('broadcast_message', data.name,data.message);
//io.sockets.emit('broadcast_message', data.name,data.message);
console.log(data.name);
console.log(data.message);
});
// when the user disconnects.. perform this
socket.on('disconnect', function(){
// remove the username from global usernames list
console.log('Socket disconnected : ');
console.log(socket.room);
socket.leave('test_room');
//console.log(socket.leave(socket.room));
});
});
});
-
qaclient.js
window.onload = function() {
var messages = [];
var field = document.getElementById("message");
sendButton = document.getElementById("sendbutton");
//var content = document.getElementById("content");
var name = document.getElementById("name");
var socket = io.connect('http://localhost:4040');
// on connection to server, ask for user's name with an anonymous callback
socket.on('connect', function(){
// call the server-side function 'adduser' and send one parameter (value of prompt)
socket.emit('adduser');
});
socket.on('welcome_fn',function(room)
{
chatcontent.innerHTML = "<b>Welcome to Chat central.. Type your message to start chatting in room "+ room +"</b>";
});
socket.on('broadcast_message', function (name,message) {
var data=new Object();
data.message=message;
data.username=name;
if(message) {
messages.push(data);
var html = '';
console.log(messages.length);
for(var i=0; i<messages.length; i++) {
html += '<b>' + messages[i].username + ': </b>';
html += messages[i].message + '<br />';
}
chatcontent.innerHTML = html;
}
//socket.emit('message', { name: name; message:field });
});
sendButton.onclick = function() {
if($.trim(field.value)=="")
{
alert("Enter the message Foo!");
}
else
{
name_tosend_tmp=name.value;
name_tosend= name_tosend_tmp.replace(/(<([^>]+)>)/ig,"");
message_tosend_tmp=field.value;
message_tosend= message_tosend_tmp.replace(/(<([^>]+)>)/ig,"");
socket.emit('message', { name: name_tosend , message:message_tosend });
var data=new Object();
data.message=message_tosend;
data.username=name_tosend;
messages.push(data);
var html = '';
for(var i=0; i<messages.length; i++) {
html += '<b>' + messages[i].username + ': </b>';
html += messages[i].message + '<br />';
}
chatcontent.innerHTML = html;
document.getElementById("name").value="";
document.getElementById("message").value="";
}
}
}