javascript - client connecting twice to server, Socket.io - Stack Overflow

I am trying to log username of logged in users by connecting socket.io to passport.js using passport.so

I am trying to log username of logged in users by connecting socket.io to passport.js using passport.socketio. It is successful however it logs the username twice and after trying and searching for a fair amount of time I am stuck.

The code sections are as follows:

The Server Code:

var server = http.createServer(app);
var io = require('socket.io')(server);

io.use(passportSocketIo.authorize({
  cookieParser: cookieParser,       // the same middleware you registred in express
  key:          'connect.sid',       // the name of the cookie where express stores its session_id
  secret:       'hello',    // the session_secret to parse the cookie
  store:        new (require("connect-mongo")(Esession))({
    url: "mongodb://localhost/local"
  }),        // we NEED to use a 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 to socket.io');
  accept(null, true);
  accept();
}

function onAuthorizeFail(data, message, error, accept){
  if(error)
    throw new Error(message);
  console.log('failed connection to socket.io:', message);

  // We use this callback to log all of our failed connections.
  accept(null, false);

  // OR

  // If you use [email protected] the callback looks different
  // If you don't want to accept the connection
  if(error)
    accept(new Error(message));
  // this error will be sent to the user as a special error-package
  // see:  > error-object
}
io.on('connection', function(socket) {
  var userID = socket.request.user;
  console.log(userID.tg+ " has connected")

});

// catch 404 and forward to 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=".11.1.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect();
</script>

The output is as follows:

successful connection to socket.io
UserName has connected
UserName has connected

I am unsure as to why it is outputting twice any help would be much appreciated. I am relatively new to Node.js but I don't believe it's the passport part that is causing it, however I am stuck so I might not have the best idea.

Thanks in Advanced

EDIT: Tried the latest version of socket and checked my own version both seem to be latest versions, Just to make sure I updated the client code to:

<script src=".11.1.js"></script>
<script src=".io-1.3.2.js"></script>
<script>
    var socket = io.connect();
</script>

the issue persists

I am trying to log username of logged in users by connecting socket.io to passport.js using passport.socketio. It is successful however it logs the username twice and after trying and searching for a fair amount of time I am stuck.

The code sections are as follows:

The Server Code:

var server = http.createServer(app);
var io = require('socket.io')(server);

io.use(passportSocketIo.authorize({
  cookieParser: cookieParser,       // the same middleware you registred in express
  key:          'connect.sid',       // the name of the cookie where express stores its session_id
  secret:       'hello',    // the session_secret to parse the cookie
  store:        new (require("connect-mongo")(Esession))({
    url: "mongodb://localhost/local"
  }),        // we NEED to use a 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 to socket.io');
  accept(null, true);
  accept();
}

function onAuthorizeFail(data, message, error, accept){
  if(error)
    throw new Error(message);
  console.log('failed connection to socket.io:', message);

  // We use this callback to log all of our failed connections.
  accept(null, false);

  // OR

  // If you use [email protected] the callback looks different
  // If you don't want to accept the connection
  if(error)
    accept(new Error(message));
  // this error will be sent to the user as a 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 and forward to 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./jquery-1.11.1.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect();
</script>

The output is as follows:

successful connection to socket.io
UserName has connected
UserName has connected

I am unsure as to why it is outputting twice any help would be much appreciated. I am relatively new to Node.js but I don't believe it's the passport part that is causing it, however I am stuck so I might not have the best idea.

Thanks in Advanced

EDIT: Tried the latest version of socket and checked my own version both seem to be latest versions, Just to make sure I updated the client code to:

<script src="http://code.jquery./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

Share Improve this question edited Jun 25, 2015 at 13:14 zecuria asked Jun 25, 2015 at 12:32 zecuriazecuria 7884 silver badges10 bronze badges 1
  • cdn.socket.io/socket.io-1.3.5.js – Breedly Commented Jun 25, 2015 at 15:28
Add a ment  | 

2 Answers 2

Reset to default 4

I found the problem which I guess was my own stupidity,

I was accepting the connection twice upon successful authorization. Hence it was registering everything twice the correction should be:

function onAuthorizeSuccess(data, accept){
  console.log('successful connection to socket.io');
  //accept(null, true); -> remove this line
  accept();
}

This apparently was an error in previous version. You probably just need to upgrade. Check out the issue below. https://github./Automattic/socket.io/issues/350

In the future, if you're trying to debug an issue like this it's useful to be acquainted with the Node Events Class Documentation. io is just an event listener, and what's happening here is for SOME reason the connection event is doing double duty.

It's probably some Socket.io code that is firing emit('connection') one too many times.

When I'm debugging stuff like this, calling emitter.listeners('event') can be useful because it will tell me who, and more importantly how many listeners are listening.

So in your case, io.listeners('connection'); will give you an array of everyone who's listening to that event. Obviously, io is registering it twice, but you can probably find out where with this trick.

Also, going into the package itself and grepping for emit('connection'); will show you just where that nasty bug is.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744343554a4569547.html

相关推荐

  • javascript - client connecting twice to server, Socket.io - Stack Overflow

    I am trying to log username of logged in users by connecting socket.io to passport.js using passport.so

    7天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信