如何解决Djnago导致StreamingHttp Response错误
我创建了一个django应用程序,用于在地图上流式传输ISS的坐标,这是生成流式HTTP响应的视图
def IssnowCoordinatesStream(request):
def IssDataStream(sleep_time = 1):
while True:
streamData = GenerateIssData()
yield 'data:{0}\n'.format(streamData.decode())
time.sleep(sleep_time)
response = StreamingHttpResponse(
IssDataStream(),status = 200,content_type = "text/event-stream"
)
response['Cache-Control'] = 'no-cache'
return response
这是[localhost / stream]中视图的输出
data:{"issnow_satid": 2447,"key": "e622c32c-b949-4abe-b3d8-ca75dbd8039e","timestamp": "2020-09-05 13:23:36.018709","message": "Success","longitude": -29.8005,"latitude": -28.4975,"speed": 27554,"altitude": 428}
并且从索引视图中,我正在尝试使用javascript
var source = new EventSource('/stream')
source.addEventListener('message',(e) => {
obj = JSON.parse(e.data)
console.log(obj);
},false);
但是它无法解析数据,并且终端上出现以下错误
Traceback (most recent call last):
File "C:\Python37\lib\wsgiref\handlers.py",line 138,in run
self.finish_response()
File "C:\Python37\lib\wsgiref\handlers.py",line 180,in finish_response
self.write(data)
File "C:\Python37\lib\wsgiref\handlers.py",line 279,in write
self._write(data)
File "C:\Python37\lib\wsgiref\handlers.py",line 453,in _write
result = self.stdout.write(data)
File "C:\Python37\lib\socketserver.py",line 799,in write
self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
[05/Sep/2020 18:45:52] "GET /streams HTTP/1.1" 500 59
任何人都可以帮助解决此错误。
注意 我浏览了网络,并尝试了Stackoverflow的所有可能的答案来解决此错误,但没有任何效果。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。