I am currently having a problem related to SSE and Windows XP. The source code below is currently working in every Chrome I tried except for Chrome in Windows XP (?) Not sure why. This is intended to be used for a control panel, where users must use Chrome. In other words, I don't care about IE, Firefox, etc.
The problem: Server side events works everywhere (Chrome) but not in Windows XP (Chrome). When I say it works, I mean that the message handler is called.
The code
Javascript code
if (!!window.EventSource) { console.log("Event source available"); var source = new EventSource('/admin/systemalert'); source.addEventListener('message', function(e) { console.log(e.data); }); source.addEventListener('open', function(e) { console.log("Connection was opened."); }, false); source.addEventListener('error', function(e) { if (e.readyState == EventSource.CLOSED) { console.log("Connection was closed."); } else { console.log(e.readyState); <-- in windows XP it prints Error here } }, false); } else { console.log("No SSE available"); }
Server side code
@Controller @RequestMapping("/admin/**") public class AdminController { @RequestMapping("systemalert") public @ResponseBody String sendMessage(HttpServletResponse response) { Random r = new Random(); response.setContentType("text/event-stream"); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } return "data:Testing 1,2,3" + r.nextInt() +"\n"; } }
As stated in the code, the line console.log(e.readyState); prints "Error" when using Chrome in Windows XP. Any ideas? Anyone see anything wrong with the source code?
Thanks in advance. Agustin
I am currently having a problem related to SSE and Windows XP. The source code below is currently working in every Chrome I tried except for Chrome in Windows XP (?) Not sure why. This is intended to be used for a control panel, where users must use Chrome. In other words, I don't care about IE, Firefox, etc.
The problem: Server side events works everywhere (Chrome) but not in Windows XP (Chrome). When I say it works, I mean that the message handler is called.
The code
Javascript code
if (!!window.EventSource) { console.log("Event source available"); var source = new EventSource('/admin/systemalert'); source.addEventListener('message', function(e) { console.log(e.data); }); source.addEventListener('open', function(e) { console.log("Connection was opened."); }, false); source.addEventListener('error', function(e) { if (e.readyState == EventSource.CLOSED) { console.log("Connection was closed."); } else { console.log(e.readyState); <-- in windows XP it prints Error here } }, false); } else { console.log("No SSE available"); }
Server side code
@Controller @RequestMapping("/admin/**") public class AdminController { @RequestMapping("systemalert") public @ResponseBody String sendMessage(HttpServletResponse response) { Random r = new Random(); response.setContentType("text/event-stream"); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } return "data:Testing 1,2,3" + r.nextInt() +"\n"; } }
As stated in the code, the line console.log(e.readyState); prints "Error" when using Chrome in Windows XP. Any ideas? Anyone see anything wrong with the source code?
Thanks in advance. Agustin
Share Improve this question edited Apr 9, 2012 at 20:46 krock 29.6k13 gold badges80 silver badges86 bronze badges asked Apr 9, 2012 at 20:40 Agustin LopezAgustin Lopez 1,3654 gold badges18 silver badges35 bronze badges 3 |3 Answers
Reset to default 11For anyone with this problem, the problem was related to the new lines needed after the data. Basically, you need two lines and not one as I was using. That way it works everywhere.
Changing this:
return "data:Testing 1,2,3" + r.nextInt() +"\n";
To this:
return "data:Testing 1,2,3" + r.nextInt() +"\n\n";
Fixes the problem..
Instead of implementing manually SSE, be aware that Spring Framework 4.2+ supports natively SSE. See this sample SSE enabled controller returning an SseEmitter.
Ok, I have created a small example in PHP + HTML5.
You can take a look at it here. You will see that I have two different buttons; one to create the event source and the second one to disconnect it.
The code prints directly to firefox/chrome console. As you will see, the message handler is called in firefox but not chrome.
This is not working on any chrome I tested so far.
Example Firefox output:
Creating event source
Open
Id: 1334072077
Message: Se puede leer esto?
Origin: http://arancione-consulting.com
Closed
Open
Id: 1334072082
Message: Se puede leer esto?
Origin: http://arancione-consulting.com
Closed
Example Chrome output:
Creating event source
Open
Closed
Open
Closed
In case anyone wants to know, the server side code is:
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache'); // prevent caching of event data.
echo "id: " . time() . "\n";
echo "Event: time\n";
echo "data: Se puede leer esto?\n";
flush();
?>
Any ideas?
response.setContentType("text/event-stream");
? Maybe this content type is not recognised on XP? – nickdos Commented Apr 10, 2012 at 10:50