简体   繁体   English

套接字io无法正确发送

[英]socket io not sending properly

I am trying to test if my sockets are sending properly from server to the client by using console.log to check if the data sent was correct. 我正在尝试使用console.log检查我的套接字是否从服务器正确发送到客户端,以检查发送的数据是否正确。 Nothing is ever outputted. 什么也没有输出。

What I am currently doing on the server side: 我目前在服务器端执行的操作:

connection.query(a_id, function(err, results, fields){
   if(err) throw err;

   var tutors = [];

   for(j in results){
      tutors.push(results[j]);
   }
   db.push({tutors: tutors});
  //console.log(JSON.stringify(db));

  io.on('connection', function(socket){
    socket.emit('new', "hello");
  }); 
});

Than on the client side I am doing 比起客户端,我正在做

io.on('connection', function(socket){
  console.log("connected");
  socket.on('new', function(data){
    console.log(data);
  });
});

I am not sure why data is not being printed maybe I dont fully understand sockets? 我不确定为什么不打印数据,也许我不完全了解套接字? Any insight would be appreciated. 任何见识将不胜感激。

Init your connection outside of query. 在查询之外初始化您的连接。 Here is example how to use io.on('connection') 是如何使用io.on('connection')示例

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

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