ws
This commit is contained in:
@@ -11,9 +11,7 @@ class ChatConsumer(WebsocketConsumer):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def receive(self, text_data):
|
def receive(self, text_data):
|
||||||
text_data_json = json.loads(text_data)
|
print('%s' % text_data)
|
||||||
message = text_data_json['message']
|
|
||||||
|
|
||||||
self.send(text_data=json.dumps({
|
self.send(text_data=json.dumps({
|
||||||
'message': message
|
'message': 'message'
|
||||||
}))
|
}))
|
||||||
|
|||||||
@@ -58,7 +58,7 @@
|
|||||||
editable: true,
|
editable: true,
|
||||||
theme: 'primary',
|
theme: 'primary',
|
||||||
coop_api_url: 'jsmind_op',
|
coop_api_url: 'jsmind_op',
|
||||||
coop_ws_url: 'ws://localhost:8000/ws/jsmind/',
|
coop_ws_url: `ws://${window.location.host}/ws/jsmind/`,
|
||||||
menuOpts: {
|
menuOpts: {
|
||||||
showMenu: true,
|
showMenu: true,
|
||||||
injectionList: [
|
injectionList: [
|
||||||
@@ -93,7 +93,6 @@
|
|||||||
}
|
}
|
||||||
const xxObj = new fetch()
|
const xxObj = new fetch()
|
||||||
xxObj.loadMind()
|
xxObj.loadMind()
|
||||||
xxObj.loadMind()
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
@@ -40,6 +40,8 @@
|
|||||||
timer_push_task: null,
|
timer_push_task: null,
|
||||||
// 定时获得变化
|
// 定时获得变化
|
||||||
timer_fetch_task: null,
|
timer_fetch_task: null,
|
||||||
|
// web_socket
|
||||||
|
web_socket: null,
|
||||||
// init
|
// init
|
||||||
_init: function (_jm) {
|
_init: function (_jm) {
|
||||||
if (!this._jm) {
|
if (!this._jm) {
|
||||||
@@ -65,7 +67,9 @@
|
|||||||
},
|
},
|
||||||
// 初始化websocket
|
// 初始化websocket
|
||||||
_init_ws: function () {
|
_init_ws: function () {
|
||||||
|
if (!this.web_socket) {
|
||||||
const ws = new WebSocket(this.coop_ws_url);
|
const ws = new WebSocket(this.coop_ws_url);
|
||||||
|
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 () {
|
||||||
//当WebSocket创建成功时,触发onopen事件
|
//当WebSocket创建成功时,触发onopen事件
|
||||||
@@ -79,10 +83,13 @@
|
|||||||
ws.onclose = function (e) {
|
ws.onclose = function (e) {
|
||||||
//当客户端收到服务端发送的关闭连接请求时,触发onclose事件
|
//当客户端收到服务端发送的关闭连接请求时,触发onclose事件
|
||||||
console.log("close");
|
console.log("close");
|
||||||
|
this.web_socket = null
|
||||||
}
|
}
|
||||||
ws.onerror = function (e) {
|
ws.onerror = function (e) {
|
||||||
//如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
|
//如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
this.web_socket = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// 清理定时任务
|
// 清理定时任务
|
||||||
|
|||||||
Reference in New Issue
Block a user