Socket.io 404 未找到 [英] Socket.io 404 Not Found

查看:54
本文介绍了Socket.io 404 未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我不断收到http://127.0.0.1:3000/socket.io/socket.io.js" 当我在 chrome 开发者工具网络下查看时找不到 404.我花了一个多小时试图理解为什么这不起作用,因为它之前运行良好,我认为我没有改变任何东西.

For some reason I keep getting "http://127.0.0.1:3000/socket.io/socket.io.js" 404 not found when I look under chrome developer tools network. I have spent over an hour trying to understand why this isn't working because it was working fine before and I didn't think I changed anything.

服务器:

var express = require('express'),
    session = require('express-session');

var app = express();

var server = require('http').Server(app);
var io = require('socket.io')(server);
var MongoStore = require('connect-mongo')(session);

var sessionMiddleware = session({
    resave: false,
    saveUninitialized: false,
    secret: 'secret',
    store: new MongoStore({url: "mongodb://localhost:27017/database"})
});

app.use(sessionMiddleware);



io.use(sessionMiddleware, function(socket, next) {
    sessionMiddleware(socket.request, socket.request.res, next);
});

io.sockets.on('connection', function (socket) {
    console.log("Socket connected");
});

app.set('view engine', 'pug');
app.use(express.static(__dirname + '/public'));

app.get('/', function(req, res) {
   res.render('index');
});

app.listen(3000);
console.log('listening');

Pug 文件:

html
    head
        script(src="http://127.0.0.1:3000/socket.io/socket.io.js")
        script(type='text/javascript', src='../javascripts/chat.js')
    body

客户端 javascript:

Client javascript:

try {
    var socket = io.connect('http://127.0.0.1:3000');

} catch(e) {
    console.log(e);
}

推荐答案

改变这个:

app.listen(3000);

到:

server.listen(3000);

app.listen() 创建一个新的和不同的服务器,因此您连接 socket.io 的服务器不是实际正在侦听的服务器,因此您的 socket.io 服务器不是活动的如果脚本文件没有首先加载失败,则无法处理 /socket.io/socket.io.js 请求或传入连接.

app.listen() creates a new and different server so the server that you have socket.io attached to is not the one that is actually listening and thus your socket.io server is not live and not able to handle the /socket.io/socket.io.js request or the incoming connection if the script file didn't first fail to load.

有关 app.listen() 的作用以及为什么它不是您想要的代码布局方式的更多详细信息,请参阅此答案:

See this answer for more details on what app.listen() does and why it isn't what you want the way your code is laid out:

websockets, express.js 并且无法与服务器建立连接

注意,您可以使用 app.listen(),但您不必创建自己的服务器,并且必须从 app.listen() 并将其用作您传递给 socket.io 初始化的服务器:

Note, you could use app.listen(), but you'd have to not create your own server and you'd have to capture the return value from app.listen() and use that as the server you pass to socket.io initialization:

var app = express();
var server = app.listen(3000);
var io = require('socket.io')(server);

这与您的代码结构不太一样(但您可以通过这种方式重新安排内容).

Which is not quite how your code is structured (but you could rearrange things to do it this way).

这篇关于Socket.io 404 未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆