gpt4 book ai didi

javascript - Socket.io + node.js - 客户端未接收广播发射

转载 作者:行者123 更新时间:2023-12-01 00:27:10 24 4
gpt4 key购买 nike

我正在尝试使用套接字io编写一个游戏,但我遇到了一些困难,如果你看一下我发送 Freset(true) 时的示例,服务器收到它并尝试将重置变量发送给客户端,然而!客户端没有收到服务器发送的消息。启动时它收到消息,之后就没有了

index.html(客户端)

<!doctype html>
<html>

<head>
<title></title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

#playerOneBoard {
background-color: rgb(202, 147, 147);
width: 99%;
height: 30%;
display: inline-block;
}

#playerTwoBoard {
background-color: rgb(146, 146, 223);
width: 99%;
height: 30%;
display: inline-block;
}

.card {
width: 200px;
height: 350px;
margin: 0 75px;
border: black 5px solid;
}

.summon {
transform: rotate(12deg);
}

.tap {
transform: rotate(90deg);
}
</style>
</head>

<body>
<div id="board">
<div id="playerOneBoard"></div>
<ul id="playerOneHand"></ul>
<ul id="playerTwoBoard"></ul>
<ul id="playerTwoHand"></ul>
</div>
<input id="u" type="radio" name="name" value="player one" checked>
<input id="u" type="radio" name="name" value="Player two">
<input id="submit1" type="button" value="synch">
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var playerOneLibrary = [];
var playerOneHand = [];
var playerOneBoard = [];
var playerOneGraveyard = [];
var playerOneExile = [];

var playerTwoLibrary = [];
var playerTwoHand = [];
var playerTwoBoard = [];
var playerTwoGraveyard = [];
var playerTwoExile = [];

//🔴🟢🔵⚫🟡⚪
$(function() {
var socket = io();

socket.on('chat message', function(p1l, p2l, p1h, p2h, p1b, p2b, p1g, p2g, p1e, p2e) {
playerOneLibrary = p1l;
playerTwoLibrary = p2l;
playerOneHand = p1h;
playerTwoHand = p2h;
playerOneBoard = p1b;
playerTwoBoard = p2b;
playerOneGraveyard = p1g;
playerTwoGraveyard = p2g;
playerOneExile = p1e;
playerTwoExile = p2e;
console.log('message recieved');
});

Freset = function(x) {
socket.emit('reset game', x);
}

FdrawCard = function(obj) {
var card = `
<li id="${obj.cid}" class="card">
<h2>${obj.name} <b> ${obj.mana} </b> </h2>
<h3> ${obj.type} </h3>
<p>
${obj.text}
<i>${obj.power}/ ${obj.toughness}</i>
</p>
<div> buttons </div>
</li>
`;
return card;
}

Fdraw = function() {
for (x in playerOneLibrary) {
$('#playerOneBoard').append(FdrawCard(playerOneLibrary[x]));
}
for (x in playerTwoLibrary) {
$('#playerTwoBoard').append(FdrawCard(playerTwoLibrary[x]));
}
}

});
</script>
</body>

</html>

index.js(服务器)

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);


//player one library
var playerOneDeck = [{
"cid": "101",
"name": "red lotus",
"type": "land",
"text": "this inherits the properties of the green lotus",
"mana": "5 🔴",
"power": "0",
"toughness": "0",
},
{
"cid": "102",
"name": "white lotus",
"type": "land",
"text": "this does double the effect of the red lotus",
"mana": "5 🟡",
"power": "0",
"toughness": "0",
}
];
var playerOneLibrary = playerOneDeck;
var playerOneHand = [];
var playerOneBoard = [];
var playerOneGraveyard = [];
var playerOneExile = [];

var playerTwoDeck = [{
"cid": "201",
"name": "blue lotus",
"type": "land",
"text": "this does more than the white lotus",
"mana": "5 🔵",
"power": "0",
"toughness": "0",
},
{
"cid": "202",
"name": "green lotus",
"type": "land",
"text": "this does nothing",
"mana": "5 🟢",
"power": "0",
"toughness": "0",
}
];
var playerTwoLibrary = playerTwoDeck;
var playerTwoHand = [];
var playerTwoBoard = [];
var playerTwoGraveyard = [];
var playerTwoExile = [];


app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});


io.on('connection', function(socket) {

socket.emit('chat message',
playerOneLibrary, playerTwoLibrary,
playerOneHand, playerTwoHand,
playerOneBoard, playerTwoBoard,
playerOneGraveyard, playerTwoGraveyard,
playerOneExile, playerTwoExile);

socket.on('chat message', function(p1l, p2l, p1h, p2h, p1b, p2b, p1g, p2g, p1e, p2e) {

playerOneLibrary = p1l;
playerTwoLibrary = p2l;
playerOneHand = p1h;
playerTwoHand = p2h;
playerOneBoard = p1b;
playerTwoBoard = p2b;
playerOneGraveyard = p1g;
playerTwoGraveyard = p2g;
playerOneExile = p1e;
playerTwoExile = p2e;
console.log('better not be a loop')
socket.broadcast.emit('chat message',
playerOneLibrary, playerTwoLibrary,
playerOneHand, playerTwoHand,
playerOneBoard, playerTwoBoard,
playerOneGraveyard, playerTwoGraveyard,
playerOneExile, playerTwoExile
);

});


socket.on('reset game', function(x) {

console.log(playerTwoLibrary);
if (x == true) {

playerOneLibrary = playerOneDeck;
playerOneHand = [];
playerOneBoard = [];
playerOneGraveyard = [];
playerOneExile = [];

playerTwoLibrary = playerTwoDeck;
playerTwoHand = [];
playerTwoBoard = [];
playerTwoGraveyard = [];
playerTwoExile = [];
console.log('game reset');

socket.broadcast.emit('chat message',
playerOneLibrary, playerTwoLibrary,
playerOneHand, playerTwoHand,
playerOneBoard, playerTwoBoard,
playerOneGraveyard, playerTwoGraveyard,
playerOneExile, playerTwoExile
);

console.log(playerTwoLibrary);
} else { console.log('didnt reset') }
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});

我确信我错过了一些东西,希望你们能为我指出好的方向

最佳答案

socket.broadcast.emit 发送到每个已连接的套接字(与 socket 位于同一命名空间中),socket 除外。因此,它发送到所有其他套接字,而不是 socket 表示的套接字。 socket.broadcast 的相关 socket.io 文档 here .

如果您想发送到所有连接的套接字,请使用:

io.emit(...)

或者,如果您只想发送到该一个套接字,请使用:

socket.emit(...)

仅供引用,我发现这个“emit cheatsheat ”包含了您可以发送的所有不同方式,非常有用。

关于javascript - Socket.io + node.js - 客户端未接收广播发射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58884004/

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