Introduce messaging feature allowing users to communicate with contacts
Implements a new ChatPage component with WebSocket for real-time messaging. Replit-Commit-Author: Agent Replit-Commit-Session-Id: c5f0c281-8dd8-4846-b452-4a07bcd21062 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/9777c70b-fc38-4831-8d6b-78dfffe041b0/9d44895d-6eba-4363-a24d-e3722f77b5e9.jpg
This commit is contained in:
@@ -443,5 +443,55 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
||||
app.use(errorTrackingMiddleware);
|
||||
|
||||
const httpServer = createServer(app);
|
||||
|
||||
// WebSocket server setup for real-time chat
|
||||
const wss = new WebSocketServer({ server: httpServer, path: '/ws' });
|
||||
|
||||
wss.on('connection', (ws: WebSocket) => {
|
||||
console.log('New WebSocket connection');
|
||||
let userId: number | null = null;
|
||||
|
||||
ws.on('message', (data: Buffer) => {
|
||||
try {
|
||||
const message = JSON.parse(data.toString());
|
||||
|
||||
if (message.type === 'join' && message.userId) {
|
||||
userId = message.userId;
|
||||
|
||||
// Add connection to user's connection list
|
||||
if (!wsConnections.has(userId)) {
|
||||
wsConnections.set(userId, []);
|
||||
}
|
||||
wsConnections.get(userId)!.push(ws);
|
||||
|
||||
console.log(`User ${userId} connected to WebSocket`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error parsing WebSocket message:', error);
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
if (userId) {
|
||||
// Remove connection from user's connection list
|
||||
const connections = wsConnections.get(userId);
|
||||
if (connections) {
|
||||
const index = connections.indexOf(ws);
|
||||
if (index > -1) {
|
||||
connections.splice(index, 1);
|
||||
}
|
||||
if (connections.length === 0) {
|
||||
wsConnections.delete(userId);
|
||||
}
|
||||
}
|
||||
console.log(`User ${userId} disconnected from WebSocket`);
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('error', (error) => {
|
||||
console.error('WebSocket error:', error);
|
||||
});
|
||||
});
|
||||
|
||||
return httpServer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user