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

时间:2014-11-12 06:09:54

标签: node.js sockets chat

我从这个页面学到了一点 - > https://github.com/Automattic/socket.io/issues/1846

套接字是否需要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)
        });
    });
},

1 个答案:

答案 0 :(得分:0)

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

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

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

这可能会为您的应用添加不正确的引用。