Es gibt ein Problem mit dieser P2P-Chat-App, da immer keine Verbindung hergestellt werden kann. Ich habe diese App mit Hilfe von KI von chatgbt erstellt. Ich bin gut in Python, aber nicht gut genug, um mit JavaScript umzugehen. Ich habe die App je nach Funktion in mehrere Dateien aufgeteilt. Das Problem mit WebRTC liegt speziell in ICE. Ich habe den Ice-Server von Google hinzugefügt, aber es funktioniert immer noch nicht.
import asyncio
import json
import os
import time
import websockets
# -----------------------------
# In-memory state
# -----------------------------
# USERS_BY_KEY: key -> { name, phone, room, ip, location, ws, last_seen }
USERS_BY_KEY = {}
# WS_TO_KEY: ws -> key
WS_TO_KEY = {}
DATA_DIR = os.path.abspath(".") # where we store per-user friends_*.json
def user_key_from(name: str, phone: str) -> str:
k = (phone or name or "").strip().lower()
return k
def friends_file_for(key: str) -> str:
safe = "".join(ch for ch in key if ch.isalnum() or ch in ("_", "-", "."))
return os.path.join(DATA_DIR, f"friends_{safe}.json")
def load_friends(key: str):
path = friends_file_for(key)
if not os.path.exists(path):
return []
try:
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, list):
# normalize to lowercase unique strings
seen, out = set(), []
for x in data:
t = (x or "").strip().lower()
if t and t not in seen:
seen.add(t)
out.append(t)
return out
except Exception:
pass
return []
def save_friends(key: str, friends_list):
# normalize and persist
seen, out = set(), []
for x in friends_list:
t = (x or "").strip().lower()
if t and t not in seen:
seen.add(t)
out.append(t)
path = friends_file_for(key)
with open(path, "w", encoding="utf-8") as f:
json.dump(out, f, ensure_ascii=False, indent=2)
return out
async def broadcast_user_list(room: str):
"""Send the list of online users in 'room' to everyone in that room."""
users_payload = []
now = time.time()
for key, info in USERS_BY_KEY.items():
if info.get("room") == room and info.get("ws") is not None:
users_payload.append({
"name": info.get("name"),
"phone": info.get("phone"),
"ip": info.get("ip"),
"room": info.get("room"),
"location": info.get("location"),
"last_seen": info.get("last_seen", now),
})
msg = json.dumps({"action": "user-list", "users": users_payload})
tasks = []
for key, info in USERS_BY_KEY.items():
if info.get("room") == room and info.get("ws") is not None:
tasks.append(info["ws"].send(msg))
if tasks:
await asyncio.gather(*tasks, return_exceptions=True)
async def handle_register(ws, data):
name = (data.get("name") or "").strip()
phone = (data.get("phone") or "").strip()
room = (data.get("room") or "main").strip() or "main"
location = data.get("location") # {lat, lon} or None
key = user_key_from(name, phone)
if not key:
await ws.send(json.dumps({"action": "error", "error": "Missing name/phone"}))
return
ip = ws.remote_address[0] if ws.remote_address else "unknown"
USERS_BY_KEY[key] = {
"name": name,
"phone": phone,
"room": room,
"ip": ip,
"location": location,
"ws": ws,
"last_seen": time.time(),
}
WS_TO_KEY[ws] = key
await ws.send(json.dumps({"action": "registered", "key": key, "ip": ip, "room": room}))
await broadcast_user_list(room)
async def handle_get_friends(ws):
key = WS_TO_KEY.get(ws)
if not key:
await ws.send(json.dumps({"action": "friends", "friends": []}))
return
friends = load_friends(key)
await ws.send(json.dumps({"action": "friends", "friends": friends}))
async def handle_friend_add(ws, data):
key = WS_TO_KEY.get(ws)
if not key:
return
target = (data.get("target") or "").strip().lower()
if not target:
return
current = load_friends(key)
if target not in current:
current.append(target)
current = save_friends(key, current)
await ws.send(json.dumps({"action": "friends", "friends": current, "ok": True}))
async def handle_friend_remove(ws, data):
key = WS_TO_KEY.get(ws)
if not key:
return
target = (data.get("target") or "").strip().lower()
if not target:
return
current = load_friends(key)
current = [x for x in current if x != target]
current = save_friends(key, current)
await ws.send(json.dumps({"action": "friends", "friends": current, "ok": True}))
async def handler(ws):
print("🌐 Client connected")
try:
async for raw in ws:
try:
data = json.loads(raw)
except json.JSONDecodeError:
continue
act = data.get("action")
if act == "register":
await handle_register(ws, data)
elif act == "get-friends":
await handle_get_friends(ws)
elif act == "friend-add":
await handle_friend_add(ws, data)
elif act == "friend-remove":
await handle_friend_remove(ws, data)
elif act == "get-all-users":
# Optional: send all users (for map/debug)
payload = []
for key, info in USERS_BY_KEY.items():
payload.append({
"name": info.get("name"),
"phone": info.get("phone"),
"room": info.get("room"),
"ip": info.get("ip"),
"location": info.get("location"),
"last_seen": info.get("last_seen", time.time()),
})
await ws.send(json.dumps({"action": "all-users", "users": payload}))
elif act == "chat":
sender_key = WS_TO_KEY.get(ws)
if not sender_key:
continue
text = (data.get("text") or "").strip()
room = USERS_BY_KEY.get(sender_key, {}).get("room", "main")
if not text:
continue
msg = json.dumps({
"action": "chat",
"from": sender_key,
"text": text,
"time": time.strftime("%H:%M:%S"),
})
# broadcast to same room
tasks = []
for key, info in USERS_BY_KEY.items():
if info.get("room") == room and info.get("ws"):
tasks.append(info["ws"].send(msg))
if tasks:
await asyncio.gather(*tasks, return_exceptions=True)
elif act == "chat-private":
sender_key = WS_TO_KEY.get(ws)
target_key = (data.get("target") or "").strip().lower()
text = (data.get("text") or "").strip()
if not sender_key or not target_key or not text:
continue
payload = json.dumps({
"action": "chat-private",
"from": sender_key,
"to": target_key,
"text": text,
"time": time.strftime("%H:%M:%S"),
})
for k, info in USERS_BY_KEY.items():
if k in (sender_key, target_key) and info.get("ws"):
await info["ws"].send(payload)
except websockets.exceptions.ConnectionClosed:
pass
finally:
# cleanup
key = WS_TO_KEY.pop(ws, None)
if key and key in USERS_BY_KEY:
room = USERS_BY_KEY[key].get("room", "main")
USERS_BY_KEY[key]["ws"] = None
USERS_BY_KEY[key]["last_seen"] = time.time()
print(f"❎ {key} disconnected")
await broadcast_user_list(room)
async def main():
host = "0.0.0.0"
port = 8555
print("📂 Data dir:", DATA_DIR)
print(f"📡 Signaling server on ws://{host}:{port}")
async with websockets.serve(handler, host, port, ping_interval=20, ping_timeout=20):
await asyncio.Future()
if __name__ == "__main__":
asyncio.run(main())
Es gibt ein [url=viewtopic.php?t=26065]Problem[/url] mit dieser P2P-Chat-App, da immer keine Verbindung hergestellt werden kann. Ich habe diese App mit Hilfe von KI von chatgbt erstellt. Ich bin gut in Python, aber nicht gut genug, um mit JavaScript umzugehen. Ich habe die App je nach Funktion in mehrere Dateien aufgeteilt. Das [url=viewtopic.php?t=26065]Problem[/url] mit WebRTC liegt speziell in ICE. Ich habe den Ice-Server von Google hinzugefügt, aber es funktioniert immer noch nicht. [code] const wsUrl = "ws://localhost:8555"; let ws, pc, dc; let myKey = null; let myName = ""; let myPhone = ""; let myRoom = "main"; let currentTarget = null; let map, markerCluster, mapRefreshInterval = null; let pendingCandidates = []; let connectionTimeout = null;
DATA_DIR = os.path.abspath(".") # where we store per-user friends_*.json
def user_key_from(name: str, phone: str) -> str: k = (phone or name or "").strip().lower() return k
def friends_file_for(key: str) -> str: safe = "".join(ch for ch in key if ch.isalnum() or ch in ("_", "-", ".")) return os.path.join(DATA_DIR, f"friends_{safe}.json")
def load_friends(key: str): path = friends_file_for(key) if not os.path.exists(path): return [] try: with open(path, "r", encoding="utf-8") as f: data = json.load(f) if isinstance(data, list): # normalize to lowercase unique strings seen, out = set(), [] for x in data: t = (x or "").strip().lower() if t and t not in seen: seen.add(t) out.append(t) return out except Exception: pass return []
def save_friends(key: str, friends_list): # normalize and persist seen, out = set(), [] for x in friends_list: t = (x or "").strip().lower() if t and t not in seen: seen.add(t) out.append(t) path = friends_file_for(key) with open(path, "w", encoding="utf-8") as f: json.dump(out, f, ensure_ascii=False, indent=2) return out
async def broadcast_user_list(room: str): """Send the list of online users in 'room' to everyone in that room.""" users_payload = [] now = time.time() for key, info in USERS_BY_KEY.items(): if info.get("room") == room and info.get("ws") is not None: users_payload.append({ "name": info.get("name"), "phone": info.get("phone"), "ip": info.get("ip"), "room": info.get("room"), "location": info.get("location"), "last_seen": info.get("last_seen", now), }) msg = json.dumps({"action": "user-list", "users": users_payload}) tasks = [] for key, info in USERS_BY_KEY.items(): if info.get("room") == room and info.get("ws") is not None: tasks.append(info["ws"].send(msg)) if tasks: await asyncio.gather(*tasks, return_exceptions=True)
async def handle_register(ws, data): name = (data.get("name") or "").strip() phone = (data.get("phone") or "").strip() room = (data.get("room") or "main").strip() or "main" location = data.get("location") # {lat, lon} or None
key = user_key_from(name, phone) if not key: await ws.send(json.dumps({"action": "error", "error": "Missing name/phone"})) return
ip = ws.remote_address[0] if ws.remote_address else "unknown"
async def handle_friend_add(ws, data): key = WS_TO_KEY.get(ws) if not key: return target = (data.get("target") or "").strip().lower() if not target: return current = load_friends(key) if target not in current: current.append(target) current = save_friends(key, current) await ws.send(json.dumps({"action": "friends", "friends": current, "ok": True}))
async def handle_friend_remove(ws, data): key = WS_TO_KEY.get(ws) if not key: return target = (data.get("target") or "").strip().lower() if not target: return current = load_friends(key) current = [x for x in current if x != target] current = save_friends(key, current) await ws.send(json.dumps({"action": "friends", "friends": current, "ok": True}))
async def handler(ws): print("🌐 Client connected") try: async for raw in ws: try: data = json.loads(raw) except json.JSONDecodeError: continue act = data.get("action")
if act == "register": await handle_register(ws, data)
elif act == "get-all-users": # Optional: send all users (for map/debug) payload = [] for key, info in USERS_BY_KEY.items(): payload.append({ "name": info.get("name"), "phone": info.get("phone"), "room": info.get("room"), "ip": info.get("ip"), "location": info.get("location"), "last_seen": info.get("last_seen", time.time()), }) await ws.send(json.dumps({"action": "all-users", "users": payload})) elif act == "chat": sender_key = WS_TO_KEY.get(ws) if not sender_key: continue text = (data.get("text") or "").strip() room = USERS_BY_KEY.get(sender_key, {}).get("room", "main") if not text: continue msg = json.dumps({ "action": "chat", "from": sender_key, "text": text, "time": time.strftime("%H:%M:%S"), }) # broadcast to same room tasks = [] for key, info in USERS_BY_KEY.items(): if info.get("room") == room and info.get("ws"): tasks.append(info["ws"].send(msg)) if tasks: await asyncio.gather(*tasks, return_exceptions=True)
elif act == "chat-private": sender_key = WS_TO_KEY.get(ws) target_key = (data.get("target") or "").strip().lower() text = (data.get("text") or "").strip() if not sender_key or not target_key or not text: continue payload = json.dumps({ "action": "chat-private", "from": sender_key, "to": target_key, "text": text, "time": time.strftime("%H:%M:%S"), }) for k, info in USERS_BY_KEY.items(): if k in (sender_key, target_key) and info.get("ws"): await info["ws"].send(payload)
Ich arbeite mit WebRTC P2P-Videostreaming auf 2-Kanal-FullHD und HD zwischen meiner Kamera und dem Internet. Das Problem tritt auf, wenn ich von Kanal HD auf Kanal FullHD umschalte und umgekehrt. Das...
Ich habe andere Beiträge überprüft, in denen der Fehler in der Betreffzeile erwähnt wurde, aber keiner davon hat geholfen. Ich habe den Server in einem Projekt und den Client in einem anderen.
Dies...
Ich habe einige JSON -Daten in DummyData . Ich bin mir nicht sicher, wie ich die Chat -Blasen links und rechts nach der Richtung platzieren kann. Ich benutze Material UI und Kontext -API. Bild als...
Ich versuche mit Pyvisa eine Verbindung zu einem GPIB -Instrument herzustellen, aber ich erhalte einen Fehler. Ich folge dem einfachen Beispiel auf der Pyvisa -Website:
Ich versuche mit Pyvisa eine Verbindung zu einem GPIB -Instrument herzustellen, aber ich erhalte einen Fehler. Ich folge dem einfachen Beispiel auf der Pyvisa -Website: