node + socket.io:多个服务器为单个客户端发出?

时间:2013-08-29 21:40:03

标签: javascript node.js socket.io

我刚刚设法使用socket.io连接到服务器脚本,所以我很高兴。关于我的脚本生成的奇怪行为,我不太高兴:我在按钮上发送一个emit到服务器脚本,服务器测试脚本将6x消息发送回控制台日志。谷歌搜索这个问题描述得到关于斑点,重复连接的想法,但我认为不是这样。

无论如何,这是客户端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的书中蚕食的。

因此,如果我点击按钮生成'btn_api_call'emit,控制台日志会说“'服务器听到了你。'”6x。希望这是一个容易设置的菜鸟错误。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

这可能是因为您在路由处理程序中注册了连接。

每次进入由该路由处理的请求时,代码都会为连接添加新的侦听器。

您的客户端可能遇到类似的问题 - 每次单击按钮时都会连接。

将连接侦听器移到路径之外,如下所示:

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

};