【问题标题】:Django Channels Realtime APIDjango 频道实时 API
【发布时间】:2021-02-22 08:42:39
【问题描述】:

所以,我正在为我的 React Native 应用程序使用 Django REST 框架构建一个 API。现在我想实现一个聊天功能,我想利用 Django Channels 通过 TCP 连接构建一个实时聊天功能。现在我读到我无法使用 Django Channels 构建实时 API。这是否意味着,我不能在我的服务器上使用 Django Channels 和在我的 React Native APP 中使用 this?:

var ws = new WebSocket('ws://host.com/path');

ws.onopen = () => {
  // connection opened
  ws.send('something'); // send a message
};

ws.onmessage = (e) => {
  // a message was received
  console.log(e.data);
};

ws.onerror = (e) => {
  // an error occurred
  console.log(e.message);
};

ws.onclose = (e) => {
  // connection closed
  console.log(e.code, e.reason);
};

我确实理解它,就像另一个版本的 HTTP(S) 连接或某种 :)

【问题讨论】:

    标签: django react-native real-time django-channels


    【解决方案1】:

    我将 Django Rest Framework 与 Angular 结合使用,我能够利用两者的强大功能。您只需将您的频道逻辑添加到您的项目中。然后,当您部署应用程序时,您将通过异步协议服务器 Daphne 代理传递您的 websocket 请求,然后通过像 Gunicorn 这样的 http 协议服务器代理传递您的 http 请求。在 github 上查看我的示例:https://github.com/ptuckett86/channel_chat

    【讨论】:

      【解决方案2】:

      回答


      Django REST Framework 是一个让事情变得不那么困难的框架。您也可以使用纯 Django 构建 API,但这并不容易。所以回答这个问题:

      • Python Django REST 框架让事情变得不那么困难
      • 您可以使用纯 Django 构建一个 REST API
      • 您可以使用 Django 构建 HTTP 或 TCP API

      信息

      Pure Django Django Rest Framework Django Channels
      Framework for building your backend. You can also build a REST Framework with pure Django, but it isn't that easy The REST Framework makes things easier. You can easily build a nice REST API for your App or Website Django Channels helps to build a Real-time API with Django -> It brings WebSockets to Django. So you haven't fetch data, instead, Django pushes them into your APP or Website (e.g. a chat)
      Can build a Rest API Can build a Rest API Can't build a Rest API
      Can't build a Websocket API Can't build a Websocket API Can build a Websocket API

      链接

      【讨论】:

        猜你喜欢
        • 2020-12-17
        • 1970-01-01
        • 2011-10-23
        • 1970-01-01
        • 2020-10-10
        • 1970-01-01
        • 2017-10-26
        • 2015-01-25
        • 2020-03-31
        相关资源
        最近更新 更多