本文介绍了Socket.io保持反复连接,并忽略其他事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个Web应用程序,使用户可以彼此下棋.这是我在服务器上的app.js:

I'm attempting to make a webapp where users can play toroidal chess with each other. This is my app.js, on the server:

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);
});

公共目录包含index.html之类的文件,其中包含棋盘,以及一些JavaScript和CSS文件,这些文件控制棋盘并且仅允许合法移动.board.js是此文件的主要文件.

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. board.js is the main file for this.

在客户端,我的index.html文件包含以下行:

On the client-side, my index.html file contains the line:

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

我的board.js文件包含以下行:

My board.js file contains the line:

var socket = io();

以及在移动验证功能中,只要进行了有效的移动,我就有一行

and also in the move validating function, whenever a valid move is made, I have the line

socket.emit('move', moveString);

但是,每当我从终端运行节点app.js,然后在Web浏览器中访问localhost:8000时,终端都会多次多次显示客户端已连接到服务器",如下所示:

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:

终端的屏幕截图

此外,每当我从浏览器进行移动或移动到另一个页面时,服务器似乎都不会注册这些事件(它不会记录客户端已与服务器断开连接",也不会记录该事件).已采取措施).我在做什么错了?

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?

推荐答案

好,看来您正在服务器上运行socket.io v2.0.4,在客户端上运行v1.2.0.出现这样的版本号不匹配会导致您看到的行为实际上是无法正确连接,导致立即尝试重新连接,并且永远不会停止这样做.

OK, it appears you're running socket.io v2.0.4 on the server and v1.2.0 on the client. 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>

socket.io服务器具有用于/socket.io/socket.io.js 的预构建路由处理程序,当该服务器处理该事件时,它将自动从node_modules目录中提供正确的匹配客户端版本请求进来.

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.

如果希望客户端来自CDN,则需要获取与服务器版本完全匹配的CDN版本,并且每次升级服务器版本时,都必须修复CDN版本以匹配.

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.

这篇关于Socket.io保持反复连接,并忽略其他事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:34