Django 實現(xiàn) Websocket 廣播、點對點發(fā)送消息的代碼
1.Django實現(xiàn)Websocket
使用Django來實現(xiàn)Websocket服務(wù)的方法很多在這里我們推薦技術(shù)最新的Channels庫來實現(xiàn)
1.1.安裝DjangoChannels
Channels安裝如果你是Windows操作系統(tǒng)的話,那么必要條件就是Python3.7
pip install channels
1.2.配置DjangoChannels
1.創(chuàng)建項目ChannelsReady
django-admin startprobject ChannelsReady
2.在項目的settings.py同級目錄中,新建文件routing.py
# routing.pyfrom channels.routing import ProtocolTypeRouterapplication = ProtocolTypeRouter({ # 暫時為空})
3.在項目配置文件settings.py中寫入
INSTALLED_APPS = [ ’channels’]ASGI_APPLICATION = 'ChannelsReady.routing.application'
1.3.啟動帶有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:13Django 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,這樣就算啟動完成了
1.4.創(chuàng)建Websocket服務(wù)
1.創(chuàng)建一個新的應(yīng)用chats
python manage.py startapp chats
2.在settings.py中注冊chats
INSTALLED_APPS = [ ’chats’, ’channels’]
3.在chats應(yīng)用中新建文件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): # 當(dāng)Websocket創(chuàng)建連接時 def connect(self): pass # 當(dāng)Websocket接收到消息時 def receive(self, text_data=None, bytes_data=None): pass # 當(dāng)Websocket發(fā)生斷開連接時 def disconnect(self, code): pass
1.5.為Websocket處理對象增加路由
1.在chats應(yīng)用中,新建urls.py
from django.urls import pathfrom chats.chatService import ChatServicewebsocket_url = [ path('ws/',ChatService)]
2.回到項目routing.py文件中增加ASGI非HTTP請求處理
from channels.routing import ProtocolTypeRouter,URLRouterfrom chats.urls import websocket_urlapplication = ProtocolTypeRouter({ 'websocket':URLRouter( websocket_url )})
總結(jié):
下載 注冊到setting.py里的app 在setting.py同級的目錄下注冊channels使用的路由----->routing.py 將routing.py注冊到setting.py 把urls.py的路由注冊到routing.py里 編寫wsserver.py來處理websocket請求<template> <div> <input type='text' v-model='message'> <p><input type='button' @click='send' value='發(fā)送'></p> <p><input type='button' @click='close_socket' value='關(guān)閉'></p> </div></template><script>export default { name:’websocket1’, data() { return { message:’’, testsocket:’’ } }, methods:{ send(){ // send 發(fā)送信息 // close 關(guān)閉連接 this.testsocket.send(this.message) this.testsocket.onmessage = (res) => { console.log('WS的返回結(jié)果',res.data); } }, close_socket(){ this.testsocket.close() } }, mounted(){ this.testsocket = new WebSocket('ws://127.0.0.1:8000/ws/') // onopen 定義打開時的函數(shù) // onclose 定義關(guān)閉時的函數(shù) // onmessage 定義接收數(shù)據(jù)時候的函數(shù) // this.testsocket.onopen = function(){ // console.log('開始連接socket') // }, // this.testsocket.onclose = function(){ // console.log('socket連接已經(jīng)關(guān)閉') // } }}</script>
3.廣播消息
3.1客戶端保持不變,同時打開多個客戶端
3.2服務(wù)端存儲每個鏈接的對象
socket_list = []class ChatService(WebsocketConsumer): # 當(dāng)Websocket創(chuàng)建連接時 def connect(self): self.accept() socket_list.append(self) # 當(dāng)Websocket接收到消息時 def receive(self, text_data=None, bytes_data=None): print(text_data) # 打印收到的數(shù)據(jù) for ws in socket_list: # 遍歷所有的WebsocketConsumer對象 ws.send(text_data) # 對每一個WebsocketConsumer對象發(fā)送數(shù)據(jù)
4.點對點消息
4.1客戶端將用戶名拼接到url,并在發(fā)送的消息里指明要發(fā)送的對象
<template> <div> <input type='text' v-model='message'> <input type='text' v-model='user'> <p><input type='button' @click='send' value='發(fā)送'></p> <p><input type='button' @click='close_socket' value='關(guān)閉'></p> </div></template><script>export default { name:’websocket1’, data() { return { message:’’, testsocket:’’, user:’’ } }, methods:{ send(){ // send 發(fā)送信息 // close 關(guān)閉連接 var data1 = {'message':this.message,'to_user':this.user} this.testsocket.send(JSON.stringify(data1)) this.testsocket.onmessage = (res) => { console.log('WS的返回結(jié)果',res.data); } }, close_socket(){ this.testsocket.close() }, generate_uuid: function() { var d = new Date().getTime(); if (window.performance && typeof window.performance.now === 'function') { d += performance.now(); //use high-precision timer if available } var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( /[xy]/g, function(c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16); } ); return uuid; }, }, mounted(){ var username = this.generate_uuid(); console.log(username) this.testsocket = new WebSocket('ws://127.0.0.1:8000/ws/'+ username +'/') console.log(this.testsocket) this.testsocket.onmessage = (res) => { console.log('WS的返回結(jié)果',res.data); } // onopen 定義打開時的函數(shù) // onclose 定義關(guān)閉時的函數(shù) // onmessage 定義接收數(shù)據(jù)時候的函數(shù) // this.testsocket.onopen = function(){ // console.log('開始連接socket') // }, // this.testsocket.onclose = function(){ // console.log('socket連接已經(jīng)關(guān)閉') // } }}</script>
4.2服務(wù)端存儲用戶名以及websocketConsumer,然后給對應(yīng)的用戶發(fā)送信息
from channels.generic.websocket import WebsocketConsumeruser_dict ={}list = []import jsonclass ChatService(WebsocketConsumer): # 當(dāng)Websocket創(chuàng)建連接時 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) # 當(dāng)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) # 當(dāng)Websocket發(fā)生斷開連接時 def disconnect(self, code): pass
總結(jié)
到此這篇關(guān)于Django 實現(xiàn) Websocket 廣播、點對點發(fā)送消息的文章就介紹到這了,更多相關(guān)Django 實現(xiàn) Websocket 廣播、點對點發(fā)送消息內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. Python 操作 MySQL數(shù)據(jù)庫3. Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實現(xiàn)教程4. 開發(fā)效率翻倍的Web API使用技巧5. bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動輸入功能實現(xiàn)代碼8. 什么是Python變量作用域9. vue使用moment如何將時間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時間格式10. python 如何在 Matplotlib 中繪制垂直線
