简体   繁体   中英

Trying to get username in socket.io and emitting a message

Okay, So I half wat got it work the way I want. Here is my modified code:

//Route to game app
app.get('/game', function (req, res) {
  if (req.user) {
    res.render('game');

    // Socket stuff
    io.on('connection', function (socket) {

      var dataObj = {
        id: req.user.id,
        username: req.user.username,
      };
      socket.emit('startup', dataObj);
      console.log(dataObj.username);

      // Server Time
      var interval = setInterval(function () {
          var momentNow = moment();
          var data = momentNow.format('LT');
          socket.emit('time', data);
        }, 60000);

      // Chat - Needs work
      socket.on('chat', function (msg) {
        message = msg;
        io.emit('message', { message: message });
      });
    });
  } else {
    req.flash('error', 'You need to be signed in!');
    res.redirect('/');
  }
});

I had to end up doing it this way, so my user tables could be defined in the socket.io part. Now the problem is this... It says undefined client-side:

socket.on('startup', function (dataObj) {
  console.log(dataObj.username);
});

It says user is undefined, but server side it is defined, why wouldn;t it transfer over like logic would dictate?

Make change on server side

io.emit('message', {message: message, username : username }

and on the client

 socket.on('message',function(data) {
    // do something with data
       var message = data.message
       var name = data.username
 });

Try this, on the client side:

$(function(){

var username =  prompt("Enter your name");
var userObj = {username: username}

$('#chat').load('chat.ejs', function () {
  $('button#test').click(function () {
    console.log('this is a test');
  });

  $('#chat-form').submit(function (event) {
    event.preventDefault();

    var msg = $('#m').val();
    console.log(msg);
    $('#m').val('');

    if (!connected || msg == '') {
      return false;
    }

    userObj.message = msg;

    socket.emit('chat', userObj);
  });

  // Populat chat
  socket.on('message', function (data) { 

    $('#messages').append($('<li>').html(data.username + ': ' + data.message));
  });
});
})

and on the server:

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

var interval = setInterval(function () {
      var momentNow = moment();
      var data = momentNow.format('LT');
      socket.emit('time', data);
    }, 60000);

  // Chat - Needs work
  socket.on('chat', function (data) {  
    io.emit('message', data);
  });
});

Okay so I figured it out, although I feel like this is a bit weird:

  var userObj = {
    id: req.user.id,
    username: req.user.username,
  };
  var dataObj = {
    id: userObj.id,
    username: userObj.username,
  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM