-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🚩 websocket server-based console ConsoleWeb
- Optimized `ConsolePlus` and some fixes: - Fixed a syntax bug in 'Console.py' - Added "Output History" to record the output of NARS reasoner uses "output event handlers" in `Interface.py` (by enhances function `register_interface`) - (NEW) ConsoleWeb: Set up a WebSocket-based server using package `websockets` - It gives ability to remote control `ConsolePlus` via WS connection "ws://[server address]:[server port]" (it can be set by user) and input Narsese/cmd, just like directly input on the command line. - In addition to receiving messages sent by clients as console input, it also returns the output of NARS reasoners as JSON text, which is in format `{"interface_name": str, "output_type": str, "content": str}` - an example output: `{"interface_name": "initial", "output_type": "ANSWER", "content": "<B-->C>. %1.000;0.900%"}`
- Loading branch information
1 parent
4b49df0
commit 299cab9
Showing
5 changed files
with
136 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import websockets | ||
import asyncio | ||
from ConsolePlus import execute_input, output_history | ||
from json import dumps | ||
|
||
|
||
async def handler(websocket, path): | ||
print(f"Connected with path={path}!") | ||
messages2send = [] | ||
len_outputs = 0 | ||
last_output_json_obj = {} | ||
async for message in websocket: | ||
messages2send.clear() | ||
# execute | ||
execute_input(message) | ||
# handle output using json | ||
if len_outputs < len(output_history): | ||
# traverse newer outputs | ||
for i in range(len_outputs, len(output_history)): | ||
# data: (interface_name, output_type, content) | ||
# format: {"interface_name": XXX, "output_type": XXX, "content": XXX} | ||
(last_output_json_obj['interface_name'], | ||
last_output_json_obj['output_type'], | ||
last_output_json_obj['content']) = output_history[i] | ||
# send | ||
messages2send.append(dumps(last_output_json_obj)) | ||
# send result if have | ||
for message2send in messages2send: | ||
print(f"send: {message} -> {message2send}") | ||
await websocket.send(message2send) | ||
# refresh output length | ||
len_outputs = len(output_history) | ||
|
||
|
||
if __name__ == '__main__': | ||
# Config | ||
d_host = '127.0.0.1' | ||
host = input(f'Please input host (default {d_host}): ') | ||
host = host if len(host) > 0 else d_host | ||
|
||
d_port = 8765 | ||
port = input(f'Please input port (default {d_port}): ') | ||
port = int(port) if port else d_port | ||
|
||
# Launch | ||
asyncio.get_event_loop().run_until_complete( | ||
websockets.serve(handler, host, port) | ||
) | ||
print(f'WS server launched on {host}:{port}.') | ||
asyncio.get_event_loop().run_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters