Django将HTTP响应推送给用户 [英] Django Push HTTP Response to users

查看:234
本文介绍了Django将HTTP响应推送给用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个用Django编写的非常简单的Web应用程序,我想在我的应用程序中实现类似回调/推送通知服务。



例如:
当一个用户(客户端)将照片上传到服务器时,服务器会通知所有其他连接的用户该照片。



我想我可以使用Django信号在用户上传照片时产生回调,但是如何让Django向其他用户发布通知?此通知可以是警报或简单地将其他用户重定向到显示上传图片的新HTML。我更喜欢后者。



我是网络编程的初学者,所以我不知道这是否适合账单需要一个实时Web应用程序实现彗星或长轮询。我的应用程序类似于聊天应用程序,但我没有提交文本文件而是图像文件。因为我认为彗星解决方案会奏效。我已经尝试了很长一段时间,但没有运气与Django来实现它,可能是因为我不明白如何用彗星解决方案完成我想要的。我希望更有经验的程序员能够指出,为了完成这个任务,或者正在朝着正确的方向(用彗星),我需要什么。



如果有人可以给我一些关于如何进行的提示和提示,以及教程链接或指南,我真的很感激。

解决方案

HTTP本质上是一个拉协议 - 即客户机从服务器中提取数据,等待一段时间,然后稍后再提取更多数据。实际上没有严格的HTTP方式来从服务器将数据推送给客户端。



当您需要推送给客户端时,您基本上有三个选项。 / p>

(1)做轮询 - 使用Ajax / javascript在每X个时间轮询服务器。 X越是感觉像一样,而且您的服务器经历的时间越多,不断响应这些请求。



(2)使用< a href =http://dev.w3.org/html5/websockets/ =nofollow noreferrer> websockets 。 HTML5规范的一部分被称为websockets。 Websockets允许浏览器打开与服务器的持久连接。一旦这个连接被打开,数据可以像客户端到服务器和服务器一样来传递给客户端,就像传统的TCP套接字一样。 websockets(最后一次听到)的麻烦在于,它们在浏览器之间仍然有点温和,当然在旧版浏览器中也不会使用



(3)使用 Flash with a Javascript界面​​。 Flash可以设置持续TCP连接 ,可以像普通的TCP连接一样用来推/拉数据。 (请参阅此SO问题: Flex中的HTTP推送示例






如果从头开始这个项目,我建议你在 Node.js Socket.io 。 Socket.io是套接字框架,您可以编程,然后JavaScript客户端(它运行在您的Web浏览器中)智能地确定要使用的最佳持久连接 - 首先尝试使用Websockets,然后Flash,然后






但是,既然你说过你想使用Python / Django,那么你应该检查一下 Django-Websockets - 在Django中使用websockets的框架。但请务必阅读作者放在页面上的 ,与使用相关的一些重大困难/限制,主要是因为Django并未设计为使用Websockets



我认为最好的选择是在用户浏览器不支持Ajax Polling时使用具有智能回退功能的Websockets。


I currently have a very simple web application written in Django, and I would like to implement something like a callback/push notification service in my application.

For example: When one user(client) uploads a photo to the server, the server notifies all other connected users about that photo.

I suppose I can use Django signals to produce a callback when a user uploads a photo, but how do I get Django to post a notification to the other users? This notification could either be alerts or simply redirecting the other users to a new html that displays the uploaded picture. I would prefer the latter.

I am a beginner in web programming, so I am not sure if this fits the bill as needing a 'real-time web application' that implements things like comet or long-polling. My application is similar to that of a chat application, except that I am not submitting text files but image files. Because of that I thought comet solutions would work. I have tried looking at Orbited and Twisted for a very long time now but had no luck in implementing it with Django, probably because I do not understand how to accomplish what I want with comet solutions. I would like the more experienced programmers to point to me what is it exactly that I need in order to accomplish this, or if I am heading into the right direction or not (with comet).

I would really appreciate it if someone could give me some tips and hints as to how to proceed, as well as tutorial links or guides.

解决方案

HTTP is inherently a "pull" protocol--i.e., a client pulls data from a server, waits around for a while and then pulls more data later. There's actually no strictly HTTP way to "push" data to a client from a server.

You have basically three options when you need to "push" to a client.

(1) Do polling--use Ajax/javascript to poll the server every X amount of time. The smaller the X the more it "feels like" a push, but also the more overhead your server experiences having to constantly respond to these requests.

(2) Use websockets. Part of the HTML5 spec is something called websockets. Websockets allows a browser to open up a persistent connection to a server. Once this connetion has been opened data can be pushed back and forth from client to server and server to client just like with more traditional TCP sockets. The trouble with websockets (last I heard) was that they can still be a bit temperamental between browsers, and of course wont work at all in older browsers.

(3) Use Flash with a Javascript interface. Flash has the capability of setting up persistent TCP connections, which can be used to push/pull data just like with a 'normal' TCP connection. (See this SO question as well: HTTP push examples in Flex)


If you were starting this project from scratch I would recommend you write your backend in Node.js with Socket.io. Socket.io is "socket-like" framework that you can program to and then the Javascript client (which runs in your webbrowser) intelligently determines the best "persistent connection" to use--first it tries to use Websockets, then Flash, then long polling of various types.


But since you've said you want to use Python/Django then you should check out Django-Websockets--a framework for using websockets with Django. But be sure to read the Disclaimer the author puts on the page, there are some significant difficulties/limitations associated with using it, primarily because Django wasn't designed with websockets in mind.

I think your best bet will end up being using Websockets with an intelligent fallback to Ajax Polling when the user's browser doesn't support it.

这篇关于Django将HTTP响应推送给用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆