gpt4 book ai didi

javascript - Socket.io http ://localhost:3000/socket. io/socket.io.js 404 (Not Found) - 如何配置 socket.IO - nodejs, apache2, websockets

转载 作者:行者123 更新时间:2023-11-29 23:20:37 24 4
gpt4 key购买 nike

好的,我在使用 socket.io 和 express 时又遇到了问题。当我运行我的 node js 应用程序时,它开始构建,然后遇到错误“GET http://localhost:3000/socket.io/socket.io.js 404(未找到)”和“Uncaught ReferenceError:io is not defined”这是我第二次使用网络套接字并收到相同的错误.对于之前的应用程序,我通过在我的 apache 服务器上设置反向代理来解决这个问题。看起来像这样;

ProxyPass /socket.io http://localhost:3000/socket.io

但是对于当前的 nodejs 应用程序,这并不能解决问题。这两个应用程序之间的主要区别在于,当前应用程序在用户将自己定向到 localhost:3000/bomber-kids-online 游戏页面之前不会开始使用 socket.io。当前的应用程序是第一个应用程序的扩展,也就是这个 nodejs 应用程序提供了一个网站以及我以前的应用程序,这是一个托管在 zEyeland.com/bomber-kids-online 上的游戏。

下面是我的 js 文件,它向浏览器发送要加载的正确 html 文件:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var express = require('express');
var router = express.Router();

var updatedMAP;
var updatedOBJECTS;

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

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

socket.on('sendNoramlBomb', function(xPosition, yPosition, power){

socket.broadcast.emit('sendNoramlBomb', xPosition, yPosition,
power);
});
socket.on('sendRedBomb', function(xPosition, yPosition, power){

socket.broadcast.emit('sendRedBomb', xPosition, yPosition, power);
});
socket.on('sendBlueBomb', function(xPosition, yPosition, power){

socket.broadcast.emit('sendBlueBomb', xPosition, yPosition,
power);
});
socket.on('sendGreyBomb', function(xPosition, yPosition, power){

socket.broadcast.emit('sendGreyBomb', xPosition, yPosition,
power);
});
socket.on('sendGreenBomb', function(xPosition, yPosition, power){

socket.broadcast.emit('sendGreenBomb', xPosition, yPosition,
power);
});

socket.on('sendPlayer', function(locationY, locationX, direction){


io.emit('sendPlayer',locationY, locationX, direction);
});

socket.on('chat message', function(msg){
io.emit('chat message', msg);
});

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

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

module.exports = router;

这是我当前项目的apache配置

    ProxyPass        /socket.io http://localhost:3000/bomber-kids-online/socket.io
ProxyPassReverse /socket.io http://localhost:3000/bomber-kids-online/socket.io


<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyRequests Off
ProxyPreserveHost On

ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/

所以回顾一下发生了什么。我正在尝试运行一个使用 websockets 的 nodejs 游戏。当在 localhost:3000/bomber-kids-online 网站上访问我的游戏时,它得到一个 ( GET http://localhost:3000/socket.io/socket.io.js 404 (Not Found) ) 我该如何解决这个问题?这次我的反向代理似乎无法修复它。您可以在 zeyeland.com/bomber-kids-online 查看我的游戏的工作版本。我当前的项目使用完全相同的 html 和 javascript 文件来运行。但是,通过检查我上面的 reverseProxy,你会注意到在我当前的项目中,游戏不是直接从 localhost:3000 访问的,而是从服务器上另一个 js 文件提供的路由访问的。

这是我的应用程序第一个 js 文件的样子;

 var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var bomberKidsRouter = require('./routes/games/bomber-kids-online-
game');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/bomber-kids-online', bomberKidsRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;

最佳答案

问题是 expresssocket.io 没有共享同一个服务器。我没有看到任何 server.listen,所以我猜测 socket.io 甚至没有在任何端口上监听。

你会收到这个错误,因为 http://localhost:3000/socket.io/socket.io.jsexpress 提供服务,当然你没有那个路线设置(你不应该这样做)。

修复方法是将 express 服务器附加到 socket.io

index.js

const app = express();

const server = require('http').Server(app);
const io = require('socket.io')(server); // Pass server to it instead of port

// Now you can pass `io` to any file you want, and setup your socket logic
// Do the same for express app.
// Or handle the logic here, whatever you prefer

server.listen(3000); // Listen

您可以这样做,或者为 socket.io 使用不同的端口。

Could you further explain or point me to documentaion about how socket.io works, why it can not run on same port as my node app

你不能让应用程序或服务器监听同一个端口,否则你会得到:Error: listen EADDRINUSE

这就是为什么如果你想在同一个端口上使用 express 和 socket.io,你必须使用同一个服务器监听两者的特定端口。

关于javascript - Socket.io http ://localhost:3000/socket. io/socket.io.js 404 (Not Found) - 如何配置 socket.IO - nodejs, apache2, websockets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50846572/

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