在node.js和socket.io中显示用户消息旁边的用户名

时间:2016-10-28 21:57:29

标签: javascript node.js socket.io

所以我有一个内置于node.js和socket.io here的聊天室我可以看到旁边的用户名加入消息并留下消息但不是旁边他们自己发送的消息是我的服务器代码

io.on('connection', function (socket) {
socket.on('adduser', function(username){
socket.username = username;
socket.room = 'General-Chat';
usernames[username] = username;
socket.join('General-Chat');
socket.broadcast.to('General-Chat').emit('updatechat', '<b   class="servertxt">SERVER: </b>', socket.username + ' has connected to this room' + '<hr>');
 socket.emit('welcomeuser', '<b class="servertxt">SERVER:</b> Welcome ',     socket.username + '<hr>');
 socket.emit('updaterooms', rooms, 'room1');
 });
 socket.on('sendchat', function (data) {
  // we tell the client to execute 'updatechat' with 2 parameters
  io.sockets.in(socket.room).emit('updatechat', socket.username, data);
 });
 socket.on('switchRoom', function(newroom){
 socket.leave(socket.room);
 socket.join(newroom);
 socket.emit('updatechat', '<b class="servertxt"> SERVER </b>', 'you have      connected to '+ newroom + '<hr>');
 // sent message to OLD room
 socket.broadcast.to(socket.room).emit('updatechat', '<b class="servertxt">    SERVER </b>', socket.username+' has left this room <hr>');
 // update socket session room title
 socket.room = newroom;
 socket.broadcast.to(newroom).emit('updatechat', '<b class="servertxt"> SERVER </b>', socket.username+' has joined this room <hr>');
 socket.emit('updaterooms', rooms, newroom);
 });
 console.log('a user connected');
 socket.on('disconnect', function () {
 console.log('user disconected');
 });
 socket.on('chat-message', function (message) {
 console.log('message:', socket.username+': ' + message.text);
 //excludes "socket" from getting the emit
 socket.broadcast.to(socket.room).emit("chat-message",message);
 });
 });

这是我的index.html脚本

    <script>
    var socket = io();

    socket.on('connect', function(){
        socket.emit('adduser', prompt("What's your name?"));
    });
    socket.on('updatechat', function (username, data) {
        $('#chatlog-display-div').append(username + data);
    });
    socket.on('welcomeuser', function(data, username){
        jQuery("#chatlog-display-div").append(data + username);
    });
    socket.on('updaterooms', function(rooms, current_room) {
        $('#rooms').empty();
        $.each(rooms, function(key, value) {
            if(value == current_room){
                $('#rooms').append('<div id="chatroom-box"><center>' + value + '<center></div><hr>');
            }
            else {
                $('#rooms').append('<div id="chatroom-box"><center><a id="chatroom-box-link" href="#" onclick="switchRoom(\''+value+'\')">' + value + '</a></center></div><hr>');
            }
        });
    });
    function switchRoom(room){
        socket.emit('switchRoom', room);
    }

     $('form').submit(function(e) {
     e.preventDefault();

           //gets the value from the message text feild and sets it as the    message var
     var message = {
            text : $('#chat-box-div-txtinpt').val()
            }
        if (message.text.trim().length !== 0) {
                        socket.emit('chat-message',message);
                //append the message to the chatlog-display-div
                $('#chat-box-div-txtinpt').focus().val('');
                jQuery("#chatlog-display-div").append('<div>'+message.text+'</div><hr>');
            }
                        });
           socket.on('chat-message', function (message) {
           jQuery("#chatlog-display-div").append('<div>'+message.text+'</div><hr>');
           });

    </script>

0 个答案:

没有答案