-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_stream_handler.py
More file actions
179 lines (151 loc) · 5.78 KB
/
simple_stream_handler.py
File metadata and controls
179 lines (151 loc) · 5.78 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
"""
Simple Stream Handler for testing WebSocket connections
Minimal implementation to verify Glass Chat integration
"""
from socketify import App, OpCode, CompressOptions
import json
import logging
from datetime import datetime
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Global state
connected_clients = set()
def ws_open(ws):
"""Handle new WebSocket connection"""
logger.info("New WebSocket connection established")
connected_clients.add(ws)
# Subscribe to broadcast channel
ws.subscribe("broadcast")
# Send welcome message
welcome_message = {
"type": "system_info",
"message": "Connected to Simple Stream Handler",
"version": "4.0-simple",
"features": {
"tool_execution": True,
"physics_simulation": False,
"ally_integration": True
},
"timestamp": datetime.now().isoformat()
}
ws.send(json.dumps(welcome_message), OpCode.TEXT)
# Send initial ping
ws.send(json.dumps({
'type': 'ping',
'timestamp': datetime.now().timestamp(),
'target': 'client',
'status': 'active',
'msg-sent-timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}), OpCode.TEXT)
def ws_close(ws, code, message):
"""Handle WebSocket connection close"""
logger.info(f"WebSocket connection closed with code {code}")
connected_clients.discard(ws)
def ws_message(ws, message, opcode):
"""Handle incoming WebSocket message"""
logger.info(f"Received message: {message}")
try:
# Parse JSON message
data = json.loads(message)
message_type = data.get('type')
logger.info(f"Processing message type: {message_type}")
# Handle different message types
if message_type == 'ping':
# Respond to ping
timestamp = data.get('timestamp')
target = data.get('target', 'sh')
if target == 'sh':
response = {
'type': 'pong',
'timestamp': timestamp,
'target': target,
'server_time': datetime.now().timestamp(),
'status': 'active',
'msg-sent-timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
ws.send(json.dumps(response), OpCode.TEXT)
elif message_type == 'ally_intent':
# Handle Ally intent messages
logger.info(f"Processing ally intent: {data.get('intent')}")
# Echo back a response
response = {
'type': 'ally_status',
'status': 'processed',
'component': 'simple_stream_handler',
'details': {
'intent': data.get('intent'),
'processed_at': datetime.now().isoformat()
},
'msg-sent-timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
ws.send(json.dumps(response), OpCode.TEXT)
# Broadcast to other clients
app.publish("broadcast", json.dumps(response), OpCode.TEXT)
else:
# Echo back unknown messages
response = {
'type': 'echo',
'original_type': message_type,
'original_message': data,
'status': 'received',
'msg-sent-timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
ws.send(json.dumps(response), OpCode.TEXT)
except json.JSONDecodeError as e:
logger.error(f"Invalid JSON received: {e}")
error_response = {
"type": "error",
"error": {
"code": "INVALID_JSON",
"message": "Message is not valid JSON"
},
"timestamp": datetime.now().timestamp()
}
ws.send(json.dumps(error_response), OpCode.TEXT)
except Exception as e:
logger.error(f"Error processing message: {e}")
error_response = {
"type": "error",
"error": {
"code": "PROCESSING_ERROR",
"message": "Internal error processing message"
},
"timestamp": datetime.now().timestamp()
}
ws.send(json.dumps(error_response), OpCode.TEXT)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Simple Stream Handler for testing")
parser.add_argument("--port", type=int, default=3000, help="Port to listen on")
args = parser.parse_args()
print("=" * 60)
print("Simple Stream Handler - WebSocket Testing")
print("=" * 60)
# Create the app
app = App()
# Set up WebSocket server
app.ws(
"/*",
{
"compression": CompressOptions.SHARED_COMPRESSOR,
"max_payload_length": 16 * 1024 * 1024, # 16MB payload limit
"idle_timeout": 60,
"open": ws_open,
"message": ws_message,
"close": ws_close,
}
)
# HTTP routes
app.any("/", lambda res, req: res.end("Simple Stream Handler - WebSocket Testing"))
app.any("/status", lambda res, req: res.end(json.dumps({
"status": "active",
"version": "4.0-simple",
"connections": len(connected_clients),
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
})))
logger.info(f"Starting Simple Stream Handler on port {args.port}")
app.listen(args.port, lambda config: logger.info(f"Listening on http://localhost:{args.port}"))
# Run the server
app.run()