Minor changes
This commit is contained in:
@@ -172,7 +172,7 @@ def sql_select(sql=None, data=None, table_name=None, record_id=None, record_id_r
|
|||||||
log.info('Executing a custom SQL select with no extra data dict...')
|
log.info('Executing a custom SQL select with no extra data dict...')
|
||||||
result = db.execute(sql)
|
result = db.execute(sql)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error('*** An exception happened. ***')
|
log.info('An exception happened. Returning False.')
|
||||||
log.error(repr(e))
|
log.error(repr(e))
|
||||||
log.error('***')
|
log.error('***')
|
||||||
log.error(str(e))
|
log.error(str(e))
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from fastapi import APIRouter, FastAPI, WebSocket, WebSocketDisconnect
|
from fastapi import APIRouter, FastAPI, WebSocket, WebSocketDisconnect
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import HTMLResponse
|
||||||
from typing import List
|
from typing import List
|
||||||
|
import aioredis, asyncio, json
|
||||||
|
|
||||||
from ..lib_general import *
|
from ..lib_general import *
|
||||||
from ..log import *
|
from ..log import *
|
||||||
@@ -29,8 +30,8 @@ html = """
|
|||||||
var client_id = Date.now()
|
var client_id = Date.now()
|
||||||
document.querySelector("#ws-id").textContent = client_id;
|
document.querySelector("#ws-id").textContent = client_id;
|
||||||
//var ws = new WebSocket(`ws://localhost:5005/ws/${client_id}`);
|
//var ws = new WebSocket(`ws://localhost:5005/ws/${client_id}`);
|
||||||
var ws = new WebSocket("ws://localhost:8000/ws_redis");
|
var ws = new WebSocket("ws://localhost:8000/ws");
|
||||||
//var ws = new WebSocket("ws://fastapi.localhost/ws_redis");
|
//var ws = new WebSocket("ws://fastapi.localhost/ws");
|
||||||
ws.onmessage = function(event) {
|
ws.onmessage = function(event) {
|
||||||
var messages = document.getElementById('messages')
|
var messages = document.getElementById('messages')
|
||||||
var message = document.createElement('li')
|
var message = document.createElement('li')
|
||||||
@@ -40,7 +41,11 @@ html = """
|
|||||||
};
|
};
|
||||||
function sendMessage(event) {
|
function sendMessage(event) {
|
||||||
var input = document.getElementById("messageText")
|
var input = document.getElementById("messageText")
|
||||||
ws.send(input.value)
|
var data = { 'client_id': client_id, 'message': input.value };
|
||||||
|
var data_json_str = JSON.stringify(data);
|
||||||
|
ws.send(data_json_str);
|
||||||
|
|
||||||
|
//ws.send(input.value)
|
||||||
input.value = ''
|
input.value = ''
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
}
|
}
|
||||||
@@ -61,6 +66,7 @@ async def get():
|
|||||||
async def websocket_endpoint(websocket: WebSocket):
|
async def websocket_endpoint(websocket: WebSocket):
|
||||||
log.setLevel(logging.DEBUG)
|
log.setLevel(logging.DEBUG)
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
log.info('Root of ws. Waiting to accept a websocket and then the redis_connector')
|
||||||
await websocket.accept()
|
await websocket.accept()
|
||||||
await redis_connector(websocket)
|
await redis_connector(websocket)
|
||||||
|
|
||||||
@@ -70,6 +76,7 @@ async def redis_connector(
|
|||||||
):
|
):
|
||||||
log.setLevel(logging.DEBUG)
|
log.setLevel(logging.DEBUG)
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
async def consumer_handler(ws: WebSocket, r):
|
async def consumer_handler(ws: WebSocket, r):
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
@@ -84,7 +91,7 @@ async def redis_connector(
|
|||||||
await r.publish("chat:c", str(data))
|
await r.publish("chat:c", str(data))
|
||||||
except WebSocketDisconnect as exc:
|
except WebSocketDisconnect as exc:
|
||||||
# TODO this needs handling better
|
# TODO this needs handling better
|
||||||
logger.error(exc)
|
log.error(exc)
|
||||||
|
|
||||||
async def producer_handler(r, ws: WebSocket):
|
async def producer_handler(r, ws: WebSocket):
|
||||||
(channel,) = await r.subscribe("chat:c")
|
(channel,) = await r.subscribe("chat:c")
|
||||||
@@ -96,7 +103,7 @@ async def redis_connector(
|
|||||||
await ws.send_text(message.decode("utf-8"))
|
await ws.send_text(message.decode("utf-8"))
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
# TODO this needs handling better
|
# TODO this needs handling better
|
||||||
logger.error(exc)
|
log.error(exc)
|
||||||
|
|
||||||
redis = await aioredis.create_redis_pool(redis_uri)
|
redis = await aioredis.create_redis_pool(redis_uri)
|
||||||
|
|
||||||
@@ -105,9 +112,9 @@ async def redis_connector(
|
|||||||
done, pending = await asyncio.wait(
|
done, pending = await asyncio.wait(
|
||||||
[consumer_task, producer_task], return_when=asyncio.FIRST_COMPLETED,
|
[consumer_task, producer_task], return_when=asyncio.FIRST_COMPLETED,
|
||||||
)
|
)
|
||||||
logger.debug(f"Done task: {done}")
|
log.debug(f"Done task: {done}")
|
||||||
for task in pending:
|
for task in pending:
|
||||||
logger.debug(f"Canceling task: {task}")
|
log.debug(f"Canceling task: {task}")
|
||||||
task.cancel()
|
task.cancel()
|
||||||
redis.close()
|
redis.close()
|
||||||
await redis.wait_closed()
|
await redis.wait_closed()
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
|
test file
|
||||||
|
|||||||
Reference in New Issue
Block a user