我正在嘗試在我的Ruby Grape API上創建服務器發送的事件。 問題是,連接似乎總是非常快速地關閉,因爲我一直在測試網頁上獲取Connection closed
事件。服務器發送的Ruby葡萄事件
客戶端連接到服務器,因爲我可以看到被調用的方法,但我想知道爲什麼連接不是恆定的,爲什麼我沒有收到我使用線程發送的數據。
這裏是我的Ruby代碼:
$connections = []
class EventsAPI < Sinantra::Base
def connections
$connections
end
get "/" do
content_type "text/event-stream"
stream(:keep_open) { |out|
puts "New connection"
out << "data: {}\n\n"
connections << out
}
end
post "/" do
data = "data\n\n"
connections.each { |out| out << data }
puts "sent\n"
end
end
這裏是我的javascript:
var source = new EventSource('http://localhost:9292/events');
source.onmessage = function(e) {
console.log("New message: ", e.data);
showMessage(e.data);
};
source.onopen = function(e) {
// Connection was opened.
};
source.onerror = function(e) {
console.log("Source Error", e)
if (e.eventPhase == EventSource.CLOSED) {
console.log("Connection was closed");
// Connection was closed.
}
};
var showMessage = function(msg) {
var out = document.getElementById('stream');
var d = document.createElement('div')
var b = document.createElement('strong')
var now = new Date;
b.innerHTML = msg;
d.innerHTML = now.getHours() + ":" + now.getMinutes() + ":" +now.getSeconds() + " ";
d.appendChild(b);
out.appendChild(d);
};
編輯:我得到了它與GET方法的工作(我改變了葡萄:: API來西納特拉: :葡萄基礎不實現流)。我現在接收數據,但連接不會保持活動狀態,當我使用post方法時,數據永遠不會到達瀏覽器。
非常感謝您的回答。
我剛剛回答,但是如果您在Grape API中專門針對SSE的示例,那麼發佈指向該文檔的鏈接將非常有用。 (特別是我想知道'keep_open'究竟做了什麼。) – 2015-04-01 17:00:55