node.js - Cannot read property 'socket's of undefined when exporting socket.io -
i'm trying modularize application , emit different event client on different js file. sample code below shows event 'onlinestatus' fired led.js. keep on getting message 'type error: cannot read property 'sockets' of undefined' whenever try emit event led.js. suspect wrong when i"m trying export io /bin/www.
/bin/www
var server = http.createserver(app); var io = require('socket.io').listen(server); var connectedclientsnum = 0; io.sockets.on('connection', function(socket) { console.log("client connected!"); socket.on('disconnect', function() { console.log("client disconnected..."); console.log("total clients connected: " + --connectedclientsnum); }) }); ... module.exports = server; module.exports.io = io;
led.js
var io = require('../bin/www').io; ... function toggleled(leds, err, callback) { /* toggle led value */ if (leds[0].value == 0) { leds[0].value = 1; leds[0].save(function(err) { if (err) { err("update led error"); } else { var person= {"status": "online"}; io.sockets.emit('onlinestatus', person); callback("update led 0 1 success"); } }); } else { leds[0].value = 0; leds[0].save(function(err) { if (err) { err("update led error"); } else { var person= {"status": "offline"}; io.sockets.emit('onlinestatus', person); callback("update led 1 0 success"); } }); } }
you should check docs @ socket.io , check see if there still socket.sockets.on()
function still in socket.io framework. i'm not sure if still there. if must have working, try changing versions of socket.io 0.9, think work.
Comments
Post a Comment