TypeError: io.sockets.clients is not a function

I follow the manual of this link (step 7). I have successfully installed both node.js or socket.js, but when I go to the page and try to send the image, I get this error on the server

Missing error handler on `socket`.
TypeError: io.sockets.clients is not a function
at Socket.<anonymous> (C:\Users\utente\Projects\webrtc\server.js:30:31)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Socket.onevent (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\socket.js:330:8)
at Socket.onpacket (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\socket.js:290:12)
at Client.ondecoded (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\client.js:193:14)
at 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)
at Decoder.add (C:\Users\utente\Projects\webrtc\node_modules\socket.io\node_modules\socket.io-parser\index.js:247:12)
at Client.ondata (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\client.js:175:18)
at emitOne (events.js:77:13)

my server.js file

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 to log server messages on the client
function log(){
    var array = [">>> Message from server:"];
    array.push.apply(array, arguments);
    socket.emit('log', array);
}

socket.on('message', function (message) {
    log('Client said:', message);
    // for a real app, would be room only (not broadcast)
    socket.broadcast.emit('message', message);
});

socket.on('create or join', function (room) {
    log('Request to 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 two clients
        socket.emit('full', room);
    }
});

socket.on('ipaddr', function () {
    var ifaces = os.networkInterfaces();
    for (var dev in ifaces) {
        ifaces[dev].forEach(function (details) {
            if (details.family=='IPv4' && details.address != '127.0.0.1') {
                socket.emit('ipaddr', details.address);
            }
      });
    }
   });

});

index.html

<!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?

Many thanks

with io.sockets.clients [room] .length; I solved the error, but now I get

Missing error handler on `socket`.
TypeError: Cannot read property '8a675bfe1203e' of undefined
at Socket.<anonymous> (C:\Users\utente\Projects\webrtc\server.js:30:38)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Socket.onevent (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\socket.js:330:8)
at Socket.onpacket (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\socket.js:290:12)
at Client.ondecoded (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\client.js:193:14)
at 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)
at Decoder.add (C:\Users\utente\Projects\webrtc\node_modules\socket.io\node_modules\socket.io-parser\index.js:247:12)
at Client.ondata (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\client.js:175:18)
at emitOne (events.js:77:13)
+4
source share
3 answers

From the fact that the log says that the error is displayed on line 30: var numClients = io.sockets.clients(room).length;

The reason is that the method io.sockets.clients(room);no longer works on socket.io v1.0 +

You can get a list of customers by calling:

var clientsList = io.sockets.adapter.rooms[room];
var numClients = clientsList.length;
+2
source

You can simply change the line of code to

var numClients = io.sockets.adapter.rooms[room]!=undefined ? Object.keys(io.sockets.adapter.rooms[room]).length:0;
0

The problem occurs if you update the dependencies to the latest versions:

"dependencies": {
    "mime": "^1.3.4",
    "socket.io": "^1.4.5"
}

while it works, if you use the dependencies specified in the book

"dependencies": {
   "socket.io": "~0.9.6",
    "mime": "~1.2.7"
}

I checked the example in the NodeJs book in action, and of course this will not work if you use the latest versions of socket.io, you will have the following two lines to make it work:

From:

var usersInRoom = io.sockets.clients(room);

To:

var usersInRoom = io.of('/').in(room).clients;

and from:

socket.emit('rooms', io.sockets.manager.rooms);

in

socket.emit('rooms', io.of('/').adapter.rooms);

Hello!

0
source

Source: https://habr.com/ru/post/1613372/


All Articles