ws
This commit is contained in:
@@ -54,6 +54,13 @@ MIDDLEWARE = [
|
|||||||
# 指定websocket主路由的位置
|
# 指定websocket主路由的位置
|
||||||
ASGI_APPLICATION = 'jsmindmulti.asgi.application'
|
ASGI_APPLICATION = 'jsmindmulti.asgi.application'
|
||||||
|
|
||||||
|
# 指定websocket CHANNEL_LAYERS
|
||||||
|
CHANNEL_LAYERS = {
|
||||||
|
"default": {
|
||||||
|
"BACKEND": "channels.layers.InMemoryChannelLayer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ROOT_URLCONF = 'jsmindmulti.urls'
|
ROOT_URLCONF = 'jsmindmulti.urls'
|
||||||
|
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
|
|||||||
@@ -1,17 +1,41 @@
|
|||||||
import json
|
from asgiref.sync import async_to_sync
|
||||||
|
|
||||||
from channels.generic.websocket import WebsocketConsumer
|
from channels.generic.websocket import WebsocketConsumer
|
||||||
|
|
||||||
|
|
||||||
class ChatConsumer(WebsocketConsumer):
|
class ChatConsumer(WebsocketConsumer):
|
||||||
def connect(self):
|
def connect(self):
|
||||||
|
self.room_group_name = 'chat%s' % bytes.decode(self.scope['query_string'])
|
||||||
|
print(self.room_group_name)
|
||||||
|
|
||||||
|
# Join room group
|
||||||
|
async_to_sync(self.channel_layer.group_add)(
|
||||||
|
self.room_group_name,
|
||||||
|
self.channel_name
|
||||||
|
)
|
||||||
self.accept()
|
self.accept()
|
||||||
|
|
||||||
def disconnect(self, close_code):
|
def disconnect(self, close_code):
|
||||||
pass
|
# Leave room group
|
||||||
|
async_to_sync(self.channel_layer.group_discard)(
|
||||||
|
self.room_group_name,
|
||||||
|
self.channel_name
|
||||||
|
)
|
||||||
|
|
||||||
|
# Receive message from WebSocket
|
||||||
def receive(self, text_data):
|
def receive(self, text_data):
|
||||||
print('%s' % text_data)
|
# Send message to room group
|
||||||
self.send(text_data=json.dumps({
|
async_to_sync(self.channel_layer.group_send)(
|
||||||
'message': 'message'
|
self.room_group_name,
|
||||||
}))
|
{
|
||||||
|
# chat_message 方法
|
||||||
|
'type': 'chat_message',
|
||||||
|
'message': text_data
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Receive message from room group
|
||||||
|
def chat_message(self, event):
|
||||||
|
message = event['message']
|
||||||
|
|
||||||
|
# Send message to WebSocket
|
||||||
|
self.send(text_data=message)
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
<script>
|
<script>
|
||||||
const fetch = function () {
|
const fetch = function () {
|
||||||
this.jm = null
|
this.jm = null
|
||||||
|
this.coop_mind_id = null
|
||||||
}
|
}
|
||||||
// prototype
|
// prototype
|
||||||
fetch.prototype = {
|
fetch.prototype = {
|
||||||
@@ -36,6 +37,7 @@
|
|||||||
loadMind: function () {
|
loadMind: function () {
|
||||||
// 打开已有脑图1、查询数据,组装option 2、实例化jm、show
|
// 打开已有脑图1、查询数据,组装option 2、实例化jm、show
|
||||||
let id = {{ id }};
|
let id = {{ id }};
|
||||||
|
this.coop_mind_id = id
|
||||||
const me = this
|
const me = this
|
||||||
$.get('list?id=' + id, function (resp) {
|
$.get('list?id=' + id, function (resp) {
|
||||||
// reset
|
// reset
|
||||||
@@ -59,6 +61,7 @@
|
|||||||
theme: 'primary',
|
theme: 'primary',
|
||||||
coop_api_url: 'jsmind_op',
|
coop_api_url: 'jsmind_op',
|
||||||
coop_ws_url: `ws://${window.location.host}/ws/jsmind/`,
|
coop_ws_url: `ws://${window.location.host}/ws/jsmind/`,
|
||||||
|
coop_mind_id: this.coop_mind_id,
|
||||||
menuOpts: {
|
menuOpts: {
|
||||||
showMenu: true,
|
showMenu: true,
|
||||||
injectionList: [
|
injectionList: [
|
||||||
|
|||||||
@@ -68,7 +68,7 @@
|
|||||||
// 初始化websocket
|
// 初始化websocket
|
||||||
_init_ws: function () {
|
_init_ws: function () {
|
||||||
if (!this.web_socket) {
|
if (!this.web_socket) {
|
||||||
const ws = new WebSocket(this.coop_ws_url);
|
const ws = new WebSocket(this.coop_ws_url + '?_' + this.coop_mind_id)
|
||||||
this.web_socket = ws
|
this.web_socket = ws
|
||||||
//申请一个WebSocket对象,参数是服务端地址,同http协议使用http://开头一样,WebSocket协议的url使用ws://开头,另外安全的WebSocket协议使用wss://开头
|
//申请一个WebSocket对象,参数是服务端地址,同http协议使用http://开头一样,WebSocket协议的url使用ws://开头,另外安全的WebSocket协议使用wss://开头
|
||||||
ws.onopen = function () {
|
ws.onopen = function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user