我正在用socket.io测试Node.js程序,它在本地运行良好。但是,当我在AWS服务器上运行它时,当我随机刷新客户端程序10-50次时,该程序停止工作。当停止时,我什至无法在服务器端使用命令'wget http://localhost:8080/test_io.html'获取html文件。但是,当我从客户端代码中删除socket.io部分时,程序运行正常。请帮助:
server : Linux ip-xxx-xx-xx-xx 4.14.33-51.37.amzn1.x86_64
Node.js : v11.0.0-pre
socket.io@2.1.1
├── socket.io-adapter@1.1.1
├─┬ socket.io-client@2.1.1
│ ├── socket.io-parser@3.2.0 deduped
└─┬ socket.io-parser@3.2.0
服务器端代码:
'use strict';
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var fs = require('fs');
var io_num = 0, fs_num = 0;
app.listen(8080);
console.log('io');
function handler (req, res) {
console.log('fs', ++fs_num);
fs.readFile(__dirname + '/test_io.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
console.log('io', ++io_num);
});
客户端:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
setTimeout(function(){
location.href = 'test_io.html';
}, 3000);
</script>