2016-09-21 9 views
0

私はNode.jsとSocket.IOで新しく、なぜ私が "未定義"を得ているのか分かりません。それはメッセージの前でユーザー名を取得しようとしていますが、私はそれを取り除くように見えます。ここでこの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をお試しくださいチャットメッセージ '、username + $('#m ')。val()); 'サーバー上で 'socket.on( 'chat message'、function(msg、username){'を投げますか?連結された文字列がサーバー上で2つの引数として返されるのはなぜですか? – 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)); 
}); 
関連する問題