Skip to content

Commit 57c635c

Browse files
SepehrBazyarSepehr Bazyar
andauthored
feat: add html file to test connections (#1)
Co-authored-by: Sepehr Bazyar <sepehr.bazyar@karnameh.ir>
1 parent fa61466 commit 57c635c

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

src/api/v1/endpoints/deals.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ async def create_deal(
2121
) -> DealOut:
2222
db_deal = Deal(**deal_in.model_dump())
2323
session.add(db_deal)
24-
await session.commit()
25-
await session.refresh(db_deal)
24+
session.commit()
25+
session.refresh(db_deal)
2626

2727
deal_data = {
2828
"id": db_deal.id,

src/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from contextlib import asynccontextmanager
33

44
from fastapi import FastAPI
5+
from fastapi.staticfiles import StaticFiles
56

67
from src.api.v1.router import router as api_router
78
from src.redis.pubsub import (
@@ -31,3 +32,10 @@ async def lifespan(app: FastAPI):
3132
prefix="/api",
3233
)
3334
app.include_router(ws_router)
35+
app.mount(
36+
"/static",
37+
StaticFiles(
38+
directory="static",
39+
),
40+
name="static",
41+
)

static/index.html

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>NYSE</title>
7+
<style>
8+
body { font-family: Arial, sans-serif; max-width: 600px; margin: 20px auto; }
9+
#deals { list-style: none; padding: 0; }
10+
#deals li { border: 1px solid #ddd; margin: 10px 0; padding: 10px; }
11+
form { margin-bottom: 20px; }
12+
</style>
13+
</head>
14+
<body>
15+
<h1>Test WebSocket Deals</h1>
16+
<form id="sub-form">
17+
<label for="tags">Subscribe to Tags (comma-separated IDs):</label><br>
18+
<input type="text" id="tags" placeholder="e.g., 1,2,3" required>
19+
<button type="submit">Subscribe</button>
20+
</form>
21+
<h2>Received Deals:</h2>
22+
<ul id="deals"></ul>
23+
24+
<script>
25+
let ws = null;
26+
const dealsList = document.getElementById('deals');
27+
const subForm = document.getElementById('sub-form');
28+
29+
function connect() {
30+
ws = new WebSocket('ws://localhost:8000/ws');
31+
32+
ws.onopen = () => {
33+
console.log('Connected to WebSocket');
34+
};
35+
36+
ws.onmessage = (event) => {
37+
const batch = JSON.parse(event.data);
38+
batch.forEach(deal => {
39+
const li = document.createElement('li');
40+
li.innerHTML = `Tag: ${deal.tag_id} | Details: ${deal.details} (ID: ${deal.id})`;
41+
dealsList.appendChild(li);
42+
});
43+
};
44+
45+
ws.onclose = () => {
46+
console.log('Disconnected. Reconnecting...');
47+
setTimeout(connect, 1000);
48+
};
49+
50+
ws.onerror = (error) => {
51+
console.error('WebSocket error:', error);
52+
};
53+
}
54+
55+
subForm.addEventListener('submit', (e) => {
56+
e.preventDefault();
57+
const tagsInput = document.getElementById('tags').value.trim();
58+
if (tagsInput && ws && ws.readyState === WebSocket.OPEN) {
59+
const tags = tagsInput.split(',').map(t => parseInt(t.trim(), 10)).filter(t => !isNaN(t));
60+
ws.send(JSON.stringify({ action: 'subscribe', tags }));
61+
console.log('Subscribed to tags:', tags);
62+
} else {
63+
alert('Connect first or enter valid tags!');
64+
}
65+
});
66+
67+
connect();
68+
</script>
69+
</body>
70+
</html>

0 commit comments

Comments
 (0)