gpt4 book ai didi

javascript - 我正在使用 socket.io 但客户端和服务器之间没有通信

转载 作者:行者123 更新时间:2023-12-02 23:50:55 24 4
gpt4 key购买 nike

我正在使用 Node js 和 socket.io 开发一款多人国际象棋游戏。当我在 localhost:8080 上运行游戏时,游戏在 index.html 文件中显示良好,并且在服务器上运行良好。然而,即使我做的一切都是正确的,国际象棋的走法也不会提交给其他玩家。少了什么东西?我已经正确安装了 socket.io

我尝试一遍又一遍地重新观看 YouTube 上的教程来发现我的错误,但我做不到。我相应地做了一切。我肯定错过了什么。这是链接https://www.youtube.com/watch?v=Isfqigjo7fQ&t=1621s

编辑

这里是构建多人国际象棋游戏教程的链接 http://dwcares.com/2015/10/21/realchess/

<!DOCTYPE html>
<html>
<head>
<title>Chess</title>
<link rel="stylesheet" href="chessboard-0.3.0.min.css">
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="chessboard-0.3.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js"></script>
<script src ="lib/socket.io-1.2.0.js"></script>
</head>
<body>
<div id="board" style="width: 400px"></div>


<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
var board = ChessBoard('board', 'start');


var board,
game = new Chess();

var removeGreySquares = function() {
$('#board .square-55d63').css('background', '');
};

var greySquare = function(square) {
var squareEl = $('#board .square-' + square);

var background = '#a9a9a9';
if (squareEl.hasClass('black-3c85d') === true) {
background = '#696969';
}

squareEl.css('background', background);
};

var onDragStart = function(source, piece) {
// do not pick up pieces if the game is over
// or if it's not that side's turn
if (game.game_over() === true ||
(game.turn() === 'w' && piece.search(/^b/) !== -1) ||
(game.turn() === 'b' && piece.search(/^w/) !== -1)) {
return false;
}
};

var onDrop = function(source, target) {
removeGreySquares();

// see if the move is legal
var move = game.move({
from: source,
to: target,
promotion: 'q' // NOTE: always promote to a queen for example simplicity
});

// illegal move
if (move === null) return 'snapback';
};

var onMouseoverSquare = function(square, piece) {
// get list of possible moves for this square
var moves = game.moves({
square: square,
verbose: true
});

// exit if there are no moves available for this square
if (moves.length === 0) return;

// highlight the square they moused over
greySquare(square);

// highlight the possible squares for this piece
for (var i = 0; i < moves.length; i++) {
greySquare(moves[i].to);
}
};

var onMouseoutSquare = function(square, piece) {
removeGreySquares();
};

var onSnapEnd = function() {
board.position(game.fen());
};

var cfg = {
draggable: true,
position: 'start',
onDragStart: onDragStart,
onDrop: onDrop,
onMouseoutSquare: onMouseoutSquare,
onMouseoverSquare: onMouseoverSquare,
onSnapEnd: onSnapEnd
};
board = ChessBoard('board', cfg);

var socket = io();
msgButton.onclick = function(e) {
socket.emit("message", "hello boi");
}



var handleMove = function(source, target) {
var move = game.move({from: source, to: target});

if (move === null) return "snapback";
else socket.emit("move", move);
};



socket.on("move", function(msg) {
game.move(msg);
board.position(game.fen());
});













</script>
</body>
</html>

下面是我的服务器文件

var express = require('express');
var app = express();
app.use(express.static("[ublic]"));
var http = require("http").Server(app);
var path = require('path');
var public = path.join(__dirname, 'public');
var io = require("socket.io")(http);


// viewed at http://localhost:8080
app.get('/', function(req, res) {
res.sendFile(path.join(public, 'index.html'));
});

io.on('connection', function(socket){
console.log('a user has connected');

socket.on("message", function(msg) {
console.log("Message from client: "+ msg);
});


socket.on("move", function(msg) {
socket.broadcast.emit("move", msg);
});

socket.on('disconnect', function(){
console.log('a user disconnected');
});

});




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

app.use('/', express.static(public));










我需要的是国际象棋游戏能够正常运行,并且在 2 个玩家之间正确提交棋步

最佳答案

var socket = io.connect('http://localhost:8080');

您的服务器正在 8080 上运行,但您正在尝试连接:80。这应该可以解决问题。

关于javascript - 我正在使用 socket.io 但客户端和服务器之间没有通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55654004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com