使用Node.js和Socket.io获取缺少错误处理程序套接字消息

时间:2015-07-31 04:03:35

标签: node.js socket.io

您好我收到以下错误,使用Node.js和socket.io运行服务器。

  

错误:

print "(\n    \"FOR SALE\"".match(/\"(.*)\"/)

我的服务器端代码如下所示。

  

Server.js:

Missing error handler on `socket`.
TypeError: Cannot read property 'connected' of undefined
    at Socket.<anonymous> (C:\Documents and Settings\Subhrajyoti\Desktop\Restore
\htdocs\video broadcast\server.js:68:13)
    at Socket.emit (events.js:95:17)
    at Socket.onevent (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\htd
ocs\video broadcast\node_modules\socket.io\lib\socket.js:330:8)
    at Socket.onpacket (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\ht
docs\video broadcast\node_modules\socket.io\lib\socket.js:290:12)
    at Client.ondecoded (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\h
tdocs\video broadcast\node_modules\socket.io\lib\client.js:193:14)
    at Decoder.Emitter.emit (C:\Documents and Settings\Subhrajyoti\Desktop\Resto
re\htdocs\video broadcast\node_modules\socket.io\node_modules\socket.io-parser\n
ode_modules\component-emitter\index.js:134:20)
    at Decoder.add (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\htdocs
\video broadcast\node_modules\socket.io\node_modules\socket.io-parser\index.js:2
47:12)
    at Client.ondata (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\htdo
cs\video broadcast\node_modules\socket.io\lib\client.js:175:18)
    at Socket.emit (events.js:95:17)
    at Socket.onPacket (C:\Documents and Settings\Subhrajyoti\Desktop\Restore\ht
docs\video broadcast\node_modules\socket.io\node_modules\engine.io\lib\socket.js
:99:14)

我在此行var port=8888; var express=require('express'); var morgan = require('morgan'); var http=require('http'); var bodyParser= require('body-parser'); var methodOverride = require('method-override'); var mongo = require('mongojs'); var database='Oditek'; var collections=['video']; var app= express(); var server=http.Server(app); var io=require('socket.io')(server); var db = mongo.connect("127.0.0.1:27017/"+database, collections); app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users app.use(morgan('dev')); // log every request to the console app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded app.use(bodyParser.json()) // parse application/json app.use(methodOverride()); // simulate DELETE and PUT db.on('ready', function () { console.log('database connected') }); app.get('/',function(req,res){ res.sendfile('view/login.html'); }); app.post('/login',function(req,res){ var username=req.body.username; var password=req.body.userpassword; if(username && password){ db.video.findOne({ username:username, password:password },function(err,doc){ if(doc){ console.log('login',doc); res.send(doc); } if(err){ console.log('login12',err); res.send("could not login"); } }); } }); app.get('/video',function(req,res){ res.sendfile('view/video.html'); }); //socket----programming// var roomid; var clients={}; io.sockets.on('connection',function(socket){ //console.log(socket); roomid=socket.handshake.query.roomid; var usertype=socket.handshake.query.usertype; socket.on('admin-join',function(data){ console.log('admin',data); if(data.IsJoinAdmin){ clients={ "socket":roomid } socket.join(roomid); } }); socket.on('user-join',function(data){ console.log('user',data); if(data.isUserJoin){ io.socket.connected[clients.socket].emit('user-already-joined',data); socket.join(roomid); } }); socket.on('send-broadcasting-message',function(data){ console.log('message',data) io.to(roomid).emit('sending-broadcasting',data); }); }); server.listen(port); console.log('server is listening on the port'+port); 收到错误。此行我用于向特定用户发送私信。请帮我解决此错误。

2 个答案:

答案 0 :(得分:0)

您的错误被抛出,因为io对象没有属性套接字,但它具有属性套接字。所以你应该这样写:

io.sockets.connected[clients.socket].emit('user-already-joined',data);

如果使用socket.io 1.0,则有更好的方法。每个套接字自动加入默认房间。

因此您可以使用以下代码通过id发出一个套接字:

io.to(clients.socket).emit('user-already-joined',data);

答案 1 :(得分:0)

查看server.js上的第68行并对其进行评论,您将看到您正在使用套接字范围中定义的公式。