socket.io 错误 - Web 套接字连接在建立连接之前关闭
Posted
技术标签:
【中文标题】socket.io 错误 - Web 套接字连接在建立连接之前关闭【英文标题】:socket.io error - web socket connection is closed before the connection is established 【发布时间】:2015-01-08 22:00:27 【问题描述】:我从这个页面学到了一点-> https://github.com/Automattic/socket.io/issues/1846
我是否需要 SSL 才能使套接字工作?
我一直在为这个错误苦苦挣扎,到目前为止没有解决方案,有没有天才可以解决这个难题?
我的应用代码
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 握手 我评论了几行代码并且它工作了,这些行包括 app.use 中间件的东西(与标题相关),还删除了 node_modules 文件夹并重新安装了保存,(认为它在本地主机上工作),我'我会尝试重现错误 【参考方案1】:您可能将 socket.io 附加到服务器端的错误项目。使用 express 时,您的 http 对象是不必要的。您可以将其直接附加到快递应用程序吗?见这里:
http://socket.io/docs/#using-with-the-express-framework
var io = require('socket.io')(app)
这可能会为您的应用附加一个不正确的参考。
【讨论】:
我相信他已经为 express 4 正确配置了它。以上是关于socket.io 错误 - Web 套接字连接在建立连接之前关闭的主要内容,如果未能解决你的问题,请参考以下文章