socket.io error - web socket connection is closed before the connection is established

耗尽温柔 提交于 2019-12-23 10:51:28

问题


I learned a bit from this page - > https://github.com/Automattic/socket.io/issues/1846

Do I need SSL for sockets to work?

I've been struggling with this error for a long time with no solution so far, can any geniuses out there to solve the puzzle?

My App Code

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)
        });
    });
},

回答1:


You might be attaching socket.io to the wrong item on the server side. Your http object is unneccessary when using express. you can attach it directly to the express app? See here:

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

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

That may be giving you an incorrect reference for your app to attach to.




回答2:


Your initialization for Express & Socket.io together seem to be correct. Does the server you're using support WebSockets? That error just looks to be one that states the a WebSocket connection couldn't be established.

Socket.io establishes a connection first with Long-Polling and then will attempt to upgrade that connection to a WebSocket connection if it can. It looks like it can't upgrade but the app may still work. Have you confirmed that the Client can't emit over the Socket to the server and that the server isn't handling it appropriately?



来源:https://stackoverflow.com/questions/26880396/socket-io-error-web-socket-connection-is-closed-before-the-connection-is-estab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!