node.js - TypeError: io.sockets.clients is not a function -
i following tutorial @ link (step 7). installed both node.js either socket.js , when go page , try send image error on server
missing error handler on `socket`. typeerror: io.sockets.clients not function @ socket.<anonymous> (c:\users\utente\projects\webrtc\server.js:30:31) @ emitone (events.js:77:13) @ socket.emit (events.js:169:7) @ socket.onevent (c:\users\utente\projects\webrtc\node_modules\socket.io\lib\socket.js:330:8) @ socket.onpacket (c:\users\utente\projects\webrtc\node_modules\socket.io\lib\socket.js:290:12) @ client.ondecoded (c:\users\utente\projects\webrtc\node_modules\socket.io\lib\client.js:193:14) @ decoder.emitter.emit (c:\users\utente\projects\webrtc\node_modules\socket.io\node_modules\socket.io-parser\node_modules\component-emitter\index.js:134:20) @ decoder.add (c:\users\utente\projects\webrtc\node_modules\socket.io\node_modules\socket.io-parser\index.js:247:12) @ client.ondata (c:\users\utente\projects\webrtc\node_modules\socket.io\lib\client.js:175:18) @ emitone (events.js:77:13)
my server.js file is
var os = require('os'); var static = require('node-static'); var http = require('http'); var socketio = require('socket.io'); var fileserver = new(static.server)(); var app = http.createserver(function (req, res) { fileserver.serve(req, res); }).listen(2013); var io = socketio.listen(app); io.sockets.on('connection', function (socket){ // convenience function log server messages on client function log(){ var array = [">>> message server:"]; array.push.apply(array, arguments); socket.emit('log', array); } socket.on('message', function (message) { log('client said:', message); // real app, room (not broadcast) socket.broadcast.emit('message', message); }); socket.on('create or join', function (room) { log('request create or join room ' + room); var numclients = io.sockets.clients(room).length; log('room ' + room + ' has ' + numclients + ' client(s)'); if (numclients === 0){ socket.join(room); socket.emit('created', room, socket.id); } else if (numclients === 1) { socket.join(room); socket.emit('joined', room, socket.id); io.sockets.in(room).emit('ready'); } else { // max 2 clients socket.emit('full', room); } }); socket.on('ipaddr', function () { var ifaces = os.networkinterfaces(); (var dev in ifaces) { ifaces[dev].foreach(function (details) { if (details.family=='ipv4' && details.address != '127.0.0.1') { socket.emit('ipaddr', details.address); } }); } }); });
the index.html is
<!doctype html> <html> <head> <meta name="keywords" content="javascript, webrtc" /> <meta name="description" content="webrtc codelab" /> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1"> <title>webrtc codelab: step x</title> <link rel="stylesheet" href="css/main.css"> </head> <body> <h1>webrtc codelab: step x</h1> <p> room url: <br> <code id="url">...</code> </p> <video class="camera" autoplay></video> <div class="controls"> <canvas id="photo" style="width: 200px; height: 150px; border: 1px solid #ccc;"></canvas> <p> <button id="snap">snap</button>-then-<button id="send">send</button> <br> - or - <br> <button id="snapandsend">snap & send</button> </p> </div> <div class="incoming"> <h2>incoming photos</h2> <div id="trail"></div> </div> <script src="node_modules\socket.io\node_modules\socket.io-client\socket.io.js"></script> <script src="js/lib/adapter.js"></script> <script src="js/main.js"></script> </body>
any help?
thanks lot
with io.sockets.clients[room].length; solved error get
missing error handler on `socket`. typeerror: cannot read property '8a675bfe1203e' of undefined @ socket.<anonymous> (c:\users\utente\projects\webrtc\server.js:30:38) @ emitone (events.js:77:13) @ socket.emit (events.js:169:7) @ socket.onevent (c:\users\utente\projects\webrtc\node_modules\socket.io\lib\socket.js:330:8) @ socket.onpacket (c:\users\utente\projects\webrtc\node_modules\socket.io\lib\socket.js:290:12) @ client.ondecoded (c:\users\utente\projects\webrtc\node_modules\socket.io\lib\client.js:193:14) @ decoder.emitter.emit (c:\users\utente\projects\webrtc\node_modules\socket.io\node_modules\socket.io-parser\node_modules\component-emitter\index.js:134:20) @ decoder.add (c:\users\utente\projects\webrtc\node_modules\socket.io\node_modules\socket.io-parser\index.js:247:12) @ client.ondata (c:\users\utente\projects\webrtc\node_modules\socket.io\lib\client.js:175:18) @ emitone (events.js:77:13)
from log says error thrown @ line 30: var numclients = io.sockets.clients(room).length;
the reason method io.sockets.clients(room);
not longer work on socket.io v1.0+
you can obtain list of clients calling:
var clientslist = io.sockets.adapter.rooms[room]; var numclients = clientslist.length;
Comments
Post a Comment