[英]Connect to a remote socket.io server from Django server
I am trying to build a real-time Django application. 我正在尝试构建一个实时Django应用程序。 Because of the way my hosting service works, I am unable to start a Websocket server in parallel of my Django server. 由于托管服务的工作方式,我无法与Django服务器并行启动Websocket服务器。
I managed to have user-to-user interactions by creating an express server on a separate NodeJS website with socket.io , and having clients on the Django server also connect to the remote socket.io server. 我通过使用socket.io在单独的NodeJS网站上创建了一个快速服务器,并使Django服务器上的客户端也连接到远程socket.io服务器,从而实现了用户之间的交互。
However, I'dlike to have my Django server directly send events to users. 但是,我想让我的Django服务器直接将事件发送给用户。 To do this, I would like to create a connection between the Django server and the NodeJS server. 为此,我想在Django服务器和NodeJS服务器之间创建一个连接。 Something like that in python: 在python中类似的东西:
socket = io("http://socket.io.server")
socket.emit('eventForUsers')
Is there anyway for me to achieve this? 反正我有实现这一目标的方法吗?
The only information I found seemed to require me to run a parallel server from my Django app, which I can't do because my host doesn't allow me to run long-term processes. 我发现的唯一信息似乎要求我从Django应用程序运行并行服务器,而我不能这样做,因为主机不允许我运行长期进程。
It really depends what is the most simple solution for you, and what are your requirements. 这实际上取决于什么是最简单的解决方案以及您的要求。 (If you want realtime bidirectional messaging then I suggest to use the socket.io-client instead of the POST
example) (如果您想要实时双向消息传递,那么我建议使用socket.io-client而不是POST
示例)
You can (for example) use POST
requests from your Django to Node 您可以(例如)使用从Django到Node的POST
请求
Django: python
example (there are other ways to perform a POST to Node from Django) Django: python
示例(还有其他方法可以从Django执行POST到Node)
reqParams = {"action":"doThis","data":"put the pizza in the oven"}
import requests
requests.post('http://ip:port/route', params = reqParams)
Node example : Listens for post at /route
(express) and prints the params of the Django request 节点示例:在/route
(快速)上侦听帖子,并输出Django请求的参数
var express = require('express');
var app = express();
app.post('/route', function (req, res) {
console.log(req.query); res.end();
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Then you can use the data in req.params
to perform action like broadcasting something to the socket.io
clients (or a specific client) 然后,您可以使用req.params
的数据执行操作,例如向socket.io
客户端(或特定客户端)广播内容
This can also be done the other way around, performing requests to send data from Node to Django using POST (GET,...)
这也可以通过另一种方式来完成,即执行请求以使用POST (GET,...)
将数据从Node发送到Django POST (GET,...)
Socket.io-client
in Django Django中的Socket.io-client
Another (easier) solution is to include the socket.io-client
in your Django webapp so it connects to your Node socket.io
server as a client when the webapp is opened by browsers. 另一个(简便)的解决方案是将socket.io-client
包含在Django Web应用程序中,以便在浏览器打开Web应用程序时将其作为客户端连接到Node socket.io
服务器。
(using javascript in django) (在django中使用javascript)
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://ip:port');
socket.on('connect', function(){alert("Hello World!");});
socket.on('event', function(data){});
socket.on('disconnect', function(){});
</script>
More Resources : 1. JavaScript (Django Docs) 2. Including the static files in your template 更多资源: 1. JavaScript(Django Docs) 2. 在模板中包含静态文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.