简体   繁体   English

我如何摆脱此Node.js聊天中的“未定义”

[英]How can I get rid of the “undefined” in this Node.js chat

I'm new with Node.js and Socket.IO and I can't seem to figure out why I keep getting "undefined", I'm trying to get a username in front of the message which it does, but I cant seem to get rid of that nasty undefined. 我是Node.js和Socket.IO的新手,我似乎无法弄清为什么我总是变得“未定义”,我正在尝试在它前面的消息前获取一个用户名,但我似乎无法摆脱那令人讨厌的不确定性。

Here is my index.js : 这是我的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); }); }); 

and here my index.html 这是我的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> 

You are probably better off sending a json object as the message if it contains multiple values - it gives you more flexibility and will fix your error in any case. 如果它包含多个值,则最好发送一个json对象作为消息-它可以为您提供更大的灵活性,并且可以在任何情况下解决您的错误。

Try this 尝试这个

in index.js 在index.js中

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

and in index.html 并在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));
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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