Изменения вебсокета
This commit is contained in:
parent
d090e5d85c
commit
ae0276c900
3 changed files with 6 additions and 4 deletions
|
@ -13,7 +13,7 @@
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
async function getLastMessages() {
|
async function getLastMessages() {
|
||||||
const url = 'http://localhost:8000/chat/last_messages'
|
const url = 'http://localhost:8000/api/chat/get_last_message/2'
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method: 'GET'
|
method: 'GET'
|
||||||
})
|
})
|
||||||
|
@ -41,7 +41,9 @@
|
||||||
document.querySelector("#ws-id").textContent = chat_id;
|
document.querySelector("#ws-id").textContent = chat_id;
|
||||||
let ws = new WebSocket(`ws://localhost:8000/chat/ws/${chat_id}?user_id=1`);
|
let ws = new WebSocket(`ws://localhost:8000/chat/ws/${chat_id}?user_id=1`);
|
||||||
ws.onmessage = function (event) {
|
ws.onmessage = function (event) {
|
||||||
appendMessage(event.data)
|
const data = JSON.parse(event.data)
|
||||||
|
appendMessage(data.message);
|
||||||
|
console.log(data)
|
||||||
};
|
};
|
||||||
|
|
||||||
function sendMessage(event) {
|
function sendMessage(event) {
|
||||||
|
|
|
@ -15,7 +15,7 @@ router = APIRouter(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('')
|
@router.get("")
|
||||||
async def get_all_chats(user: Users = Depends(get_current_user)):
|
async def get_all_chats(user: Users = Depends(get_current_user)):
|
||||||
return await UserDAO.get_user_allowed_chats(user.id)
|
return await UserDAO.get_user_allowed_chats(user.id)
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ class ConnectionManager(WebSocket):
|
||||||
async def broadcast(self, user_id: int, chat_id: int, message: str):
|
async def broadcast(self, user_id: int, chat_id: int, message: str):
|
||||||
await self.add_message_to_database(user_id=user_id, chat_id=chat_id, message=message)
|
await self.add_message_to_database(user_id=user_id, chat_id=chat_id, message=message)
|
||||||
for websocket in self.active_connections[chat_id]:
|
for websocket in self.active_connections[chat_id]:
|
||||||
await websocket.send_text('{"message": ' + message + "}")
|
await websocket.send_json({'message': message})
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def add_message_to_database(user_id: int, chat_id: int, message: str):
|
async def add_message_to_database(user_id: int, chat_id: int, message: str):
|
||||||
|
|
Loading…
Add table
Reference in a new issue