File tree Expand file tree Collapse file tree 3 files changed +15
-3
lines changed Expand file tree Collapse file tree 3 files changed +15
-3
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ const MyWebSocket = () => {
8
8
const [ socket , setSocket ] = useState ( null )
9
9
const [ isOpen , setIsOpen ] = useState ( false )
10
10
const [ cellData , setCellData ] = useState ( "" ) // type=text, textarea
11
+ const [ result , setResult ] = useState ( { } )
11
12
// 1. connect to the websocket server
12
13
// 2. push & listen for messages in the websocket connection
13
14
// 3. disconnect from websocket server
@@ -30,6 +31,12 @@ const MyWebSocket = () => {
30
31
socket . onclose = ( ) => {
31
32
setIsOpen ( false )
32
33
}
34
+
35
+ socket . onmessage = ( event ) => {
36
+ const { data} = event
37
+ const msgData = JSON . parse ( data )
38
+ setResult ( msgData )
39
+ }
33
40
}
34
41
return ( ) => {
35
42
if ( socket ) {
@@ -72,8 +79,12 @@ const MyWebSocket = () => {
72
79
< button onClick = { performOpen } > Open</ button >
73
80
</ div >
74
81
< div > { isOpen &&
75
-
76
- < textarea placeholder = 'Your cell data' value = { cellData } name = 'cellData' onChange = { handleInputChange } />
82
+ < React . Fragment >
83
+ < textarea placeholder = 'Your cell data' value = { cellData } name = 'cellData' onChange = { handleInputChange } />
84
+ { result . result && < div > { result . result } </ div > }
85
+ { result . error && < div className = 'text-error' > { result . error } </ div > }
86
+
87
+ </ React . Fragment >
77
88
78
89
} </ div >
79
90
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ async def websocket_handler(websocket, path):
34
34
if cell_data != None :
35
35
stdout , stderr = await call_cli_command (cell_data )
36
36
print (stdout , stderr )
37
+ await websocket .send (json .dumps ({"result" : stdout , "error" : stderr }))
37
38
# await websocket.send(json.dumps({"this": "awesome"}))
38
39
# print(message)
39
40
# pass
Original file line number Diff line number Diff line change 1
- body {
1
+ .text-error {
2
2
color : red !important ;
3
3
}
You can’t perform that action at this time.
0 commit comments