javascript 如何在同一个端口上使用 ExpressJS 和 Socket.io?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12235406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use ExpressJS and Socket.io on a same port?
提问by NiLL
In the third version of ExpressJS express.createServer()changed to express()this changes makes difficult to bind socket.io on a same port. Maybe somebody could find robust decision.
在第三个版本的 ExpressJS express.createServer()更改为express()这个变化使得很难在同一个端口上绑定 socket.io。也许有人可以找到可靠的决定。
Now, this doesn't work:
现在,这不起作用:
var express = require('express')
, app = express.createServer()
, io = require('socket.io').listen(app);
My current workflow: https://gist.github.com/3596852
我目前的工作流程:https: //gist.github.com/3596852
回答by Cristi Mihai
It's described on the socket.iogithub page (as @Golostated in your comment):
它在socket.iogithub 页面上有描述(如@Golo在您的评论中所述):
var app = express()
, server = require('http').createServer(app)
, io = io.listen(server);
server.listen(80);
This works, I have it running.
这有效,我让它运行。
Probably what Golo have forgotten is to change the listen from app.listen(80)
to server.listen(80)
. I've struggled with this too until I realised my stupid mistake.
大概 Golo 忘记的是将听从 更改app.listen(80)
为server.listen(80)
。我也一直在努力解决这个问题,直到我意识到我的愚蠢错误。
回答by supernova
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server)
app.start = app.listen = function(){
return server.listen.apply(server, arguments)
}
app.start(8080)