node js socket io代码是否正确但仍然出错?

时间:2018-05-02 02:58:10

标签: node.js websocket socket.io

节点js socket io代码是否正确但仍然是错误?

............................................... .................................................. ...................................

app.js

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

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

users = {};
io.on('connection', function(socket){
    console.log('Connected');
    socket.on('setUsername', function(data){
        if(users.indexOf(data) > -1)
        {
            socket.emit('userExists', data + ' is not avariable. please use other');
        }
        else
        {
            users.push(data);
            socket.emit('userSet', {username: data});
        }
    })
    socket.on('msg', function(data){
        io.sockets.emit('newmsg', data);
    })
});

http.listen(3000, function(){
    console.log('start server on port :3000');
});

............................................... .................................................. ...................................

的index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Real time App chat</title>
</head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script type="text/javascript">
var socket = io();
function setUsername(){
    socket.emit('setUsername', document.getElementByid('name').value);
};
var user;
socket.on('userExists', function(data){
    document.getElementByid('error-container').innerHTML = data;
});
socket.on('userSet', function(data){
    user = data.username;
    document.body.innerHTML = '<input type="text" id="message">\
                               <button type="button" name="button" onclick="sendMessage()">ส่ง</button>\
                               <div id="message-container"></div>';
});

function sendMessage(){
    var msg = document.getElementByid('message').value;
    if(msg)
    {
        socket.emit('msg', {message: msg, user: user});
    }
}

socket.on('newmsg', function(data){
    if(user)
    {
        document.getElementByid('message-container').innerHTML += '<div><b>' + data.user + '</b>:' + data.message + '</div>';
    }
})
</script>

<div id="error-container" ></div>
<input id="name" type="text" name="name" value="" placeholder="please set your name">
<button type="button" name="button" onclick="setUsername()">Start Chat</button>
</body>
</html>

当我按节点app.js加载时效果很好。

当我访问my-domain.com:3000时,它表现得很好。

但是,当我尝试将数据填充到inotu类型文本id="name"并按button时,我收到2错误。

enter image description here

我该怎么做才能修好它?

1 个答案:

答案 0 :(得分:1)

您的代码中出现拼写错误&#34; index.html&#34;在这个功能:

function setUsername(){
   socket.emit('setUsername', document.getElementByid('name').value);
};

应该是:

function setUsername(){
   socket.emit('setUsername',document.getElementById('name').value);
};

文档。 getElementById (&#39; name&#39;)&#39; i&#39; id应为大写