套接字IO聊天示例很慢

时间:2018-02-27 13:28:54

标签: javascript node.js socket.io

我是Node.js和Socket.IO的新手,我想尝试在

解释的例子

https://socket.io/get-started/chat/

我做了我必须做的所有事情并且工作正常:我打开了两个标签,并且两个客户端都显示了消息,但出于某种原因,它们会在5/6秒后出现(有时甚至更晚)。 你们知道为什么(我使用的是Windows 10)吗?

这是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>

这是package.json:

{
    "name": "socket-example",
    "version": "0.0.1",
    "description": "my first socket.io app",
    "dependencies": {
        "engine.io": "^3.1.5",
        "express": "^4.15.2",
        "socket.io": "^2.0.4"
    }
}

1 个答案:

答案 0 :(得分:1)

似乎是一个已知问题 - &gt;

https://github.com/socketio/socket.io/issues/3179

所以在你的index.js文件..

更改 - &gt;

var io = require('socket.io')(http);

到 - &gt;

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

这样做我现在得到即时反馈,运行Windows 10 ..