From 36d9572beab69815e43bf2766f304c4d6601f9ac Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sat, 12 Apr 2025 22:10:49 +0800 Subject: [PATCH] bugfix --- bricks/qaframe.js | 24 +++++++++++++++++++----- bricks/websocket.js | 38 +++++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/bricks/qaframe.js b/bricks/qaframe.js index e5529e3..bd485b7 100644 --- a/bricks/qaframe.js +++ b/bricks/qaframe.js @@ -1,10 +1,10 @@ bricks = window.bricks || {}; -bricks.QAFrame = class extends bricks.PopupWindow { +bricks.QAFrame = class extends bricks.VBox { /* { ws_url: - ws_opts: + ws_params: title: description: courseware:{ @@ -30,15 +30,26 @@ bricks.QAFrame = class extends bricks.PopupWindow { this.add_widget(this.bottom_w); var url = this.ws_url; if (this.ws_params){ - url += '?' + URLSearchParams(this.ws_params).toString(); + url += '?' + new URLSearchParams(this.ws_params).toString(); } - this.ws = new bricks.WebSocket(url); + this.ws = new bricks.WebSocket({ + ws_url:url + }); + this.ws.bind('onopen', this.start_question_answer.bind(this)); + this.ws.bind('onquestion', this.show_question.bind(this)); + this.ws.bind('oncourseware', this.show_courseware.bind(this)); if (this.courseware){ this.play_course(); } else { this.show_conform(); } } + show_question(d){ + console.log('show_question(), d=', d); + } + show_courseware(d){ + console.log('show_courseware(), d=', d); + } show_conform(){ var btn = new bricks.Button({ label: 'Start ?' @@ -58,7 +69,10 @@ bricks.QAFrame = class extends bricks.PopupWindow { this.main_w.clear_widgets(); var d = { 'type': 'qa_start', - 'data': null + 'data': { + d: 'test data', + v: 100 + } }; this.ws.send(d) } diff --git a/bricks/websocket.js b/bricks/websocket.js index 4fe14b6..25b47c3 100644 --- a/bricks/websocket.js +++ b/bricks/websocket.js @@ -22,23 +22,27 @@ bricks.WebSocket = class extends bricks.VBox { } else { this.ws = new WebSocket(this.ws_url); } - this.ws.onopen = function(){ - this.dispatch('onopen'); - console.log("open"); - } - this.ws.onclose = function(e){ - this.dispatch('onclose'); - console.log("close"); - } - this.ws.onerror = function(e){ - this.dispatch('onerror'); - console.log(error); - } - this.ws.onmessage = function(e){ - var d = JSON.parse(e.data); - var eventname = 'on' + d.type; - this.dispatch(eventname, d.data); - } + this.ws.onopen = this.on_open.bind(this); + this.ws.onmessage = this.on_message.bind(this); + this.ws.onclose = this.on_close.bind(this); + this.ws.onerror = this.on_error.bind(this); + } + on_open(e){ + this.dispatch('onopen'); + console.log("open"); + } + on_close(e){ + this.dispatch('onclose'); + console.log("close"); + } + on_error(e){ + this.dispatch('onerror'); + console.log(error); + } + on_message(e){ + var d = JSON.parse(e.data); + var eventname = 'on' + d.type; + this.dispatch(eventname, d.data); } send_text(text){ var d = {