Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Socket IO聊天示例速度较慢_Javascript_Node.js_Socket.io - Fatal编程技术网

Javascript Socket IO聊天示例速度较慢

Javascript Socket IO聊天示例速度较慢,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我不熟悉Node.js和Socket.IO,我想尝试一下 我做了我必须做的一切,它工作了:我打开了两个选项卡,消息出现在两个客户端中,但出于某种原因,它们出现在5/6秒后(有时甚至更晚)。 你们知道为什么吗(我用的是Windows10) 这是index.js文件代码: var app = require('express')(); var http = require('http').createServer(app); var io = require('socket.io')(http);

我不熟悉Node.js和Socket.IO,我想尝试一下

我做了我必须做的一切,它工作了:我打开了两个选项卡,消息出现在两个客户端中,但出于某种原因,它们出现在5/6秒后(有时甚至更晚)。 你们知道为什么吗(我用的是Windows10)

这是index.js文件代码:

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;

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

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

http.listen(port, function(){
  console.log('listening on *:' + port);
});
这是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;
        }

        #messages {
            list-style-type: none;
            margin: 0;
            padding: 0;
        }

        #messages li {
            padding: 5px 10px;
        }

        #messages li:nth-child(odd) {
            background: #eee;
        }

        #messages {
            margin-bottom: 40px
        }
    </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="https://code.jquery.com/jquery-1.11.1.js"></script>

    <script>
        var socket = io();

        $(function () {
            $('form').submit(function () {
                socket.emit('chat message', $('#m').val());
                $('#m').val('');
                return false;
            });
            socket.on('chat message', function (msg) {
                $('#messages').append($('<li>').text(msg));
                window.scrollTo(0, document.body.scrollHeight);
            });
        });
    </script>
</body>

</html>

似乎是一个已知的问题->

因此,在index.js文件中

更改->

var io = require('socket.io')(http);
至->

通过运行windows 10,我现在可以获得即时反馈

var io = require('socket.io')(http);
var io = require('socket.io')(http, { wsEngine: 'ws' });