简体   繁体   English

socket.io函数在回调中不起作用

[英]socket.io functions not working in callbacks

The issue I am having is that I want the current session to join a room if they pass a database check. 我遇到的问题是,如果他们通过数据库检查,我希望当前会话加入会议室。 If they do pass the check, meaning the query returns a result, it should add the user to a room. 如果他们确实通过了检查,这意味着查询将返回结果,则应将用户添加到房间中。 However, when I call socket.join("room"), it does not work if its in the database callback function. 但是,当我调用socket.join(“ room”)时,如果它在数据库回调函数中不起作用。 Here is a SIMPLIFIED version of my code: 这是我的代码的简化版本:

var pg = require('pg');
var conString = 'connection_to_postgres_server'

var server = require('http').createServer();
var io = require('socket.io')(server);
var port = 8080;


function defaultOnSuccess(query, result){
  console.log("Result of query `", query, "` -> ", result);
}
function defaultOnError(query, err){
  console.log("Error of query `", query, "` -> ", err);
}
function runQuery(query, queryParams, onSuccess, onError){
  onSuccess = (typeof onSuccess !== 'undefined' ? onSuccess : defaultOnSuccess);
  onError = (typeof onError !== 'undefined' ? onError : defaultOnError);
  pg.connect(conString, function(err, client, done){
    if(err){
      onError(query, err);
      done();
      return;
    }
    client.query(query, queryParams, function(err, result){
      if(err){
        onError(query, err);
        done();
        return;
      }
      else {
        onSuccess(query, result);
        done();
        return;
      }
    });
  });
}
function listenOn(channel, onNotification, onError){
  onError = (typeof onError !== 'undefined' ? onError : defaultOnError);
  pg.connect(conString, function(err, client, done){
    if(err){
      onError(channel, err);
      done();
      return;
    }
    client.on('notification', function(msg) {
      onNotification(channel, msg);
    });
    var query = client.query("LISTEN \"" + channel + "\";");
    done();
  });
}

io.on('connection', function(socket){
  runQuery("THIS QUERY SHOULD RETURN EXACTLY ONE RESULT IF THE USER IS VALIDATED", [], 
    function(query, result){
      if(result.rowCount == 1){

        console.log("Pre rooms: ", socket.rooms);
        socket.join("hello");
        console.log("Current rooms: ", socket.rooms);
      }
    }
  );
});

io.on('connection', function(socket){
  socket.on('disconnect', function(){
  });
});

server.listen(port, function(){
  console.log('listening on *:' + port);
  listenOn("project_tc_changed", 
    function(channel, message){
      console.log(message);
      io.emit("data", message);
    }
  );
});

When I connect with a client, the output of the "Pre rooms:" and "Current rooms:" log is exactly the same. 当我与客户建立联系时,“ Pre rooms:”和“ Current rooms:”日志的输出完全相同。 In addition, the io.emit() in server.listen does not work, even though I know the code is getting called, because the message gets logged. 另外,即使我知道代码被调用,server.listen中的io.emit()也不起作用,因为消息已被记录。

I know for a fact that the socket.join() call and the io.emit() call are getting reached, they are just not having any effects, and not returning any errors. 对于一个事实,我知道达到了socket.join()调用和io.emit()调用,它们只是没有任何效果,并且没有返回任何错误。

The socket.join is working as expected, but due to the asynchronous nature of javascript your console logs are not showing what you expected. socket.join可以按预期工作,但是由于javascript的异步特性,您的控制台日志未显示您的预期。 In javascript every line of code is ran asynchronously, including your socket.join and console.log. 在javascript中,每行代码都是异步运行的,包括socket.join和console.log。 This is why we have to make use of callbacks to see what the environment looks like after a function has completed. 这就是为什么我们必须使用回调来查看函数完成后环境的样子。 socket.join allows for this callback. socket.join允许此回调。 So to see the room join in action, we simply have to change our 3 lines of code to the following: 因此,要查看会议室的加入,我们只需要将3行代码更改为以下内容:

    console.log("Pre rooms: ", socket.rooms);
    socket.join("hello", function(){
      console.log("Current rooms: ", socket.rooms);
    );

As for your emit; 至于你的发射; If you believe your emit is being reached and the message variable contains data, your io.emit should work. 如果您认为达到了发射并且message变量包含数据,则io.emit应该可以工作。 So without seeing what the client side code looks like it is hard to help solve for this. 因此,如果看不到客户端代码是什么样子,很难解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM