我正在测试HTML5服务器发送事件,看是否可以在我的项目中使用。我尝试按照http://www.html5rocks.com/en/tutorials/eventsource/basics和http://www.w3schools.com/html/html5_serversentevents.asp上的教程进行操作,但无法使其工作。
这是我的HTML(sse.html)和Python CGI脚本(sse.py):
以下是 sse.py 文件内容:
我将'sse.html'放入'c:\code\sse'文件夹中,将'sse.py'放到'c:\code\sse\cgi-bin'文件夹中。然后,我运行了'python -m CGIHTTPServer'来启动开发 Web 服务器。
当我连接到'http://localhost:8000/sse.html'时,我没有看到控制台记录从服务器接收的数据。相反,我看到了一条消息,上面写着"Resource interpreted as Script but transferred with MIME type text/plain: "chrome-extension://hgimnogjllphhhkhlmebbmlgjoejdpjl/bar.js"."。
有人知道问题出在哪里吗?
谢谢!
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var source = new EventSource("/cgi-bin/sse.py");
source.onmessage = function(event) {
console.log(event.data);
};
</script>
</head>
<body>
Testing SSE!
</div>
</body>
</html>
以下是 sse.py 文件内容:
import sys
import datetime
import time
sys.stdout.write('Content-type: text/event-stream\r\n\r\n')
while True:
now = datetime.datetime.now()
sys.stdout.write('data: %s\r\n\r\n' % now)
sys.stdout.flush()
time.sleep(5)
我将'sse.html'放入'c:\code\sse'文件夹中,将'sse.py'放到'c:\code\sse\cgi-bin'文件夹中。然后,我运行了'python -m CGIHTTPServer'来启动开发 Web 服务器。
当我连接到'http://localhost:8000/sse.html'时,我没有看到控制台记录从服务器接收的数据。相反,我看到了一条消息,上面写着"Resource interpreted as Script but transferred with MIME type text/plain: "chrome-extension://hgimnogjllphhhkhlmebbmlgjoejdpjl/bar.js"."。
有人知道问题出在哪里吗?
谢谢!