java - JavaScript consuming SSE from Jersey produces unexpected output -


i have jersey rest web service running on jetty web server. server running on port 8000 on localhost only.

at moment there 1 method:

@get @produces(ssefeature.server_sent_events) public eventoutput getserversentevents() {     final eventoutput eventoutput = new eventoutput();     new thread(new runnable() {         @override         public void run() {             try {                 (int = 0; < 10; i++) {                     thread.sleep(1000);                     final outboundevent.builder eventbuilder = new outboundevent.builder();                     eventbuilder.data(string.class, "hello world " + + "!");                     final outboundevent event = eventbuilder.build();                     eventoutput.write(event);                 }             } catch (exception e) {                 // exception handling             } {                 eventoutput.close();             }         }     }).start();     return eventoutput; } 

typing http://localhost:8000 in chrome produces expected output of:

data: hello world 0! data: hello world 1! data: hello world 2! data: hello world 3! data: hello world 4! data: hello world 5! data: hello world 6! data: hello world 7! data: hello world 8! data: hello world 9! 

using following javascript code:

function runaether() {     var eventsource = new eventsource("http://localhost:8000");         eventsource.onmessage = function(event) {         document.getelementbyid("result").innerhtml += event.data + "<br>";     }; } 

produces following result:

hello world 0! hello world 1! hello world 2! hello world 3! hello world 4! hello world 5! hello world 6! hello world 7! hello world 8! hello world 9! hello world 0! hello world 1! hello world 2! hello world 3! hello world 4! hello world 5! , on... 

why javascript solution keep getting output, unexpected? how can fix error?


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -