javascript - client connecting twice to server, Socket.io -
i trying log username of logged in users connecting socket.io passport.js using passport.socketio. successful logs username twice , after trying , searching fair amount of time stuck.
the code sections follows:
the server code:
var server = http.createserver(app); var io = require('socket.io')(server); io.use(passportsocketio.authorize({ cookieparser: cookieparser, // same middleware registred in express key: 'connect.sid', // name of cookie express stores session_id secret: 'hello', // session_secret parse cookie store: new (require("connect-mongo")(esession))({ url: "mongodb://localhost/local" }), // need use sessionstore. no memorystore please success: onauthorizesuccess, // *optional* callback on success - read more below fail: onauthorizefail, // *optional* callback on fail/error - read more below })); function onauthorizesuccess(data, accept){ console.log('successful connection socket.io'); accept(null, true); accept(); } function onauthorizefail(data, message, error, accept){ if(error) throw new error(message); console.log('failed connection socket.io:', message); // use callback log of our failed connections. accept(null, false); // or // if use socket.io@1.x callback looks different // if don't want accept connection if(error) accept(new error(message)); // error sent user special error-package // see: http://socket.io/docs/client-api/#socket > error-object } io.on('connection', function(socket) { var userid = socket.request.user; console.log(userid.tg+ " has connected") }); // catch 404 , forward error handler app.use(function(req, res, next) { var err = new error('not found'); err.status = 404; next(err); }); var port = normalizeport(process.env.port || '3000'); app.set('port', port); server.listen(port);
the client code
<script src="http://code.jquery.com/jquery-1.11.1.js"></script> <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect(); </script>
the output follows:
successful connection socket.io username has connected username has connected
i unsure why outputting twice appreciated. relatively new node.js don't believe it's passport part causing it, stuck might not have best idea.
thanks in advanced
edit: tried latest version of socket , checked own version both seem latest versions, make sure updated client code to:
<script src="http://code.jquery.com/jquery-1.11.1.js"></script> <script src="https://cdn.socket.io/socket.io-1.3.2.js"></script> <script> var socket = io.connect(); </script>
the issue persists
i found problem guess own stupidity,
i accepting connection twice upon successful authorization. hence registering twice correction should be:
function onauthorizesuccess(data, accept){ console.log('successful connection socket.io'); //accept(null, true); -> remove line accept(); }
Comments
Post a Comment