跳转到主要内容

Django channels与Redux之间的可重用桥梁

项目描述

Django channels与Redux之间的可重用桥梁。

快速入门

$ pip install django_redux
$ npm install django-channels django_redux

为您的项目创建一个名为 engine.py 的文件

from django_redux import action, AsyncReduxConsumer


class MyConsumer(AsyncReduxConsumer):

    async def connect(self, message):
        if message.user.is_authenticated:
            await self.send_json({
                'type': 'SET_USER',
                'user': {
                    'username': self.message.user.username,
                }
            })

    # This method will be called when the `INCREMENT_COUNTER` action gets
    # fired from the JS via the reduxBridge (see below).
    @action('INCREMENT_COUNTER')
    async def incr_counter(self, message):
        await self.send_json({'type': 'INCREMENTED_COUNTER', 'incrementBy': message['incrementBy']})

在您的js入口点

// app.js

import React from 'react';

import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, } from 'redux';

import reducer from '../reducers';
import Root from '../containers/Root.react';

import { WebSocketBridge } from 'django-channels';
import { eventToAction } from 'django_redux';

const store = createStore(
  reducer,
);


export const reduxBridge = new WebSocketBridge();
reduxBridge.connect("ws://:8000/ws/");
reduxBridge.addEventListener("message", eventToAction(store));

render(
  <Provider store={store}>
    <Root />
  </Provider>,
  document.getElementById('root')
);

从redux发送一个动作

import { createAction } from 'redux-actions';

import ActionTypes from './constants';
import { reduxBridge } from './app';


export const incrementCounter = createAction(ActionTypes.INCREMENT_COUNTER, (incrementBy) => {
  reduxBridge.send({
    type: ActionTypes.INCREMENT_COUNTER,
    incrementBy
  });
});

从后端发送一个动作

from django_redux import send_action

await send_action('mygroup', {
    'type': 'ACTION_NAME',
    'payload': {'any': 'thing'},
})

所有客户端都会自动添加到一个名为 “broadcast” 的组。

认证用户会自动添加到一个名为 “user.{user.pk}” 的组,以便您可以方便地引用。

待办事项

  • 测试
    • send_action

  • 数据绑定

  • 文档
    • 多路复用

致谢

大部分代码是从johnpaulett/channel_chat改编的。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

django_redux-1.0.0.tar.gz (3.6 kB 查看哈希值)

上传于 源代码

构建分发版

django_redux-1.0.0-py2.py3-none-any.whl (135.9 kB 查看哈希值)

上传于 Python 2 Python 3

由以下机构支持