Django
实现Websocket
使用Django
来实现Websocket
服务的方法很多在这里我们推荐技术最新的Channels
库来实现
DjangoChannels
Channels
安装如果你是Windows
操作系统的话,那么必要条件就是Python3.7
pip install channels
DjangoChannels
1.创建项目ChannelsReady
django-admin startprobject ChannelsReady
2.在项目的settings.py
同级目录中,新建文件routing.py
# routing.py
from channels.routing import ProtocolTypeRouter
application = ProtocolTypeRouter({
# 暂时为空
})
3.在项目配置文件settings.py
中写入
INSTALLED_APPS = [
'channels'
]
ASGI_APPLICATION = "ChannelsReady.routing.application"
Channels
提供的ASGI
的Django
项目You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
February 01, 2020 - 17:27:13
Django version 3.0.2, using settings 'ChannelsReady.settings'
Starting ASGI/Channels version 2.4.0 development server at http://0.0.0.0:8000/
Quit the server with CTRL-BREAK.
很明显可以看到ASGI/Channels
,这样就算启动完成了
Websocket
服务1.创建一个新的应用chats
python manage.py startapp chats
2.在settings.py
中注册chats
INSTALLED_APPS = [
'chats',
'channels'
]
3.在chats
应用中新建文件chatService.py
from channels.generic.websocket import WebsocketConsumer
# 这里除了 WebsocketConsumer 之外还有
# JsonWebsocketConsumer
# AsyncWebsocketConsumer
# AsyncJsonWebsocketConsumer
# WebsocketConsumer 与 JsonWebsocketConsumer 就是多了一个可以自动处理JSON的方法
# AsyncWebsocketConsumer 与 AsyncJsonWebsocketConsumer 也是多了一个JSON的方法
# AsyncWebsocketConsumer 与 WebsocketConsumer 才是重点
# 看名称似乎理解并不难 Async 无非就是异步带有 async / await
# 是的理解并没有错,但对与我们来说他们唯一不一样的地方,可能就是名字的长短了,用法是一模一样的
# 最夸张的是,基类是同一个,而且这个基类的方法也是Async异步的
class ChatService(WebsocketConsumer):
# 当Websocket创建连接时
def connect(self):
pass
# 当Websocket接收到消息时
def receive(self, text_data=None, bytes_data=None):
pass
# 当Websocket发生断开连接时
def disconnect(self, code):
pass
Websocket
处理对象增加路由1.在chats
应用中,新建urls.py
from django.urls import path
from chats.chatService import ChatService
websocket_url = [
path("ws/",ChatService)
]
2.回到项目routing.py
文件中增加ASGI
非HTTP
请求处理
from channels.routing import ProtocolTypeRouter,URLRouter
from chats.urls import websocket_url
application = ProtocolTypeRouter({
"websocket":URLRouter(
websocket_url
)
})
socket_list = []
class ChatService(WebsocketConsumer):
# 当Websocket创建连接时
def connect(self):
self.accept()
socket_list.append(self)
# 当Websocket接收到消息时
def receive(self, text_data=None, bytes_data=None):
print(text_data) # 打印收到的数据
for ws in socket_list: # 遍历所有的WebsocketConsumer对象
ws.send(text_data) # 对每一个WebsocketConsumer对象发送数据
from channels.generic.websocket import WebsocketConsumer
user_dict ={}
list = []
import json
class ChatService(WebsocketConsumer):
# 当Websocket创建连接时
def connect(self):
self.accept()
username = self.scope.get("url_route").get("kwargs").get("username")
user_dict[username] =self
print(user_dict)
# list.append(self)
# 当Websocket接收到消息时
def receive(self, text_data=None, bytes_data=None):
data = json.loads(text_data)
print(data)
to_user = data.get("to_user")
message = data.get("message")
ws = user_dict.get(to_user)
print(to_user)
print(message)
print(ws)
ws.send(text_data)
# 当Websocket发生断开连接时
def disconnect(self, code):
pass