开发者

Use a "x-dom-event-stream" stream in javascript?

开发者 https://www.devze.com 2022-12-25 03:35 出处:网络
HTML5 draft contains an API called EventSource to stream data (notifications) trough javascript using only one server call.

HTML5 draft contains an API called EventSource to stream data (notifications) trough javascript using only one server call.

Looking it up, I found an exemple on Opera Labs of the javascript part :

document.getElementsByTagName("event-source")[0]
        .addEventListener("server-time", eventHandler, false);

funct开发者_StackOverflowion eventHandler(event)
{
    // Alert time sent by the server
    alert(event.data); 
}

and the server side part :

<?php
header("Content-Type: application/x-dom-event-stream");
while(true) {
    echo "Event: server-time\n";
    $time = time();
    echo "data: $time\n";
    echo "\n";
    flush();
    sleep(3);
}
?>

But as of today, it seems only Opera has implemented the API, neither Chrome nor Safari have a working version (Am I wrong here ?)

So my question is, is there any other way in javascript, maybe more complex, to use this one stream to get data ?

EDIT : I'm looking at Comet stuff right now, but I'm not sure how to reuse that :) EDIT 2 : Apparentry, "x-dom-event-stream" has now been renamed "text/event-stream" EDIT 3 : Got to understand way more of it with this recent article from javanet


See Orbited, which provides a javascript library and the server. The javascript library tries to use WebSockets and falls back to long-polling or other methods.

Here's a good guide: http://thingsilearned.com/2009/08/03/starting-out-with-comet-orbited-part-3-%E2%80%93-the-client/


Well, Google is really my friend, after a long search, i found this script from Ericson Labs (?!)

https://labs.ericsson.com/apis/eventsource/

From their page :

The EventSource enabler allows you to use the upcoming Event Source API in any current web browser. Simply include the enabler library in your page, and you are able to use the API. When user agents implement the Event Source API themselves, your application will immediately and transparently start using the native implementation.

However this solution requires an API key from Ericsson, and cannot be used for a commercial application, which is not the best solution for me :(

So if you have a better approach..


Have a look at the Kaazing Open Gateway. I believe it works similarly to the ericsson api in that it transparently switches to using the native implementation as it becomes available. I think it provides both eventsource and websockets.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号