2012-05-22 60 views
0

我在使用iisnode獲取使用iisnode的節點快速應用程序時遇到了一些困難。我在iis中有一個簡單的http pub/sub服務器設置。這通過POST接收消息,並將數據推送到相關的監聽客戶端。代碼...使用iisnode運行節點快速服務器 - EventSource處理程序未啓動

服務器

var express = require('express'); 
var app = require('express').createServer(); 

var clients = {}; 

app.configure(function(){ 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
}); 

app.get('/pubsubleads/leads/:id', function(request, response) { 
    var id = request.params.id.toString(); 

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

    clients[id] = response; 

    request.on('close', function(){ 
     console.log('connection closing'); 
     delete clients[id]; 
    }); 
}); 

app.post('/pubsubleads/leads', function(request, response){ 
    console.log('New post'); 

    response.writeHead(200, {'Content-Type': 'text/plain'}); 
    response.end(); 

    var id = request.body.id; 

    if(clients[id] != null) { 
     console.log('id = ' + id); 
     console.log('data = ' + request.body.data); 

     clients[id].write('id: ' + request.body.id + '\n'); 
     clients[id].write('data: ' + request.body.data + '\n\n'); 
    } 
}); 
app.listen(process.env.PORT); 
console.log('Server started on port ' + process.env.PORT); 

客戶

var source = new EventSource('/pubsub/data/@Session.SessionID'); 

source.onopen = function (e) { 
    alert('onopen'); 
    document.body.innerHTML += 'Connected <br>'; 
}; 

source.onmessage = function (e) { 
    alert('onmessage: ' + e.data); 
    document.body.innerHTML += e.data + '<br/>'; 
}; 

source.onerror = function (e) { 
    if (e.readyState == EventSource.CLOSED) { 
     alert('closed'); 
    } 
}; 

...其中的參數傳遞給EventSource的網址是什麼,唯一標識此客戶端。

服務器和客戶端位於同一根域下的單獨子域中。我已確認服務器正在正確接收郵件。我遇到的問題是客戶端事件處理程序從未被擊中。我曾嘗試在$(document).ready()中包裝客戶端代碼,但這沒有任何影響。我已經使用獨立的節點進程複製了IIS之外的設置,並且正常工作。

我在Windows 7 64位上全部使用Chrome 18,nodejs 0.6.18,iisnode,IIS 7.5。

我剛剛從創意中脫穎而出,一直未能找到任何類似的在線討論問題。任何幫助,將不勝感激。

回答

3

默認情況下,IIS最多緩存響應數據的4MB沖洗,從而提高在短期反應和靜態文件的情況下的性能之前。看起來您打算將一小段數據流傳輸回客戶端,並期望傳輸這些數據塊的延遲時間很短。

iisnode允許您通過在每次寫入後強制刷新響應數據來覆蓋此默認IIS行爲。請將web.config中的system.webServer \ iisnode \ @flushResponse配置設置設置爲true(https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config#L90)這種行爲。或者,如果升級到iisnode v0.1.19或更高版本,也可以使用新的iisnode.yml配置文件來設置此值(有關詳細信息,請參閱http://tomasz.janczuk.org/2012/05/yaml-configuration-support-in-iisnode.html)。

+1

非常好。 flushResponse = true是答案。非常感謝你。 –

0

EventSource沒有與WebSockets相同的客戶端API。您需要使用addEventListener

source.addEventListener('open', function (e) { 
    // opened 
}); 

source.addEventListener('message', function (e) { 
    // e.data 
}); 

source.addEventListener('error', function (e) { 
    // whoops 
}); 
+0

我曾嘗試過,以前和現在再次,但不註冊EventSource上的偵聽器。從我的帖子複製粘貼時,以及提供第三個參數時(bool useCapture = false),情況就是這樣。它們在頁面加載後爲空。使用我以前的語法,偵聽器被定義,但沒有命中。奇怪的是,如果我離開了一個客戶足夠長的時間,最終,onerror處理程序確實觸發了,但onopen和onmessage從未做過。 –

+0

你可以爲每個消息嘗試'event:foo'然後'source.addEventListener('foo',function(e){...})'? – igorw

+0

剛剛嘗試過,但沒有運氣,我很害怕。 –

相關問題