如何在 node.js 中接收 socket.io 客户端事件?

Posted

技术标签:

【中文标题】如何在 node.js 中接收 socket.io 客户端事件?【英文标题】:How to receive the socket.io client side event in node.js? 【发布时间】:2013-03-06 15:47:58 【问题描述】:

在我的应用程序中,我使用带有 node.js 的 socket.io。在主应用程序文件中接收客户端事件工作正常。但是在其他 node.js 模块中接收事件对我不起作用。任何人都可以帮我解决这个问题。

客户端js文件

  var fullName=$("#fullName").val();
  var emailId=$("#emailId").val(); 
  var contactNo=$("#ContactNo").val();
  var msg=$("#message").val();
  var userInfo=fName:fullName,email:emailId,contactNumber:contactNo,message:msg;
  var socket = io.connect('http://localhost:8000');
  socket.emit('uploadResume',userInfo);

app.js - 服务器端主文件

  var express = require('express')
               , http = require('http');

  var app = express();
  app.configure(function()
     app.use(express.static(__dirname + '/public'));
  );
 var server = http.createServer(app);
 var io = require('socket.io').listen(server);
 server.listen(8000);

 exports.io=io;
 console.log('io object is set.');
 var mongodb = require('mongodb');
 var server = new mongodb.Server("localhost", 27017, );

 new mongodb.Db('test', server, w: 1).open(function (error, client) 


    exports.client = client;
    console.log('client object is set.');

 );

Notification.js

 console.log('Notification module is called');

var app=require('../server');
console.log('app module is ready to use.');
var ioObj=app.io;
var clientObj=app.client;

ioObj.sockets.on('connection', function (socket) 
socket.on('uploadResume', function (userInfo) 
 console.log('data is '+userInfo); 
 var collection = new mongodb.Collection(clientObj, 'test_collection');
  collection.insert(userInfo,safe:true,function(err, objects) 
    if(!err)

     console.log('Data inserted successfully.');
    
    if (err && err.message.indexOf('E11000 ') !== -1) 
      // this _id was already inserted in the database
    
  );




   );
 );

控制台消息

   info  - socket.io started
   io object is set.
   client object is set.
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized g2Sx9h5FL9Gxzs3KiAcj
   debug - setting request GET /socket.io/1/websocket/g2Sx9h5FL9Gxzs3KiAcj
   debug - set heartbeat interval for client g2Sx9h5FL9Gxzs3KiAcj
   debug - client authorized for 
   debug - websocket writing 1::
   debug - emitting heartbeat for client g2Sx9h5FL9Gxzs3KiAcj
   debug - websocket writing 2::
   debug - set heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - set heartbeat interval for client g2Sx9h5FL9Gxzs3KiAcj
   debug - emitting heartbeat for client g2Sx9h5FL9Gxzs3KiAcj
   debug - websocket writing 2::
   debug - set heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - set heartbeat interval for client g2Sx9h5FL9Gxzs3KiAcj
   debug - emitting heartbeat for client g2Sx9h5FL9Gxzs3KiAcj
   debug - websocket writing 2::
   debug - set heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - set heartbeat interval for client g2Sx9h5FL9Gxzs3KiAcj
   debug - emitting heartbeat for client g2Sx9h5FL9Gxzs3KiAcj
   debug - websocket writing 2::
   debug - set heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - set heartbeat interval for client g2Sx9h5FL9Gxzs3KiAcj
   debug - emitting heartbeat for client g2Sx9h5FL9Gxzs3KiAcj
   debug - websocket writing 2::
   debug - set heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - set heartbeat interval for client g2Sx9h5FL9Gxzs3KiAcj
   debug - emitting heartbeat for client g2Sx9h5FL9Gxzs3KiAcj
   debug - websocket writing 2::
   debug - set heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - set heartbeat interval for client g2Sx9h5FL9Gxzs3KiAcj
   debug - emitting heartbeat for client g2Sx9h5FL9Gxzs3KiAcj
   debug - websocket writing 2::
   debug - set heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client g2Sx9h5FL9Gxzs3KiAcj
   debug - set heartbeat interval for client g2Sx9h5FL9Gxzs3KiAcj

【问题讨论】:

您能否指定在服务器运行时打印哪些控制台消息? 代码似乎是正确的..请详细说明您遇到的错误或输出...... @AmolMKulkarni 请检查我的代码中是否更新了控制台消息 @shelman 请检查我的代码中是否更新了控制台消息 【参考方案1】:

在您的主应用程序文件中,您必须包含模块

app.js

  var notification=require('./Notifications.js');  // module included
  var express = require('express'),
  http = require('http');

  var app = express();
  app.configure(function()
     app.use(express.static(__dirname + '/public'));
  );
 var server = http.createServer(app);
 var io = require('socket.io').listen(server);
 server.listen(8000);

 exports.io=io;
 console.log('io object is set.');
 var mongodb = require('mongodb');
 var server = new mongodb.Server("localhost", 27017, );

 new mongodb.Db('test', server, w: 1).open(function (error, client) 


    exports.client = client;
    console.log('client object is set.');

 );

因此,如果您不将模块文件包含到主 app.js 文件中,它将无法工作

【讨论】:

以上是关于如何在 node.js 中接收 socket.io 客户端事件?的主要内容,如果未能解决你的问题,请参考以下文章

Android 客户端未从 node.js 服务器接收到 socket.io 消息

Java Socket IO不会发送到Node.js,但可以连接和接收

Node.js 如何在快速路由中使用 socket.io

从 socket.io 收到消息时从客户端播放音频 - node.js

node.js 和 socket.io。 websocket的传输类型配置?

使用集群将 Socket.IO 扩展到多个 Node.js 进程时的一些问题