socket.io错误-建立连接之前先关闭Web套接字连接 [英] socket.io error - web socket connection is closed before the connection is established

查看:158
本文介绍了socket.io错误-建立连接之前先关闭Web套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此页中学到了一些知识->

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

这可能为您提供了不正确的参考您要附加的应用。


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

解决方案

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.

这篇关于socket.io错误-建立连接之前先关闭Web套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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