Django 将 HTTP 响应推送给用户

发布于 2024-10-28 07:24:11 字数 524 浏览 0 评论 0 原文

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

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

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

我是网络编程的初学者,所以我不确定这是否符合要求,因为需要一个实现彗星或长轮询等功能的“实时网络应用程序”。我的应用程序与聊天应用程序类似,只是我不提交文本文件而是图像文件。因此,我认为彗星解决方案会起作用。我已经尝试查看 Orbited 和 Twisted 很长时间了,但没有成功地使用 Django 实现它,可能是因为我不明白如何使用 comet 解决方案来实现我想要的目标。我希望更有经验的程序员指出我到底需要什么才能实现这一目标,或者我是否正朝着正确的方向前进(使用彗星)。

如果有人能给我一些关于如何继续的提示和提示,以及教程链接或指南,我将非常感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上的星空 2024-11-04 07:24:12

如果您使用过 nginx,这是一个不错的选择:),您可以使用推送模块 http://pushmodule.slact。 net/,我发现它相当容易使用。
您有一个用于在通道上发布消息的 URL(这可以在 python 中轻松完成,例如使用 httplib),以及一个用于从通道中提取消息的 URL(并且一个通道可能由多个用户使用)。另请参阅 http://blog.jamieisaacs.com/ 2010/08/27/comet-with-nginx-and-jquery/ 用于 jquery 集成。

If ever you use nginx, which is a good choice :), you may use the push module http://pushmodule.slact.net/, I found it rather easy to use.
You have one URL to publish messages on a channel (that can be done easily in python, with httplib for example), and one URL to pull messages from a channel (and a channel may be used by more than one user). See also http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/ for a jquery integration.

呆橘 2024-11-04 07:24:11

HTTP 本质上是一个“拉”协议——即,客户端从服务器拉数据,等待一段时间,然后拉更多数据。实际上没有严格的 HTTP 方式将数据从服务器“推送”到客户端。

当您需要“推送”给客户时,您基本上有三种选择。

(1) 进行轮询——使用 Ajax/javascript 每 X 时间轮询一次服务器。 X 越小,“感觉”越像推送,但服务器必须不断响应这些请求的开销也越大。

(2) 使用websockets。 HTML5 规范的一部分称为 websockets。 Websockets 允许浏览器打开与服务器的持久连接。一旦打开此连接,数据就可以从客户端到服务器以及从服务器到客户端来回推送,就像更传统的 TCP 套接字一样。 Websockets 的问题(我上次听说)是它们在浏览器之间仍然有点不稳定,当然在旧版浏览器中根本无法工作。

(3) 使用带有Javascript接口的Flash。 Flash 能够设置持久 TCP 连接,它可用于推送/拉取数据,就像使用“普通”TCP 连接一样。 (另请参阅这个问题:Flex 中的 HTTP 推送示例


如果您开始从头开始这个项目我建议您使用 Node.js 中编写后端.io/" rel="noreferrer">Socket.io。 Socket.io 是“类套接字”框架,您可以对其进行编程,然后 Javascript 客户端(在您的网络浏览器中运行)智能地确定要使用的最佳“持久连接”——首先它尝试使用 Websockets,然后是 Flash,然后各种类型的长轮询。


但既然你已经说过你想使用 Python/Django 那么你应该查看 Django-Websockets--在 Django 中使用 websockets 的框架。但请务必阅读作者在页面上放置的免责声明,使用它会遇到一些重大困难/限制,主要是因为 Django 在设计时并未使用 websockets头脑。

我认为最好的选择最终是使用 Websockets,当用户的浏览器不支持 Ajax 轮询时,智能回退到 Ajax 轮询。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文