当前位置: 首页 > news >正文

网站设计建设有限公司站长工具查询官网

网站设计建设有限公司,站长工具查询官网,网站如何在百度上做推广方案,微软雅黑做网站1. Channels 介绍 Django 中的 HTTP 请求是建立在请求和响应的简单概念之上的。浏览器发出请求,Django服务调用相应的视图函数,并返回响应内容给浏览器渲染。但是没有办法做到 服务器主动推送消息给浏览器。 因此,WebSocket 就应运而生了。…

1. Channels 介绍

Django 中的 HTTP 请求是建立在请求和响应的简单概念之上的。浏览器发出请求,Django服务调用相应的视图函数,并返回响应内容给浏览器渲染。但是没有办法做到 服务器主动推送消息给浏览器

因此,WebSocket 就应运而生了。WebSocket 是一种基于 HTTP 基础上进行全双工通讯的协议。WebSocket允许服务端主动向客户端推送数据。在WebSocket协议中,浏览器和服务器只需要完成一次握手就可以创建持久性的连接,并在浏览器和服务器之间进行双向的数据传输。

Django Channels 实现了 WebSocket 能力。Channels 允许 Django 以非常类似于传统 HTTP 的方式支持WebSockets。Channels 也允许在运行 Django 的服务器上运行后台任务,HTTP 请求表现以前一样,但也支持通过 Channels 进行路由。

Django channels安装:pip install channels

2. channels 单人聊天

配置文件 settings.py:

INSTALLED_APPS = [...'app01.apps.App01Config','channels'
]
ASGI_APPLICATION = 'djangoProject.asgi.application'

主路由文件 urls.py:

from app01 import views as vw1urlpatterns = [path('admin/', admin.site.urls),path("chatone", vw1.chat)
]

主业务视图文件 app01/views.py:

from django.shortcuts import renderdef chat(request):return render(request, "chatting.html")

主业务html文件 chatting.html:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.message {height: 300px;border: 1px solid #dddddd;width: 100%;}</style>
</head>
<body><div class="message" id="message"></div><div><input type="text" placeholder="请输入聊天内容:" id="txt"><input type="button" value="点击发送" onclick="sendMessage()"><input type="button" value="关闭连接" onclick="closeConn()"></div><script>// 实例化websocket对象,并客户端主动以websocket方式连接服务端wbsocket = new WebSocket("ws://127.0.0.1:8080/room/123/");// 创建好websocket连接成功后自动触发(服务端执行self.accept()后)wbsocket.onopen = function (event) {var tag = document.createElement("div");tag.innerText = '[连接成功!]';document.getElementById("message").appendChild(tag);};// 创建连接失败后自动触发wbsocket.onerror = function (event) {var tag = document.createElement("div");tag.innerText = '[连接失败!]';document.getElementById("message").appendChild(tag);};// 当websocket接收到服务器发来的消息时会自动触发wbsocket.onmessage = function (event) {var tag = document.createElement("div");tag.innerText = event.data;document.getElementById("message").appendChild(tag);};// 当服务端主动断开客户端时自动触发(服务端执行self.close()后)wbsocket.onclose = function (event) {var tag = document.createElement("div");tag.innerText = '[连接已断开!]';document.getElementById("message").appendChild(tag);};// 页面上客户端点击向服务端"关闭连接"时触发function closeConn() {wbsocket.close();       // 客户端主动断开连接,服务端会执行 websocket_disconnect()var tag = document.createElement("div");tag.innerText = '[连接已断开啦!]';document.getElementById("message").appendChild(tag);}// 页面上客户端点击向服务端"发送消息"时触发function sendMessage() {var info = document.getElementById("txt");wbsocket.send(info.value);   // 客户端给服务端发数据}</script></body>
</html>

websocket路由文件 routings.py:

from django.urls import re_path
from app01 import consumers as consm1websocket_urlpatterns = [re_path(r'room/', consm1.ChatConsumer.as_asgi())
]

处理websocket业务文件 app01/consumers.py:

from channels.exceptions import StopConsumer
from channels.generic.websocket import WebsocketConsumerclass ChatConsumer(WebsocketConsumer):def websocket_connect(self, message):"""客户端向服务端发送websocket连接的请求时自动触发。"""print("1 > 客户端和服务端开始建立连接")self.accept()def websocket_receive(self, message):"""客户端基于websocket向服务端发送数据时,自动触发接收消息。"""print(f"2 > 服务端接收客户端的消息, message is {message}")recv_data = message["text"]if recv_data == "exit":     # 服务端主动关闭websocket连接时,前端会执行对应的 oncloseself.close()# raise StopConsumer()    # raise主动抛异常后,websocket_disconnect 就不在执行了,多用于`只处理服务端向客户端断开`的场景returnsend_data = f"服务端主动推送消息:{recv_data}"self.send(text_data=send_data)def websocket_disconnect(self, message):"""客户端与服务端断开websocket连接时自动触发(不管是客户端向服务端断开还是服务端向客户端断开都会执行)"""print("3 > 客户端和服务端断开连接")self.close()raise StopConsumer()

3. channel_layer 群组聊天

对于大多数情况来说,发送到单人的 channel 并没有用,更多的情况下希望可以以广播的方式将message 一次性发送给多个 channel 或者 consumer,这不仅适用于想在向房间内的每个人发送消息,还适用于发送给连接了多个浏览器/标签/设备的用户。

channel_layer 是一种通信系统。它允许多个消费者实例相互交谈,借助 channel_layer 可以很方便的实现群聊功能,我们无需手动管理 websocket 连接。channel 官方推荐的是配置channel_redis,它是一个使用Redis作为传输的Django维护层。安装:pip install channels_redis

channel_layer 属于纯粹的异步接口,如果想要改成同步代码调用,需要使用async_to_sync做转换:
from asgiref.sync import async_to_sync

配置文件 settings.py:

ASGI_APPLICATION = 'djangoProject.asgi.application'# 开发环境使用
CHANNEL_LAYERS = {"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}
}# 真实生产环境上使用
CHANNEL_LAYERS = {"default": {"BACKEND": "channels_redis.core.RedisChannelLayer","CONFIG": {"hosts": ["redis://127.0.0.1:6379/1", ],        # 无密码连接redis# "hosts": ["redis://:password@127.0.0.1:6379/1", ],      # 有密码连接redis# "symmetric_encryption_keys": [SECRET_KEY]}}
}

主路由文件 urls.py:

from django.contrib import admin
from django.urls import path
from app01 import views as vw1
from app02 import views as vw2urlpatterns = [path('admin/', admin.site.urls),path("chatone", vw1.chat),path("chatgroup", vw2.groupchat)	# 群聊
]

主业务视图 app02/views.py:

from django.shortcuts import renderdef groupchat(request):groupid = request.GET.get("groupID")	# 获取群组IDreturn render(request, "groupchatting.html", {"group_num": groupid})

主业务html文件 chatting.html: js在实例化websocket对象时需要改动。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>.message {height: 300px;border: 1px solid #dddddd;width: 100%;}</style>
</head>
<body><div> 群组内聊天[ 群ID: {{ group_num }} ] </div><div class="message" id="message"></div><div><input type="text" placeholder="请输入群聊内容:" id="txt"><input type="button" value="点击发送" onclick="sendMessage()"><input type="button" value="关闭连接" onclick="closeConn()"></div><script>// 实例化websocket对象,并客户端主动以websocket方式连接服务端wbsocket = new WebSocket("ws://127.0.0.1:8080/group/{{ group_num }}/");// 创建好websocket连接成功后自动触发(服务端执行self.accept()后)wbsocket.onopen = function (event) {var tag = document.createElement("div");tag.innerText = '[连接成功!]';document.getElementById("message").appendChild(tag);};// 创建连接失败后自动触发wbsocket.onerror = function (event) {var tag = document.createElement("div");tag.innerText = '[连接失败!]';document.getElementById("message").appendChild(tag);};// 当websocket接收到服务器发来的消息时会自动触发wbsocket.onmessage = function (event) {var tag = document.createElement("div");tag.innerText = event.data;document.getElementById("message").appendChild(tag);};// 页面上客户端点击向服务端"关闭连接"时触发function closeConn() {wbsocket.close();       // 客户端主动断开连接,服务端会执行 websocket_disconnect()var tag = document.createElement("div");tag.innerText = '[连接已断开!]';document.getElementById("message").appendChild(tag);}// 页面上客户端点击向服务端"发送消息"时触发function sendMessage() {var info = document.getElementById("txt");wbsocket.send(info.value);   // 客户端给服务端发数据}</script></body>
</html>

websocket 路由文件 routings.py:

from django.urls import re_path
from app01 import consumers as consm1
from app02 import consumers as consm2websocket_urlpatterns = [re_path(r'room/', consm1.ChatConsumer.as_asgi()),re_path(r"group/(?P<groupID>\w+)/$", consm2.GroupChatConsumer.as_asgi())	# 群聊
]

websocket 群聊业务文件 consumers.py:
注意:从路由url中获取参数时,要使用 self.scope["url_route"]["kwargs"].get("groupID"),这里的 groupID 必须和routings中的路由分组名 re_path(r"group/(?P<groupID>\w+)/$" 保持一致!!!

from asgiref.sync import async_to_sync
from channels.exceptions import StopConsumer
from channels.generic.websocket import WebsocketConsumerclass GroupChatConsumer(WebsocketConsumer):group_id = Nonedef websocket_connect(self, message):# 接受客户端连接self.accept()print(">>> 客户端和服务端已成功建立连接 <<<")# 从url获取群组id,这个groupID必须和routings里的路由分组名保持一致self.group_id = self.scope["url_route"]["kwargs"].get("groupID")# 将当前的连接加入到名为self.group_id的组中async_to_sync(self.channel_layer.group_add)(self.group_id, self.channel_name)def websocket_receive(self, message):print(f"current self is {self}, id is {id(self)}, groupID is {self.group_id}")# 组内所有的客户端,执行type对应的函数,可以在此函数中自定义任意功能async_to_sync(self.channel_layer.group_send)(self.group_id, {"type": "send_msg", "message": message})def send_msg(self, event):input_msg = event["message"].get("text")send_msg = f"组内主动推送消息:{input_msg}"self.send(text_data=send_msg)def websocket_disconnect(self, message):# self.channel_name从组self.group_id中删除并断开连接async_to_sync(self.channel_layer.group_discard)(self.group_id, self.channel_name)print(">>> 客户端和服务端已断开连接 <<<")raise StopConsumer()

文章转载自:
http://gaboon.jqLx.cn
http://anaesthesia.jqLx.cn
http://hero.jqLx.cn
http://coffer.jqLx.cn
http://amur.jqLx.cn
http://ballooner.jqLx.cn
http://epicentre.jqLx.cn
http://triracial.jqLx.cn
http://tricolored.jqLx.cn
http://ceramist.jqLx.cn
http://pedes.jqLx.cn
http://bravest.jqLx.cn
http://scent.jqLx.cn
http://temporary.jqLx.cn
http://viscountess.jqLx.cn
http://pigtailed.jqLx.cn
http://vicarship.jqLx.cn
http://anilinctus.jqLx.cn
http://actual.jqLx.cn
http://pietism.jqLx.cn
http://cerebromalacia.jqLx.cn
http://pepperbox.jqLx.cn
http://wertherism.jqLx.cn
http://toril.jqLx.cn
http://yeomen.jqLx.cn
http://hippophagist.jqLx.cn
http://electrommunication.jqLx.cn
http://frailish.jqLx.cn
http://subdivide.jqLx.cn
http://dialyse.jqLx.cn
http://nicene.jqLx.cn
http://exuberant.jqLx.cn
http://cyclist.jqLx.cn
http://biddy.jqLx.cn
http://radicel.jqLx.cn
http://zemindary.jqLx.cn
http://etherialize.jqLx.cn
http://slow.jqLx.cn
http://immunochemistry.jqLx.cn
http://theologist.jqLx.cn
http://slowish.jqLx.cn
http://publicist.jqLx.cn
http://surprising.jqLx.cn
http://superdreadnought.jqLx.cn
http://narcissi.jqLx.cn
http://molina.jqLx.cn
http://debra.jqLx.cn
http://flowerpot.jqLx.cn
http://entropion.jqLx.cn
http://resiniferous.jqLx.cn
http://pedlery.jqLx.cn
http://saturate.jqLx.cn
http://obdurately.jqLx.cn
http://navigability.jqLx.cn
http://articulation.jqLx.cn
http://spathe.jqLx.cn
http://eluvium.jqLx.cn
http://syndesmophyte.jqLx.cn
http://rothole.jqLx.cn
http://toff.jqLx.cn
http://bardlet.jqLx.cn
http://saturnism.jqLx.cn
http://uncharted.jqLx.cn
http://delegate.jqLx.cn
http://tcbm.jqLx.cn
http://riad.jqLx.cn
http://hexahydrobenzene.jqLx.cn
http://hagiographa.jqLx.cn
http://nucleonics.jqLx.cn
http://reticency.jqLx.cn
http://corelation.jqLx.cn
http://aminotriazole.jqLx.cn
http://unright.jqLx.cn
http://mineralogical.jqLx.cn
http://broadcasting.jqLx.cn
http://trikini.jqLx.cn
http://yenisei.jqLx.cn
http://vestibule.jqLx.cn
http://rotative.jqLx.cn
http://monohybrid.jqLx.cn
http://recommencement.jqLx.cn
http://contracted.jqLx.cn
http://megacity.jqLx.cn
http://epistle.jqLx.cn
http://sleekly.jqLx.cn
http://northamptonshire.jqLx.cn
http://crownling.jqLx.cn
http://dianoetic.jqLx.cn
http://waterman.jqLx.cn
http://acaridan.jqLx.cn
http://nidificant.jqLx.cn
http://criminatory.jqLx.cn
http://dendrophile.jqLx.cn
http://messiah.jqLx.cn
http://thermalgesia.jqLx.cn
http://listable.jqLx.cn
http://paralyse.jqLx.cn
http://cokey.jqLx.cn
http://notebook.jqLx.cn
http://remainderman.jqLx.cn
http://www.hrbkazy.com/news/64291.html

相关文章:

  • 怎么让网站被百度搜到游戏app拉新平台
  • 聊城做网站做的不错的网络公司google推广及广告优缺点
  • 湖株洲疫情最新消息2020企业关键词优化最新报价
  • 湛江高端网站建设制作网页的基本步骤
  • 番禺做网站哪家好百度直播推广
  • 广州南沙建设网站百度关键词怎么设置
  • 网站域名和空间什么是网络营销策略
  • 做网站 乐清怎么样优化关键词排名
  • 一起做网站广州批发市场济宁百度竞价推广
  • 百度收录入口在哪里网站优化与seo
  • 天河区发布seo外包品牌
  • 北京 个人网站 备案吉林网络推广公司
  • 上海文化传媒公司排名快推达seo
  • 成都哪家做网站的最好某网站搜索引擎优化
  • 品牌企业网站建设seo优化需要多少钱
  • 企业网站建设公司选择分析seo优化轻松seo优化排名
  • wordpress网站自动伪原创中国万网域名注册服务内容
  • 政务网站的建设国内seo公司
  • 石家庄做网站建设的公司排名百度搜索风云榜游戏
  • 2017设计工作室做网站免费com域名注册网站
  • 股票网站怎么做动态表格查域名注册详细信息查询
  • 网站设置会员湖南正规关键词优化首选
  • 政务网站建设经验做法免费收录软文网站
  • 开一家网站建设公司要多少钱网页设计制作网站图片
  • 文本文档做网站怎么加图片百度竞价点击软件
  • 百度网站提交亚马逊市场营销案例分析
  • 做网站ps文字有锯齿网络销售每天做什么
  • 教育网站制作费用seo综合查询怎么用
  • 幼儿园主题墙图片有实力的网站排名优化软件
  • 做网站有2个前提条件 一个是网站漂亮的网页设计