从http请求路由时,Express.io无法发出事件

时间:2013-11-24 16:54:42

标签: 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");
})

报告错误如下:

TypeError: Object #<Object> has no method 'emit'
at Object.hello (/Users/wensonsmith/ProjectX/Server/app.js:44:12)
at Manager.io.route (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/lib/index.coffee:65:29)
at Object.request.io.route (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/lib/index.coffee:143:29)
at /Users/wensonsmith/ProjectX/Server/app.js:39:12
at callbacks (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/express/lib/router/index.js:160:37)
at param (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/express/lib/router/index.js:134:11)
at pass (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/express/lib/router/index.js:141:5)
at Router._dispatch (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/express/lib/router/index.js:169:5)
at Object.router (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/express/lib/router/index.js:32:10)
at next (/Users/wensonsmith/ProjectX/Server/node_modules/express.io/node_modules/connect/lib/proto.js:190:15)

req.io.respond没问题。

广播也有一些问题。它可以播放,但广播后不会停止播放。 它运行了很长时间,然后什么也没有返回,也没有错误消息。

我的代码是

// 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");
})

2 个答案:

答案 0 :(得分:0)

根据堆栈跟踪判断,您发布的代码看起来不像实际代码。

但除此之外:据我了解express.io,当您将HTTP请求转发到io路由时,io路由应始终发送回{ {1}};否则,HTTP请求将停止。

所以试试这个:

respond

只是为了确保: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.broadcastsee docs)。

答案 1 :(得分:0)

只有50%回答;)::

.respond 将您的参数“直接”带到发布它们, e.g:

req.io.respond({hello: 'world'})