[英]node + socket.io: multiple server emits for single client emit?
I've just managed to connect to a server script with socket.io, so I'm happy about that. 我刚刚设法通过socket.io连接到服务器脚本,所以对此我感到很高兴。 I'm less happy though about the weird behavior my script generates: I send one emit to the server script on a buttonclick, and the server test script sends back a message 6x to the console log.
我对我的脚本产生的怪异行为不满意:我通过一次clickclick将一个发射发送到服务器脚本,而服务器测试脚本将消息发送回6x到控制台日志。 Googling this problem description gets ideas about spotty, repeating connections, but I don't think that's it.
谷歌搜索此问题描述可获得有关断断续续,重复连接的想法,但我认为并非如此。
Anyway, here's the client app.js: 无论如何,这是客户端app.js:
var commentapp={
init: function(){
var commentapp=this;
commentapp.btn_api=$('#btn_api');
commentapp.btn_api.click(this.get_comment_data);
},
get_comment_data: function(btn_event){
var commentapp=this;
console.log('trying to connect');
commentapp.socket=io.connect('http://localhost:8080');
commentapp.socket.on('connect', function() {
commentapp.socket.emit('btn_api_call');
}); //commentapp.socket.on 'connect',
commentapp.socket.on('serverMessage', function(content){
console.log(content);
}
); //commentapp.socket.on('serverMessage'
}
};
$(function() {
commentapp.init();
});
The server script is as follows: 服务器脚本如下:
var httpd = require("http").createServer(handler);
var io=require('/Users/user/Virtualenvs/node_modules/socket.io/lib/socket.io').listen(httpd);
var fs = require('fs');
var url = require("url");
var path = require("path");
var port = process.argv[2] || 8080;
httpd.listen(parseInt(port, 10));
function handler (request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
console.log(uri);
path.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return; //these returns get you out of the function I think
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary"); //otherwise here's where the file gets finally served
response.end();
}); //fs.readFile
}); //path.exists
io.sockets.on('connection',function(socket) {
socket.on('btn_api_call', function() {
socket.emit('serverMessage', 'Server heard you.');
});
});
};
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");
Both of these are cannibalized from https://github.com/accbel/nodejs-socketio-example and Pedro Teixeira's book. 这两个都可以从https://github.com/accbel/nodejs-socketio-example和Pedro Teixeira的书中蚕食掉。
So if I click the button to generate the 'btn_api_call'emit, the console log will say "'Server heard you.'" 6x. 因此,如果我单击该按钮以生成“ btn_api_call”发出,则控制台日志将显示“服务器听到了您的声音” 6倍。 Hopefully this is a rookie mistake easily set straight.
希望这是一个菜鸟错误,很容易就解决了。
Thanks for your help! 谢谢你的帮助!
This is likely due to having your registration for connections inside of a route handler. 这可能是由于您在路由处理程序中注册了连接。
Each time a request comes in that is handled by that route, the code is adding a new listener for connections. 每次收到该路由处理的请求时,代码都会为连接添加一个新的侦听器。
You likely have a similar problem in your client - connecting each time the button is clicked. 您的客户端中可能存在类似的问题-每次单击按钮时都进行连接。
Move your connection listener outside the route like this: 将您的连接侦听器移动到路由之外,如下所示:
function handler (request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
console.log(uri);
path.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return; //these returns get you out of the function I think
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary"); //otherwise here's where the file gets finally served
response.end();
}); //fs.readFile
}); //path.exists
};
io.sockets.on('connection',function(socket) {
socket.on('btn_api_call', function() {
socket.emit('serverMessage', 'Server heard you.');
});
});
On the client move the connect logic to init - something like: 在客户端上,将连接逻辑移至init-类似:
var commentapp={
init: function(){
var commentapp=this;
commentapp.btn_api=$('#btn_api');
commentapp.btn_api.click(this.get_comment_data);
console.log('trying to connect');
commentapp.socket=io.connect('http://localhost:8080');
commentapp.socket.on('connect', function() {
commentapp.socket.emit('btn_api_call');
}); //commentapp.socket.on 'connect',
commentapp.socket.on('serverMessage', function(content){
console.log(content);
}
); //commentapp.socket.on('serverMessage'
},
get_comment_data: function(btn_event){
var commentapp=this;
commentapp.socket.emit('btn_api_call');
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.