简体   繁体   English

Socket.io反复连接并忽略其他事件

[英]Socket.io keeps connecting repeatedly and ignores other events

I'm attempting to make a webapp where users can play toroidal chess with each other. 我正在尝试制作一个用户可以互相玩象棋的webapp。 This is my app.js, on the server: 这是我的app.js,在服务器上:

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

app.use(express.static('public'));
app.get('/', function(req, res) {
  res.sendFile(__dirname + '/public/index.html');
  console.log("Got request for homepage");
});

io.on('connection', function(socket){
   console.log('A client has connected to the server');
   socket.on('move', function(moveString){
     console.log("Move made: " + moveString);
   });
  socket.on('disconnect', function(){
    console.log('A client has disconnected from the server');
  });
});

http.listen(port, function() {
    console.log('listening on *: ' + port);
});

The public directory contains files like index.html which contains the chessboard, as well as some javascript and CSS files that control the board and only allow legal moves. 公共目录包含像index.html这样的文件,其中包含棋盘,以及一些控制棋盘的javascript和CSS文件,只允许合法移动。 board.js is the main file for this. board.js是这个的主要文件。

On the client-side, my index.html file contains the line: 在客户端,我的index.html文件包含以下行:

<script src="js/board.js"></script>

My board.js file contains the line: 我的board.js文件包含以下行:

var socket = io();

and also in the move validating function, whenever a valid move is made, I have the line 并且在移动验证功能中,每当进行有效移动时,我都有线

socket.emit('move', moveString);

However, whenever I run node app.js from the terminal and then visit localhost:8000 in a web browser, the terminal repeatedly displays that "A client has connected to the server", multiple times per second, like this: 但是,每当我从终端运行节点app.js然后在Web浏览器中访问localhost:8000时,终端会反复显示“客户端已连接到服务器”,每秒多次,如下所示:

Screenshot of terminal 终端截图

Also, whenever I make a move or move to another page from the browser, the server doesn't seem to register those events (it doesn't log "A client has disconnected from the server", nor does it log that a move has been made). 此外,每当我从浏览器移动或移动到另一个页面时,服务器似乎都没有注册这些事件(它没有记录“客户端已与服务器断开连接”,也没有记录移动有已被制作)。 What am I doing wrong? 我究竟做错了什么?

OK, it appears you're running socket.io v2.0.4 on the server and v1.2.0 on the client. 好的,看来你在服务器上运行socket.io v2.0.4,在客户端运行v1.2.0。 Having mismatched version numbers like that can cause the behavior you see which is actually a failure to connect properly which causes an immediate attempt to reconnect and it just never stops doing that. 如果版本号不匹配,可能会导致您看到的行为实际上无法正常连接,导致立即尝试重新连接,并且它永远不会停止这样做。

You can get the correct version directly from the server by using this in the client: 您可以在客户端中使用以下命令直接从服务器获取正确的版本:

<script src="/socket.io/socket.io.js"></script>

The socket.io server has a pre-built route handler for /socket.io/socket.io.js and it will automatically serve the right matching client version from within the node_modules directory when that request comes in. socket.io服务器有一个预先构建的/socket.io/socket.io.js路由处理程序,当该请求进入时,它将自动从node_modules目录中提供正确匹配的客户端版本。

If you want the client to come from a CDN, then you need to get the CDN version that exactly matches the server version and anytime you upgrade the server version, you have to fix the CDN version to match. 如果您希望客户端来自CDN,那么您需要获得与服务器版本完全匹配的CDN版本,并且只要您升级服务器版本,就必须修复CDN版本以匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM