私のアプリケーションでは、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