简体   繁体   English

服务器已发送事件无法在Google Chrome中使用

[英]Server Sent Event not working in google chrome

This is my server JSP code "Server_Date.jsp" 这是我的服务器JSP代码“Server_Date.jsp”

<%
response.setHeader("cache-control", "no-cache"); 
response.setContentType("text/event-stream");
out.print("data: " + (new java.util.Date()).toString() + "x\n\n");
out.flush();   
%>

This my client jsp page "Client_Serverdate.jsp" 这是我的客户端jsp页面“Client_Serverdate.jsp”

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body  onload="begin()">
<h1>Getting server updates</h1>
<div id="result"></div>
<script >
if(typeof(EventSource)!=="undefined")
{     
var source=new EventSource("Server_Date.jsp");      
source.addEventListener("message", function(event) {
    document.getElementById("result").innerHTML = event.data;
}, false);
}
else
{
document.getElementById("result").innerHTML="Sorry, your browser does not support server-sent events...";
}
</script>   
</body>
</html>

Please help me this code is working fine in Mozilla Firefox, opera but not working in Google chrome i was checked with 18.x and 20.x also 请帮助我这个代码在Mozilla Firefox中工作正常,歌剧但不在Google Chrome中工作我用18.x和20.x也检查了

It is going to server page i have checked with print statement but its not coming to the line "document.getElementById("result").innerHTML = event.data;" 它将转到我用print语句检查的服务器页面,但它没有到达“document.getElementById(”result“)行.internalHTML = event.data;”

Thanks... 谢谢...

For one you are calling a function begin() that is not defined although that should not be the issue here. 对于你正在调用一个未定义的函数begin() ,虽然这不应该是问题。

Does the Chrome development console show any errors? Chrome开发控制台是否显示任何错误? It should show at least one. 它应该显示至少一个。 because of the begin function. 因为开始功能。 And does the network tab show traffic to Server_Date.jsp? 网络选项卡是否显示Server_Date.jsp的流量?

I used to have the same issue. 我曾经有同样的问题。 As server part, I used PHP, but I guess it works the same. 作为服务器部分,我使用PHP,但我猜它的工作方式相同。 What fixed it for me was adding ob_flush() . 为我修复的是添加ob_flush() Now, I don't know what it should be in your language, but maybe it helps you in the right direction. 现在,我不知道你的语言应该是什么,但也许它可以帮助你朝着正确的方向前进。

I've met the same problem, and i resolved it by add a newline at the end of the servlet. 我遇到了同样的问题,我通过在servlet的末尾添加一个换行符来解决它。 like this: 像这样:

response.setContentType("text/event-stream;charset=UTF-8");
response.addHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.println("data: " + new Date());
out.println();
out.flush();
out.close();

Problem was solved 问题解决了

Solution : 方案:

Page encoding problem : client side used UTF-8 Encoding 页面编码问题:客户端使用UTF-8编码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

Server side UTF-8 was not mention, so after adding charset=UTF-8 in setContentType its working 没有提到服务器端的UTF-8,所以在setContentType中添加charset = UTF-8后就可以了

response.setContentType("text/event-stream;charset=UTF-8");

Thanks for people take your effort to answer my question 感谢您的努力回答我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM