Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to resolve TypeError: Cannot read property 'connected' of undefined #2192

Closed
subhrajyotiw5rtc opened this issue Jul 31, 2015 · 9 comments
Closed

Comments

@subhrajyotiw5rtc
Copy link

@subhrajyotiw5rtc subhrajyotiw5rtc commented Jul 31, 2015

Hi,I am getting the following error while using socket.io version 1.3.5 with node.js for my app.

error:

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)

And server file is given below.

server.js.

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.sockets.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);

Please help me to resolve this error.

@dsalcedo
Copy link

@dsalcedo dsalcedo commented Jul 31, 2015

Yor code its wrong, should be:

io.on('connection', function (socket) {
  socket.on('event', function(){

     });
});

For questions use stackoverflow, issues its for socket.io engine bugs/issues/errors.
code strong!!

@dsalcedo
Copy link

@dsalcedo dsalcedo commented Aug 5, 2015

Please close this question listed as 'issue'. For questions please use stackoverflow.
Code strong.

@mike-aungsan
Copy link

@mike-aungsan mike-aungsan commented Aug 6, 2015

@dsalcedo

I have the same problem.

I use exactly the same code from API

https://github.com/socketio/socket.io#namespaceclientsfnfunction

io.clients(function (err, clients) {
    ^

TypeError: undefined is not a function
at Namespace. (/home/test/socketio/socketio.js:64:9)
at Namespace.emit (events.js:107:17)
at Namespace.emit (/home/test/socketio/node_modules/socket.io/lib/namespace.js:205:10)
at /home/test/socketio/node_modules/socket.io/lib/namespace.js:172:14
at process._tickDomainCallback (node.js:381:11)
Program node server.js exited with code

@darrachequesne
Copy link
Member

@darrachequesne darrachequesne commented Jan 12, 2017

That issue was closed automatically. Please check if your issue is fixed with the latest release, and reopen if needed (with a fiddle reproducing the issue if possible).

@lensh
Copy link

@lensh lensh commented May 11, 2017

you should use io.to(socketid).emit('user-already-joined',data) instead of io.sockets.connected[clients.socket].emit('user-already-joined',data);
about more,you can see my blog :http://blog.csdn.net/zls986992484/article/details/71599297

@zzxBear
Copy link

@zzxBear zzxBear commented Jul 3, 2017

[Vue warn]: Error in render function: "TypeError: Cannot read property 'resolve' of undefined"

found in

--->

at D:\v-project\v-boot\src\App.vue

warn
(unknown) TypeError: Cannot read property 'resolve' of undefined
at Proxy.render (eval at (build.js:1377), :369:21)
at VueComponent.Vue._render (eval at (build.js:817), :3904:22)
at VueComponent.updateComponent (eval at (build.js:817), :2440:21)
at Watcher.get (eval at (build.js:817), :2779:25)
at new Watcher (eval at (build.js:817), :2762:12)
at mountComponent (eval at (build.js:817), :2444:17)
at VueComponent.Vue$3.$mount (eval at (build.js:817), :7572:10)
at VueComponent.Vue$3.$mount (eval at (build.js:817), :9671:16)
at init (eval at (build.js:817), :3359:13)
at createComponent (eval at (build.js:817), :4902:9)

@zzxBear
Copy link

@zzxBear zzxBear commented Jul 3, 2017

这是啥问题,求大神解答

@notedit
Copy link

@notedit notedit commented Jul 3, 2017

@zzxBear none of socketio's problom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants
@notedit @dsalcedo @mike-aungsan @subhrajyotiw5rtc @darrachequesne @lensh @zzxBear and others
You can’t perform that action at this time.