我有一个来自socket.io官方网站的基本节点快递/套接字示例应用程序(链接粘贴在下面),我注意到当用户连接时,'连接'甚至只是一遍又一遍地开火。
所以我发现一个帖子(Socket IO chat repeating user connected)说socket.io 2.xx中有一个错误,当我恢复到socket.io 1.3.7这个问题确实停止了,这是一个确认的bug,我们应该回到1.xx版本?或者是否有一种使用socket.io的新方法,它没有反映当前的文档?
我的服务器代码:
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');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
我的客户端代码:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
我的服务器终端输出(只是永远重复):
a user connected
a user connected
a user connected
...
Socket.io官方示例: https://socket.io/get-started/chat/
答案 0 :(得分:1)
由于socket.io发布了2.x版本,如果您在客户端和服务器上的socket.io代码版本不匹配,似乎存在导致无限重复连接,断开连接,断开连接等问题的问题。
客户端缓存可能会进一步加剧这种情况,有时候浏览器客户端会遇到旧版本。
通常的解决方法是确保使用 <script>
// Get the template HTML and remove it from the doument
var previewNode = document.querySelector("#template");
previewNode.id = "";
var previewTemplate = previewNode.parentNode.innerHTML;
previewNode.parentNode.removeChild(previewNode);
var myDropzone = new Dropzone(document.body, { // Make the whole body a dropzone
url: "/admin/boats/{{ $boat->id}}/photos", // Set the url
thumbnailWidth: 80,
thumbnailHeight: 80,
parallelUploads: 20,
previewTemplate: previewTemplate,
autoQueue: false, // Make sure the files aren't queued until manually
added
previewsContainer: "#previews", // Define the container to display the
previews
clickable: ".fileinput-button" // Define the element that should be used
as click trigger to select files.
});
myDropzone.on("addedfile", function(file) {
// Hookup the start button
file.previewElement.querySelector(".start").onclick = function() {
myDropzone.enqueueFile(file); };
});
// Update the total progress bar
myDropzone.on("totaluploadprogress", function(progress) {
document.querySelector("#total-progress .progress-bar").style.width = progress + "%";
});
myDropzone.on("sending", function(file) {
// Show the total progress bar when upload starts
document.querySelector("#total-progress").style.opacity = "1";
// And disable the start button
file.previewElement.querySelector(".start").setAttribute("disabled", "disabled");
});
// Hide the total progress bar when nothing's uploading anymore
myDropzone.on("queuecomplete", function(progress) {
document.querySelector("#total-progress").style.opacity = "0";
});
// Setup the buttons for all transfers
// The "add files" button doesn't need to be setup because the config
// `clickable` has already been specified.
document.querySelector("#actions .start").onclick = function() {
myDropzone.enqueueFiles(myDropzone.getFilesWithStatus(Dropzone.ADDED));
};
document.querySelector("#actions .cancel").onclick = function() {
myDropzone.removeAllFiles(true);
};
</script>
获取socket.io客户端,因为socket.io服务器会发送与其完全匹配的客户端版本。但是,你似乎已经这样做了,所以我猜你有某种浏览器缓存问题。您可以手动清除浏览器缓存以查看是否存在此问题。
答案 1 :(得分:-1)
将此用于服务器:
var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static('node_modules')); // serve static files
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
这在HTML中:
<!DOCTYPE html>
<html lang="en">
<body>
<script src="socket.io-client/dist/socket.io.js"></script>
<script>
var socket = io();
</script>
</body>
</html>
该错误可能与Socket IO提供的<script src="/socket.io/socket.io.js"></script>
标记有关。