我正在关注此link的教程(第7步)。 我成功地将node.js安装了socket.js,但是当我转到页面并尝试发送图像时,我在服务器上收到此错误
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)
我的server.js文件是
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>
任何帮助?
非常感谢
with io.sockets.clients [room] .length;我解决了错误,但现在我得到了
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)
答案 0 :(得分:2)
从日志中说出的错误是在第30行引发的:
x2 \in [1,4]
原因是方法var numClients = io.sockets.clients(room).length;
不再适用于socket.io v1.0 +
您可以致电:
获取客户列表io.sockets.clients(room);
答案 1 :(得分:0)
您只需将代码行更改为
即可var numClients = io.sockets.adapter.rooms[room]!=undefined ? Object.keys(io.sockets.adapter.rooms[room]).length:0;
答案 2 :(得分:0)
如果您将依赖项更新为最新版本,则会出现此问题:
"dependencies": {
"mime": "^1.3.4",
"socket.io": "^1.4.5"
}
虽然它可以使用书中指定的依赖项
"dependencies": {
"socket.io": "~0.9.6",
"mime": "~1.2.7"
}
我查看了NodeJs一书中的示例,当然如果你使用最新版本的socket.io它将不起作用,你将有以下两行来使它工作:
自:
var usersInRoom = io.sockets.clients(room);
要:
var usersInRoom = io.of('/').in(room).clients;
和来自:
socket.emit('rooms', io.sockets.manager.rooms);
为:
socket.emit('rooms', io.of('/').adapter.rooms);
问候!