我用socket.io得到404如何解决这个问题

时间:2017-02-09 16:30:49

标签: node.js sockets socket.io

我是node.js的新手,我正在尝试设置Socket.io,但它给出了404这样的,

socket.io-1.4.7.js:1 GET http://localhost:9000/socket.io/?EIO=3&transport=polling&t=LeZf0Tg 404 (Not Found)Request.create @ socket.io-1.4.7.js:1Request @ socket.io-1.4.7.js:1XHR.request @ socket.io-1.4.7.js:1XHR.doPoll @ socket.io-1.4.7.js:1Polling.poll @ socket.io-1.4.7.js:1Polling.doOpen @ socket.io-1.4.7.js:1Transport.open @ socket.io-1.4.7.js:1Socket.open @ socket.io-1.4.7.js:1Socket @ socket.io-1.4.7.js:1Socket @ socket.io-1.4.7.js:1Manager.open.Manager.connect @ socket.io-1.4.7.js:2(anonymous function) @ socket.io-1.4.7.js:3
socket.io-1.4.7.js:1 GET http://localhost:9000/socket.io/?EIO=3&transport=polling&t=LeZf1h_ 404 (Not Found)

这里我的服务器端代码是

var express = require("express");
var app = express();
var mongoose = require("mongoose");
var bodyParser = require("body-parser");
var config = require("./config/config");
var http    = require('http').Server(app);
var io      = require('socket.io')(http);

// configuration
mongoose.connect(config.database);

// template engine
app.set("view engine", "ejs");
app.engine("html", require("ejs").renderFile);
app.set("views", __dirname + "/../client/views");

// use
app.use(express.static("../client/public"));
app.use(express.static("../client/app"));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//app.use(morgan("dev"));



// Route handler for www requests
app.get('/*', function (req, res, next) {
    if (req.headers.host.match(/^www/) !== null) {
        res.redirect('http://' + req.headers.host.replace(/^www\./, '') + req.url);
    } else {
        next();
    }
});

app.use("/", require("./routes"));
app.use("/admin", require("./routes/admin"));
app.use("/test", require("./routes/testUser"));

var port = process.env.PORT || 9000;

io.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});


var server = app.listen(port, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log("app listening on " + host + " " + port);
});

和我的客户端我正在使用Ejs模板引擎来创建Socket连接

<html>
<body>
    <p class="dancing">Simple Blog</p>

    <div class="social-icons">
        <a href="https://twitter.com" target="_blank">
            <i class="fa fa-twitter-square"></i>
        </a>
        <a href="https://github.com/" target="_blank">
            <i class="fa fa-github-square"></i>
        </a>
        <a href="https://www.linkedin.com" target="_blank">
            <i class="fa fa-linkedin-square"></i>
        </a>
        <a href="https://www.facebook.com" target="_blank">
            <i class="fa fa-facebook-square"></i>
        </a>
    </div>

<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script>
    var socket = io.connect('http://localhost:9000');
    socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
    });
</script>
</body>
</html>

可以请任何人帮助我解决这个问题。

2 个答案:

答案 0 :(得分:1)

您没有在服务器上正确执行socket.io初始化。

这两行代码:

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

正在创建一个新的http服务器并将socket.io附加到它,然后该新服务器永远不会启动。

您可能想要做的是与socket.io共享您的同一个快速服务器(通常使用socket.io)。

在您的代码中,您可以通过删除上面的这两行然后在最后插入一行来实现:

// code you already have
var server = app.listen(port, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log("app listening on " + host + " " + port);
});

// initialize socket.io to use your existing express server
var io = require('socket.io')(server);

答案 1 :(得分:0)

您已使用http require('socket.io')(http);

初始化了socket.io

所以你必须像http.listen(port)那样监听端口。类似的问题已回答here