wip
This commit is contained in:
@ -23,7 +23,6 @@ let storeRecordings = false;
|
||||
let queueCounter = 0;
|
||||
|
||||
const sessions = new Map();
|
||||
const users = new Map();
|
||||
const chats = new Map(); // Store chat rooms
|
||||
|
||||
storage.init().then(() => {
|
||||
@ -48,18 +47,57 @@ wss.on('connection', (ws) => {
|
||||
ws.on('message', (message) => {
|
||||
try {
|
||||
const data = JSON.parse(message);
|
||||
console.log('Received message:', data.type);
|
||||
|
||||
if (data.type === 'join') {
|
||||
const { username } = data;
|
||||
users.set(ws.sessionId, { username, sessionId: ws.sessionId });
|
||||
broadcastUserList();
|
||||
} else if (data.type === 'startChat') {
|
||||
const { users: chatUsers } = data;
|
||||
const chatId = Math.random().toString(36).substring(2);
|
||||
chats.set(chatId, { participants: [ws.sessionId, ...chatUsers], messages: [] });
|
||||
broadcastUserList();
|
||||
} else if (data.type === 'audio') {
|
||||
handleAudioData(ws, data.audio);
|
||||
switch (data.type) {
|
||||
case 'join':
|
||||
const { username } = data;
|
||||
sessions.set(ws.sessionId, { username, sessionId: ws.sessionId });
|
||||
broadcastUserList();
|
||||
break;
|
||||
case 'startChat':
|
||||
const { users: chatUsers } = data;
|
||||
const chatId = Math.random().toString(36).substring(2);
|
||||
let participants = [ws.sessionId, ...chatUsers];
|
||||
// Deduplicate participants
|
||||
participants = [...new Set(participants)];
|
||||
|
||||
chats.set(chatId, { participants, messages: [] });
|
||||
|
||||
const userNames = participants.map(sessionId => {
|
||||
const user = sessions.get(sessionId);
|
||||
return user ? `${user.username}(${user.sessionId})` : 'Unknown User';
|
||||
});
|
||||
|
||||
console.log('Creating chat room. Users:', userNames);
|
||||
|
||||
participants.forEach(sessionId => {
|
||||
const participantSocket = Array.from(wss.clients).find(client => client.sessionId === sessionId);
|
||||
if (participantSocket && participantSocket.readyState === WebSocket.OPEN) {
|
||||
participantSocket.send(JSON.stringify({ newChatRoom: { id: chatId, participants: userNames } }));
|
||||
}
|
||||
});
|
||||
|
||||
broadcastUserList();
|
||||
break;
|
||||
case 'audio':
|
||||
console.log('(queue ' + queueCounter + ') Received ' + (data.audio.length / 1024 / 1024).toFixed(3) + ' MB audio from client. Crrent language: ' + language, 'task: ' + data.task);
|
||||
handleAudioData(ws, data.audio);
|
||||
break;
|
||||
case 'reconnect':
|
||||
const userSession = sessions.get(data.sessionId);
|
||||
if (userSession) {
|
||||
sessions.set(ws.sessionId, userSession);
|
||||
ws.sessionId = data.sessionId;
|
||||
broadcastUserList();
|
||||
// Send existing chats
|
||||
const userChats = Array.from(chats.values()).filter(chat => chat.participants.includes(ws.sessionId));
|
||||
ws.send(JSON.stringify({ type: 'chats', chats: userChats }));
|
||||
ws.send(JSON.stringify({ type: 'userList', users: userList }));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
console.log('Unknown message type:', data.type);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to parse message', err);
|
||||
@ -67,7 +105,7 @@ wss.on('connection', (ws) => {
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
users.delete(ws.sessionId);
|
||||
// allUsers.delete(ws.sessionId);
|
||||
sessions.delete(ws.sessionId);
|
||||
broadcastUserList();
|
||||
});
|
||||
@ -140,7 +178,7 @@ function transcribeAudio(ws, formData, sessionData) {
|
||||
}
|
||||
|
||||
function broadcastUserList() {
|
||||
const userList = Array.from(users.values()).map(user => ({ username: user.username, sessionId: user.sessionId }));
|
||||
const userList = Array.from(sessions.values()).map(user => ({ username: user.username, sessionId: user.sessionId }));
|
||||
wss.clients.forEach(client => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(JSON.stringify({ type: 'userList', users: userList }));
|
||||
@ -153,6 +191,11 @@ app.get('/', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, 'chat-client.html'));
|
||||
});
|
||||
|
||||
app.get('/audio.js', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, 'audio.js'));
|
||||
});
|
||||
|
||||
|
||||
app.post('/log', (req, res) => {
|
||||
console.log(`[LOG ${new Date().toISOString()}] ${req.body.message}`);
|
||||
res.status(200).send('OK');
|
||||
@ -185,6 +228,7 @@ app.post('/settings', (req, res) => {
|
||||
|
||||
app.post('/upload', (req, res) => {
|
||||
const timestamp = Date.now();
|
||||
console.log('Received audio data:', timestamp);
|
||||
fs.mkdir('rec', { recursive: true }, (err) => {
|
||||
if (err) return res.status(500).send('ERROR');
|
||||
const file = fs.createWriteStream(`rec/audio_slice_${timestamp}.ogg`);
|
||||
|
Reference in New Issue
Block a user