2016-09-21 34 views
0

我是新的Node.js和Socket.IO,我似乎無法弄清楚爲什麼我總是收到「undefined」,我' m試圖在它所做的消息前面得到一個用戶名,但我似乎無法擺脫那個討厭的undefined。我如何擺脫這個Node.js聊天中的「undefined」

這是我index.js

var app = require('express')(); 
 
var http = require('http').Server(app); 
 
var io = require('socket.io')(http); 
 

 

 
app.get('/', function(req, res){ 
 
    res.sendfile('index.html'); 
 
}); 
 

 
io.on('connection', function(socket){ 
 
    console.log('a user connected'); 
 
    io.emit('chat message','User Connected'); 
 
    socket.on('disconnect', function(){ 
 
    io.emit('chat message','User Disconnected'); 
 
    console.log('user disconnected'); 
 
    }); 
 
}); 
 

 
http.listen(13280, function(){ 
 
    console.log('listening on *:13280'); 
 
}); 
 

 
io.on('connection', function(socket){ 
 
    socket.on('chat message', function(msg){ 
 
    console.log('message: ' + msg); 
 
    }); 
 
}); 
 

 
io.on('connection', function(socket){ 
 
    socket.on('chat message', function(msg,username){ 
 
    io.emit('chat message',username + msg); 
 
    }); 
 
});

,在這裏我index.html

<!doctype html> 
 
<html> 
 
    <head> 
 
    <title>Socket.IO chat</title> 
 
    <style> 
 
     * { margin: 0; padding: 0; box-sizing: border-box; } 
 
     body { font: 13px Helvetica, Arial; } 
 
     form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } 
 
     form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } 
 
     form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } 
 
     .name {margin-bottom: 40px;} 
 
     #messages { list-style-type: none; margin: 0; padding: 0; } 
 
     #messages li { padding: 5px 10px; } 
 
     #messages li:nth-child(odd) { background: #eee; } 
 
    </style> 
 
    </head> 
 
<body> 
 
    <ul id="messages"></ul> 
 
    <form action=""> 
 
     <input id="m" autocomplete="off" /><button>Send</button> 
 
    </form> 
 

 
     <script src="/socket.io/socket.io.js"></script> 
 
<script src="http://code.jquery.com/jquery-1.11.1.js"></script> 
 
<script> 
 
var name = prompt("Choose a username","username"); 
 
var halveusername = "[" + name; 
 
var username = halveusername + "] "; 
 
var socket = io(); 
 

 
    $('form').submit(function(){ 
 
    socket.emit('chat message',username + $('#m').val()); 
 
    $('#m').val(''); 
 
    return false; 
 
    }); 
 

 
    socket.on('chat message', function(msg){ 
 
    $('#messages').append($('<li>').text(msg)); 
 
    }); 
 

 
</script> 
 
    </body> 
 
</html>

+1

你發送'socket.emit(」聊天消息',用戶名+ $('#m')。val());'然而你是前例pecing'socket.on('chat message',function(msg,username){'在服務器上?爲什麼連接的字符串會作爲服務器上的兩個參數返回? – adeneo

回答

0

如果包含多個值,您可能會更好地發送json對象作爲消息 - 它爲您提供了更大的靈活性,並且會在任何情況下解決您的錯誤。

試試這個

在index.js

io.on('connection', function(socket){ 
    socket.on('chat message', function(message){ 
    io.emit('chat message', message); 
    }); 
}); 

和index.html中

$('form').submit(function(){ 

    var message = { 
     username: username, 
     text: $('#m').val() 
    }; 

    socket.emit('chat message', message); 
    $('#m').val(''); 
    return false; 
}); 

socket.on('chat message', function(msg){ 
    $('#messages').append($('<li>').text(msg.username + msg.text)); 
});