This commit is contained in:
yumoqing 2024-04-25 18:08:32 +08:00
parent ff954059cb
commit 603d6bfe82
4 changed files with 63 additions and 1 deletions

View File

@ -6,7 +6,7 @@ SOURCES=" page_data_loader.js factory.js uitypesdef.js utils.js \
tree.js multiple_state_image.js dynamiccolumn.js form.js message.js conform.js \
paging.js datagrid.js dataviewer.js iframe.js \
floaticonbar.js miniform.js wterm.js dynamicaccordion.js \
llm_dialog.js "
llm_dialog.js websocket.js "
echo ${SOURCES}
cat ${SOURCES} > ../dist/bricks.js
# uglifyjs --compress --mangle -- ../dist/bricks.js > ../dist/bricks.min.js

53
bricks/websocket.js Normal file
View File

@ -0,0 +1,53 @@
var bricks = window.bricks || {}
bricks.WebSocket = class extends bricks.VBox {
constructor(opts){
super(opts);
this.dialog = new bricks.VScrollPanel({})
this.dialog.set_css('filler')
this.input = new bricks.UiStr({
name:'prompt',
label:'Say something'
});
this.add_widget(this.dialog);
this.add_widget(this.input);
this.input.bind('keypress', this.check_keypress.bind(this));
this.ws = new WebSocket(this.ws_url);
this.ws.onopen = function(){
console.log("open");
}
this.ws.onclose = function(e){
//当客户端收到服务端发送的关闭连接请求时触发onclose事件
console.log("close");
}
this.ws.onerror = function(e){
//如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
console.log(error);
}
this.ws.onmessage = this.show_response.bind(this);
}
check_keypress(e){
if (e.key == 'Enter'){
var v = this.input.getValue();
var s = v.prompt;
console.log('check_keypress()', v, s);
if (s != ''){
this.show_input(s);
}
}
}
show_input(s){
var w = new bricks.MdWidget({mdtext:s});
w.set_css('user_msg');
this.dialog.add_widget(w);
this.ws.send(s);
}
show_response(e){
var d = JSON.parse(e.data);
console.log(e.data, d.type, d.data, d);
var w = new bricks.MdWidget({mdtext:d.data});
w.set_css('llm_msg');
this.dialog.add_widget(w);
}
}
bricks.Factory.register('WebSocket', bricks.WebSocket);

View File

@ -1 +1,2 @@
print('ws_data=', ws_data)
return 'resp:' + ws_data

8
examples/websocket.ui Normal file
View File

@ -0,0 +1,8 @@
{
"widgettype":"WebSocket",
"options":{
"width":"100%",
"height":"100%",
"ws_url":"{{entire_url('echo.ws')}}"
}
}