简体   繁体   English

如何通过Django通道WebSocket传递请求并调用Django视图

[英]How to pass request over Django channels WebSocket and call Django view

I'm working on a single page application with Django, and would like to use WebSockets, and therefore Channels. 我正在使用Django处理单页应用程序,并且想使用WebSockets,因此要使用Channels。 To keep things simple, I think I want to handle all server communication over a WebSocket alone, rather than adding XHR (XML HTTP Request) into the mix. 为简单起见,我想我想仅通过WebSocket处理所有服务器通信,而不是在混合中添加XHR(XML HTTP请求)。 I'm using channels from the get-go since there will be a lot of data pushed from the server to the client asynchronously. 我从一开始就使用通道,因为会有很多数据从服务器异步推送到客户端。

With regular Django, a conventional request made to https://example.com/login or https://example.com/logout or whatever and the Django URL router will decide what view to send it to. 对于常规Django,通常会向https://example.com/loginhttps://example.com/logout或其他内容发出请求,而Django URL路由器将决定将其发送到哪个视图。 Instead, I would like to have the user perform their action in the client, handle it with Javascript, and use the WebSocket to send the request to the server. 相反,我希望用户在客户端中执行其操作,使用Javascript处理它,并使用WebSocket将请求发送到服务器。 Since I'm using Django-allauth, I would like to use the provided Django views to handle things like authentication. 由于我使用的是Django-allauth,因此我想使用提供的Django视图来处理身份验证之类的事情。 The server would then update the client with the necessary state information from the view. 然后,服务器将使用视图中的必要状态信息更新客户端。

My question : how can I process the data received over the WebSocket and submit the HTTP request to the Django view? 我的问题 :如何处理通过WebSocket接收的数据并将HTTP请求提交到Django视图? My channels consumer would then take the rendered HTML and send it back to the client to update the page or section. 然后,我的频道使用者将获取呈现的HTML,并将其发送回客户端以更新页面或部分。

I can picture what would happen using XHR, but I'm trying to avoid mixing the two, unless someone can point out the usefulness in using XHR plus WebSockets...? 我可以想象使用XHR会发生什么,但是我试图避免将两者混为一谈,除非有人能指出使用XHR和WebSockets的有用性……? I suppose another option is to use XHR for authentication and other client initiated requests, and use the WebSocket for asynchronously updating the client. 我想另一个选择是使用XHR进行身份验证和其他客户端发起的请求,并使用WebSocket异步更新客户端。 Does this make any sense at all? 这一点有意义吗?

Update : It occurs to me that I could use requests from PyPi, and make an sync_to_async call to localhost using credentials I received over the WebSocket. 更新 :在我看来,我可以使用来自PyPi的requests ,并使用通过WebSocket收到的凭据对本地主机进行sync_to_async调用。 However, this would require me to then handle the session data and send it back to the client. 但是,这将需要我处理会话数据并将其发送回客户端。 This seems like a lot more work. 这似乎需要做很多工作。 That said, I could maintain the sessions themselves on the server and just associate them with the WebSocket connection itself. 也就是说,我可以将会话本身保留在服务器上,而只需将它们与WebSocket连接本身相关联即可。 Since I'm using a secure WebSocket wss:// is there any possibility for hijacking the WebSocket connection? 由于我使用的是安全的WebSocket wss://是否有可能劫持WebSocket连接?

Check out this project that gives the ability to process a channels websocket request using Django Rest Framework views. 签出此项目该项目使您能够使用Django Rest Framework视图处理Channels Websocket请求。 You can try to adapt it to a normal Django view. 您可以尝试使其适应普通的Django视图。

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

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