te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Socket.io keeps connecting repeatedly and ignores other events - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Socket.io keeps connecting repeatedly and ignores other events - Stack Overflow

programmeradmin4浏览0评论

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

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.

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

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

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

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:

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?

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

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.

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

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

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

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:

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?

Share Improve this question asked Jan 3, 2018 at 0:22 ZanikZanik 1032 silver badges10 bronze badges 4
  • 1 The last time I saw this type of behavior, it was because there were mismatched client and server versions of socket.io. What version of socket.io are you running at client and server? How are you loading the client-side socket.io code? – jfriend00 Commented Jan 3, 2018 at 0:59
  • I know almost nothing about any of these technologies, so excuse me if I say something incredibly wrong or stupid. So – Zanik Commented Jan 3, 2018 at 4:14
  • @jfriend00 when I type "npm list socket.io" in the terminal, it says "[email protected] /Users/Ryan/Desktop/Projects/Webapps/Toroidal └── [email protected]", so I assume that means I'm running version 2.0.4 on the server side. For the client-side socket.io, I have the line <script src="cdn.socket.io/socket.io-1.2.0.js"></script>, so I'm running version 1.2.0 there. Is that the source of this problem? I used that because I couldn't figure out how to load the socket.io code – Zanik Commented Jan 3, 2018 at 4:20
  • directly from the node_modules directory. – Zanik Commented Jan 3, 2018 at 4:21
Add a ment  | 

1 Answer 1

Reset to default 17

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>

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 es in.

If you want the client to e 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.

发布评论

评论列表(0)

  1. 暂无评论