-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
82 lines (68 loc) · 2.45 KB
/
Copy pathapp.py
File metadata and controls
82 lines (68 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit
import socket
import threading
from datetime import datetime
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
# Use 'threading' mode to avoid eventlet/gevent complexity and errors
socketio = SocketIO(app, async_mode='threading')
# Map of Socket.IO session IDs to TCP socket connections to Chat_Server.py
tcp_connections = {}
def tcp_listen_thread(sid, tcp_socket):
"""Listens for data from Chat_Server.py and sends it to the browser."""
while True:
try:
data = tcp_socket.recv(1024)
if not data:
break
msg = data.decode()
# Push message back to the specific browser session
socketio.emit('new_message', {'msg': msg}, room=sid)
except:
break
# Cleanup on disconnect
if sid in tcp_connections:
try:
tcp_connections[sid].close()
except:
pass
del tcp_connections[sid]
socketio.emit('status', {'msg': '[Disconnected from Server]'}, room=sid)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('join')
def handle_join(data):
username = data.get('username', 'Anonymous')
sid = request.sid
try:
# Connect to Chat_Server.py
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.connect(('127.0.0.1', 5002))
tcp_socket.send(username.encode())
tcp_connections[sid] = tcp_socket
# Start background thread to listen to the TCP server
t = threading.Thread(target=tcp_listen_thread, args=(sid, tcp_socket))
t.daemon = True
t.start()
emit('status', {'msg': f'Connected to SpamShield as {username}'})
except Exception as e:
emit('status', {'msg': f'Connection Failed: {str(e)}'})
@socketio.on('send_message')
def handle_message(data):
sid = request.sid
msg = data.get('msg', '').strip()
if msg and sid in tcp_connections:
try:
tcp_connections[sid].send(msg.encode())
except Exception as e:
emit('status', {'msg': f'Send Error: {str(e)}'})
@socketio.on('disconnect')
def handle_disconnect():
sid = request.sid
if sid in tcp_connections:
tcp_connections[sid].close()
del tcp_connections[sid]
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=5000)