gpt4 book ai didi

node.js - 使用socket.IO err_connection_refused

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:03 26 4
gpt4 key购买 nike

我正在使用 nodejs 和 socket.io 制作多人游戏。

当我在运行 Nodejs 的笔记本电脑上工作时,一切正常。我可以打开多个窗口选项卡并且我有多个用户。

但是当我尝试从旁边的另一台笔记本电脑连接到我的本地 IP(从 Node 服务器)时,我收到了 socket.io 错误

 "GET http://localhost:8000/socket.io/socket.io.js net::ERR_CONNECTION_REFUSED"

代码

 /**************************************************
** GAME INITIALISATION
**************************************************/
function init() {
// Declare the canvas and rendering context
canvas = document.getElementById("gameCanvas");
ctx = canvas.getContext("2d");

// Maximise the canvas
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

// Initialise keyboard controls
keys = new Keys();

// Calculate a random start position for the local player
// The minus 5 (half a player size) stops the player being
// placed right on the egde of the screen
var startX = Math.round(Math.random()*(canvas.width-5)),
startY = Math.round(Math.random()*(canvas.height-5));

// Initialise the local player
localPlayer = new Player(startX, startY);

socket = io.connect("http://localhost", {port: 8000, transports: ["websocket"]});

remotePlayers = [];

// Start listening for events
setEventHandlers();


};

已添加代码

  /**************************************************
** NODE.JS REQUIREMENTS
**************************************************/
var util = require("util"), // Utility resources (logging, object inspection, etc)
io = require("socket.io"); // Socket.IO
Player = require("./Player").Player; // Player class


/**************************************************
** GAME VARIABLES
**************************************************/
var socket, // Socket controller
players; // Array of connected players


/**************************************************
** GAME INITIALISATION
**************************************************/
function init() {
// Create an empty array to store players
players = [];

socket = io.listen(8000);

socket.configure(function()
{
socket.set("transports", ["websocket"]);
socket.set("log level", 2);
});

setEventHandlers();

};

最佳答案

我重试将 localhost 替换为运行 Nodejs 服务器的 PC 的 IP 地址,结果确实有效。

关于node.js - 使用socket.IO err_connection_refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27207469/

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