当从http请求路由时,Express.io无法发出事件
它报告如下错误:当从http请求路由时,Express.io无法发出事件,express,socket.io,Express,Socket.io,它报告如下错误: Here i use those codes: // Initial web request. app.get('/hello', function(req, res) { // Forward to an io route. req.io.route('hello') }) app.io.route('hello', function(req) { //Here use emit req.io.emit("world","world");
Here i use those codes:
// Initial web request.
app.get('/hello', function(req, res) {
// Forward to an io route.
req.io.route('hello')
})
app.io.route('hello', function(req) {
//Here use emit
req.io.emit("world","world");
})
从堆栈跟踪判断,您发布的代码看起来不像是实际代码 但除此之外:据我所知,当您将HTTP请求转发到io路由时,io路由应始终使用
respond
发回响应;否则,HTTP请求将暂停
所以试试这个:
// Initial web request.
app.get('/hello', function(req, res) {
// Forward to an io route.
req.io.route('hello')
})
// Forward io route to another io route.
app.io.route('hello', function(req) {
req.io.broadcast("world","world");
})
只是为了确保:req.io.broadcast
不会将消息发送回发起请求的客户端。如果您想这样做,请改用app.io.broadcast
()
.response将您的论点“直接”传递给发出它们,
e、 g:
对添加
req.io.respond()
后效果很好!非常感谢!顺便问一下,你知道为什么req没有方法“emit”吗?@WensonSmith我认为它被称为req.io.emit()
// Initial web request.
app.get('/hello', function(req, res) {
// Forward to an io route.
req.io.route('hello')
})
// Forward io route to another io route.
app.io.route('hello', function(req) {
req.io.broadcast("world","world");
})
app.get('/hello', function(req, res) {
req.io.route('hello');
});
app.io.route('hello', function(req) {
// broadcast first...
req.io.broadcast("world","world");
// ...then send back an empty response
req.io.respond();
});
req.io.respond({hello: 'world'})