2014-06-28 22 views
2

我正試圖在Cloud代碼中實現一個簡單的SSE。服務器在Cloud代碼中發送事件

這裏是我的快遞路線打開連接:

app.get('/call', function(req, res) { 

    // let request last as long as possible 
    req.socket.setTimeout(30000); 

    //send headers for event-stream connection 
    res.writeHead(200, { 
    'Content-Type': 'text/event-stream', 
    'Cache-Control': 'no-cache', 
    'Connection': 'keep-alive' 
    }); 

    res.write('\n'); 

    // The 'close' event is fired when a user closes their browser window. 
    req.on("close", function() { 

    }); 
}); 

我也確信包括:

var http = require('http'); 

問題是,我得到這個錯誤:

I2014-06-28T19:09:50.500Z] TypeError: Object function() { 
    throw Error('IncomingMessage.socket not supported'); 
    } has no method 'setTimeout' 
    at app.js:129:14 
    at callbacks (express_router.js:161:37) 
    at param (express_router.js:135:11) 
    at pass (express_router.js:142:5) 
    at Router._dispatch (express_router.js:170:5) 
    at Object.router (express_router.js:33:10) 
    at next (connect_proto.js:240:15) 
    at Object.handle (app.js:88:7) 
    at next (connect_proto.js:240:15) 
    at Object.parseExpressCookieSession [as handle] (parse-express-cookie-session.js:315:7) 

我也嘗試了:

req.setTimeout(30000); 

但是得到一個類似的錯誤。

我看了一些關於如何setTimeout沒有實現,直到節點的更高版本。這可能是問題嗎?我無法找到Parse正在運行的節點的版本。

+1

'process.versions' – Prinzhorn

回答

2

解析雲代碼不支持setTimeout()。他們似乎沒有在文檔中的任何地方提到這一點,但他們在舊論壇和其他問題中多次提及。

+2

我剛剛決定嘗試建立自己的節點服務器。我遇到了太多的限制,試圖將Cloud Code用於我正在嘗試做的事情。 – Ramsel

0

你可以嘗試與parse.com結合使用PubNub.com獲得實時的體驗。

+0

關於如何使用它的任何細節? –

+0

此鏈接將幫助你到達那裏http://www.pubnub.com/blog/realtime-collaboration-sync-parse-api-pubnub/ –

+0

你能不能給一個代碼示例,你如何使用它來解決問題這個問題? –

-1

我已經赫克託·拉莫斯從解析找到了答案在此Google Group Discussion

setTimeout() is not available in Cloud Code, but you can use process.nextTick() instead. You can see an example in Fosco's queuing module Parse-Resque .