diff --git a/static/html/detail.html b/static/html/detail.html index ec76b8c..22e5704 100644 --- a/static/html/detail.html +++ b/static/html/detail.html @@ -57,7 +57,8 @@ container: 'jsmind_container', editable: true, theme: 'primary', - coop_api_url: null, + coop_api_url: 'jsmind_op', + coop_ws_url: 'ws://localhost:8000/ws/jsmind/', menuOpts: { showMenu: true, injectionList: [ diff --git a/static/js/jsmind.coop.js b/static/js/jsmind.coop.js index 3d1d008..d73fb4c 100644 --- a/static/js/jsmind.coop.js +++ b/static/js/jsmind.coop.js @@ -11,30 +11,23 @@ function jsMindCallBackFacade(type, body) { const coop = $w.coop - coop.jsMindCallBack_(type, body) + if (coop) { + coop.jsMindCallBack_(type, body) + } else { + console.error('coop is undefined') + } } Jm.coop = function (_jm) { - if (!this._jm) { - this._jm = _jm - _jm.add_event_listener(jsMindCallBackFacade) - // 设置属性 - this.coop_api_url = _jm.options.coop_api_url - this.client_id = Jm.util.uuid.newid() - - const coop_mind_id = _jm.options.coop_mind_id - if (this.notNull(coop_mind_id)) { - this.coop_mind_id = coop_mind_id - // this.timerFetch() - } - // this.timerPush() - } + this._init(_jm) } Jm.coop.prototype = { // jsMind 对象 _jm: null, // 后台api 接口地址 coop_api_url: null, + // websocket 地址 + coop_ws_url: null, // 标记当前客户端(同一个IP可能打开多个浏览器,需要用client_id区分) client_id: null, // 当前脑图ID @@ -47,6 +40,51 @@ timer_push_task: null, // 定时获得变化 timer_fetch_task: null, + // init + _init: function (_jm) { + if (!this._jm) { + this._jm = _jm + _jm.add_event_listener(jsMindCallBackFacade) + + // 初始化属性 + this._init_properties(_jm) + // 初始化websocket + this._init_ws() + } + }, + _init_properties: function (_jm) { + // 设置属性 + this.coop_api_url = _jm.options.coop_api_url + this.coop_ws_url = _jm.options.coop_ws_url + this.client_id = Jm.util.uuid.newid() + const coop_mind_id = _jm.options.coop_mind_id + if (this.notNull(coop_mind_id)) { + this.coop_mind_id = coop_mind_id + // this.timerFetch() + } + }, + // 初始化websocket + _init_ws: function () { + const ws = new WebSocket(this.coop_ws_url); + //申请一个WebSocket对象,参数是服务端地址,同http协议使用http://开头一样,WebSocket协议的url使用ws://开头,另外安全的WebSocket协议使用wss://开头 + ws.onopen = function () { + //当WebSocket创建成功时,触发onopen事件 + console.log("open"); + ws.send("hello"); //将消息发送到服务端 + } + ws.onmessage = function (e) { + //当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据 + console.log(e.data); + } + ws.onclose = function (e) { + //当客户端收到服务端发送的关闭连接请求时,触发onclose事件 + console.log("close"); + } + ws.onerror = function (e) { + //如果出现连接、处理、接收、发送数据失败的时候触发onerror事件 + console.log(error); + } + }, // 清理定时任务 clear_timer: function () { console.log('clear_timer')