I'm currently working on a simple game using WebSockets (first time!). However, my console is filled with net::ERR_CONNECTION_REFUSED
errors, and I can't get anything to work.
I've been trying to fix the errors for hours and have seen a few other questions online with similar problems, but nothing has worked for me so far.
Here is my code:
server.js
:
const { createGameState, gameLoop } = require("./game")
const { FRAME_RATE } = require("./constants")
const io = require("socket.io")(httpServer, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"]
}
});
io.on("connection", client => {
const state = createGameState()
startGameInterval(client, state)
});
function startGameInterval(client, state) {
const intervalId = setInterval(() => {
const winner = gameLoop(state);
console.log("interval");
if(!winner) {
client.emit("gameOver");
clearInterval(intervalId);
} else {
client.emit("gameover");
clearInterval(intervalId);
}
}, 1000 / FRAME_RATE); // 1000
}
io.listen(3000);
index.js
(top part):
const backgroundColor = "#1c1315";
const snakeColor = "#7d7778";
const foodColor = "#f55872";
const socket = io("http://localhost:3000")
socket.on("init", handleInit());
// socket.on("gamestate", handleGameState());
const gameScreen = document.getElementById("gameScreen");
let canvas, ctx;
const gameState = {
player: {
pos: {
x: 3,
y: 10,
},
vel: {
x: 1,
y: 0,
},
snake: [
{x: 1, y: 10},
{x: 2, y: 10},
{x: 3, y: 10},
]
},
food: {
x: 7,
y: 7,
},
gridsize: 20,
};
Here is the error that I'm getting; it gets printed in the console every 5 seconds or so. polling-xhr.js:206 GET http://localhost:3000/socket.io/?EIO=4&transport=polling&t=NW6GI07 net::ERR_CONNECTION_REFUSED
I'm very new to socket.io/websockets in general, so I don't know what this error means or what I could do to fix the issue. Any help would be massively appreciated!
Aucun commentaire:
Enregistrer un commentaire