在我的应用程序中使用socket.io与node.js.Receiving主应用程序文件中的客户端事件工作正常。但是在其他node.js模块中接收事件对我不起作用。任何人都可以帮助我。< / p>
客户端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
答案 0 :(得分:0)
,您必须包含模块
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文件中,则无法正常工作