节点+ socket.io:多个服务器发射单个客户端发射?

我刚刚设法连接到一个服务器脚本与socket.io,所以我很高兴。 尽pipe我的脚本产生了奇怪的行为,但我还是不太高兴:我发送一个发送给服务器脚本的button,服务器testing脚本将6x消息发送回控制台日志。 谷歌search这个问题的描述获得点子,重复连接的想法,但我不认为就是这样。

无论如何,这是客户端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(); }); 

服务器脚本如下所示:

 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"); 

这两个都是从https://github.com/accbel/nodejs-socketio-example和Pedro Teixeira的书中去掉的。

所以,如果我点击button来生成'btn_api_call'发送,控制台日志会说''服务器听到你''。 希望这是一个容易设定的新秀错误。

谢谢你的帮助!

这很可能是由于在路由处理程序中注册了连接。

每次有一个请求到达时,代码就会为这个连接添加一个新的监听器。

您的客户端可能有类似的问题 – 每次单击button时连接。

将您的连接监听器移动到路由之外,如下所示:

 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.'); }); }); 

在客户端移动连接逻辑到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'); } };