通过nodejs为socket.io提供django包
django-socketio-events的Python项目详细描述
###什么是Django Socketio活动?
只需将django代理到nodejs/socket io服务器后,即可将socket.io功能添加到django应用程序中。
在“app_folder”中定义的任何回调函数都将作为目标“my_app.callback”公开到socket.io。请参见下面的示例。
\python manage.py runserverjs<;hostname>;<;public port>;<;proxy port>;<;socket port>;
````
在任何需要socket io连接的django模板中,将此模板标记添加到头部:
```
{%load nodjango%}
<;!
<;lt;html>;
<;lt;head>;
<;lt;lt;lt;script type=“text/javascript”src=///code.jquery.com/jquery-2.1.1.min.js”>;lt;lt;lt;script>;
{%socketio-head-head-read-head-read-head>;
<;
>;
>;lt;lt;lt;lt;lt;lt;gt;
>;
<;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;(一)有权使用;
socket.on('my_callback',function(data){
console.log(json.stringify(data));
$('button')。on('click',function(){
socket.emit('django',{
'event':'my_app.change_color',
'callback':'my_callback',
'color': 'red'
});
});
</script>
</body>
</html>
```
```
# my_app/socketio.py
def change_color(color):
# change color
# ...
# profit
return {'result': 42}
```
### Installation
```
pip安装django socketio events
``
,然后将“nodjango”添加到“已安装的应用程序”中。
只需将django代理到nodejs/socket io服务器后,即可将socket.io功能添加到django应用程序中。
在“app_folder”中定义的任何回调函数都将作为目标“my_app.callback”公开到socket.io。请参见下面的示例。
\python manage.py runserverjs<;hostname>;<;public port>;<;proxy port>;<;socket port>;
````
在任何需要socket io连接的django模板中,将此模板标记添加到头部:
```
{%load nodjango%}
<;!
<;lt;html>;
<;lt;head>;
<;lt;lt;lt;script type=“text/javascript”src=///code.jquery.com/jquery-2.1.1.min.js”>;lt;lt;lt;script>;
{%socketio-head-head-read-head-read-head>;
<;
>;
>;lt;lt;lt;lt;lt;lt;gt;
>;
<;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;lt;(一)有权使用;
socket.on('my_callback',function(data){
console.log(json.stringify(data));
$('button')。on('click',function(){
socket.emit('django',{
'event':'my_app.change_color',
'callback':'my_callback',
'color': 'red'
});
});
</script>
</body>
</html>
```
```
# my_app/socketio.py
def change_color(color):
# change color
# ...
# profit
return {'result': 42}
```
### Installation
```
pip安装django socketio events
``
,然后将“nodjango”添加到“已安装的应用程序”中。