socket.io错误 - 在连接建立之前web套接字连接已关闭

5
我从这个页面上学到了一些知识 - > https://github.com/Automattic/socket.io/issues/1846 我的问题是:Socket是否需要SSL支持才能工作?

enter image description here

我已经苦苦思索了很长时间,但是还没有找到解决方案。请问有没有聪明的人可以帮忙解决这个难题?

我的应用程序代码:
var express = require("express");
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.use(express.static(__dirname + '/html'));

http.listen(process.env.PORT || 3000, function(){
    console.log('listening on *:', process.env.PORT || 3000);
    new shell.Shell(app, io);
});


app.use(function(request, response, next){
        response.header("Access-Control-Allow-Origin", "*");
        response.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
        response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Auth-Token");
        request.id = self.id++;
        next();
    }); 

    var self = this;

    app.get("/", function(request, response) {
        response.end("");
    });

    app.get("/entitlement/:uri", function(request, response){
        self.delegate.entitlement(request, response);
    });

    app.get("/speakers", function(request, response) {
        self.delegate.speaker(request, response);
    });

    app.get("/speakers/:id", function(request, response) {
        self.delegate.speaker(request, response);
    });

    app.get("/sponsors", function(request, response){
        self.delegate.sponsor(request, response);
    });

    app.get("/sponsors/:id", function(request, response) {
        self.delegate.sponsor(request, response); 
    });

    app.get("/agendas", function(request, response) {
        self.delegate.agenda(request, response);
    });

    app.get("/agendas/:id", function(request, response) {
        self.delegate.agenda(request, response); 
    });

    app.get("/sessions/:id", function(request, response){
        self.delegate.agenda(request, response);
    });

    app.get("/attendees", function(request, response) {
        //self.delegate.attendee(request, response);    
    });

    ///attendees/:id to get chat history

    io.on("connection", function(socket){

        //self.delegate.connection(io, socket, null);

        socket.on('get-age-in-dog-years', function(data, fn) {
            console.log(data);
            fn(data.age * 7) ;
        });

        socket.on("chat", function(chat){
            //self.delegate.chat(io, socket, chat);
        });

        socket.on("disconnect", function(){
            //self.delegate.disconnect(io, socket)
        });
    });
},

这是错误400的坏请求。似乎客户端正在期望一个WebSocket连接,但升级握手可能没有发生。您可以进行tcpdump并查看ws握手。 - gogasca
1
我在代码中添加了一些注释,然后它就正常工作了。这些行包括使用app.use中间件的内容(涉及头部信息)。此外,我还删除了node_modules文件夹,并使用save重新安装了它(尽管在本地环境上是可以工作的)。我将尝试重现错误。 - user2727195
1个回答

0

你可能在服务器端将socket.io附加到了错误的项目上。当使用express时,你的http对象是不必要的。你可以直接将它附加到express应用程序上。请参见这里:

http://socket.io/docs/#using-with-the-express-framework

var io = require('socket.io')(app)

这可能会给您的应用程序提供错误的引用以便附加。


我相信他已经正确地配置了Express 4。 - gkiely

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接