Compare commits
1 Commits
mimo/code/
...
mimo/creat
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
571475a749 |
@@ -66,12 +66,7 @@ class NostrIdentity:
|
|||||||
if privkey_hex:
|
if privkey_hex:
|
||||||
self.privkey = int(privkey_hex, 16)
|
self.privkey = int(privkey_hex, 16)
|
||||||
else:
|
else:
|
||||||
# Rejection sampling: avoid modulo bias from % N
|
self.privkey = int.from_bytes(os.urandom(32), 'big') % N
|
||||||
while True:
|
|
||||||
candidate = int.from_bytes(os.urandom(32), 'big')
|
|
||||||
if 0 < candidate < N:
|
|
||||||
self.privkey = candidate
|
|
||||||
break
|
|
||||||
self.pubkey = get_pubkey(self.privkey)
|
self.pubkey = get_pubkey(self.privkey)
|
||||||
|
|
||||||
def sign_event(self, event):
|
def sign_event(self, event):
|
||||||
|
|||||||
21
server.py
21
server.py
@@ -50,21 +50,24 @@ async def broadcast_handler(websocket: websockets.WebSocketServerProtocol):
|
|||||||
# Broadcast to all OTHER clients
|
# Broadcast to all OTHER clients
|
||||||
if not clients:
|
if not clients:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
disconnected = set()
|
disconnected = set()
|
||||||
# Track (client, task) pairs so failed clients are identified directly
|
# Create broadcast tasks, tracking which client each task targets
|
||||||
send_jobs = []
|
task_client_pairs = []
|
||||||
for client in clients:
|
for client in clients:
|
||||||
if client != websocket and client.open:
|
if client != websocket and client.open:
|
||||||
send_jobs.append((client, asyncio.create_task(client.send(message))))
|
task = asyncio.create_task(client.send(message))
|
||||||
|
task_client_pairs.append((task, client))
|
||||||
|
|
||||||
if send_jobs:
|
if task_client_pairs:
|
||||||
results = await asyncio.gather(*[job[1] for job in send_jobs], return_exceptions=True)
|
tasks = [pair[0] for pair in task_client_pairs]
|
||||||
for (target_client, _), result in zip(send_jobs, results):
|
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
|
for i, result in enumerate(results):
|
||||||
if isinstance(result, Exception):
|
if isinstance(result, Exception):
|
||||||
logger.error(f"Failed to send to a client {target_client.remote_address}: {result}")
|
target_client = task_client_pairs[i][1]
|
||||||
|
logger.error(f"Failed to send to client {target_client.remote_address}: {result}")
|
||||||
disconnected.add(target_client)
|
disconnected.add(target_client)
|
||||||
|
|
||||||
if disconnected:
|
if disconnected:
|
||||||
clients.difference_update(disconnected)
|
clients.difference_update(disconnected)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user