【m】聊天消息接入
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
import store from './store.js';
|
||||
import UIRenderer from './renderer.js';
|
||||
import apiClient from './api.js';
|
||||
import wsManager from './websocket.js';
|
||||
// import wsManager from './websocket.js';
|
||||
import { mockCallSession } from './models.js';
|
||||
import { showNotification, generateId } from './utils.js';
|
||||
|
||||
@@ -39,75 +39,75 @@ let connectionId = "";
|
||||
* 绑定WebSocket事件
|
||||
*/
|
||||
function bindWebSocketEvents() {
|
||||
wsManager.on('connect', () => {
|
||||
console.log('WebSocket connected');
|
||||
showNotification('已连接到服务器');
|
||||
});
|
||||
// wsManager.on('connect', () => {
|
||||
// console.log('WebSocket connected');
|
||||
// showNotification('已连接到服务器');
|
||||
// });
|
||||
|
||||
wsManager.on('disconnect', () => {
|
||||
console.log('WebSocket disconnected');
|
||||
showNotification('与服务器的连接已断开', 5000);
|
||||
});
|
||||
// wsManager.on('disconnect', () => {
|
||||
// console.log('WebSocket disconnected');
|
||||
// showNotification('与服务器的连接已断开', 5000);
|
||||
// });
|
||||
|
||||
wsManager.on('message-received', (data) => {
|
||||
console.log('Message received:', data);
|
||||
store.addMessage(data.message);
|
||||
});
|
||||
// wsManager.on('message-received', (data) => {
|
||||
// console.log('Message received:', data);
|
||||
// store.addMessage(data.message);
|
||||
// });
|
||||
|
||||
wsManager.on('user-joined', (data) => {
|
||||
console.log('User joined:', data);
|
||||
showNotification(`${data.userId} 加入了通话`);
|
||||
});
|
||||
// wsManager.on('user-joined', (data) => {
|
||||
// console.log('User joined:', data);
|
||||
// showNotification(`${data.userId} 加入了通话`);
|
||||
// });
|
||||
|
||||
wsManager.on('user-left', (data) => {
|
||||
console.log('User left:', data);
|
||||
showNotification(`${data.userId} 离开了通话`);
|
||||
});
|
||||
// wsManager.on('user-left', (data) => {
|
||||
// console.log('User left:', data);
|
||||
// showNotification(`${data.userId} 离开了通话`);
|
||||
// });
|
||||
|
||||
wsManager.on('media-state-changed', (data) => {
|
||||
console.log('Media state changed:', data);
|
||||
// 更新远端媒体状态
|
||||
if (data.userId !== store.getLocalUser().id) {
|
||||
store.updateRemoteMedia(data);
|
||||
}
|
||||
});
|
||||
// wsManager.on('media-state-changed', (data) => {
|
||||
// console.log('Media state changed:', data);
|
||||
// // 更新远端媒体状态
|
||||
// if (data.userId !== store.getLocalUser().id) {
|
||||
// store.updateRemoteMedia(data);
|
||||
// }
|
||||
// });
|
||||
|
||||
wsManager.on('network-quality', (data) => {
|
||||
console.log('Network quality changed:', data);
|
||||
// 更新网络质量
|
||||
const state = store.getState();
|
||||
if (data.userId === state.session.remoteUser.id) {
|
||||
state.session.remoteUser.networkQuality = data.quality;
|
||||
store.notify({ type: 'NETWORK_CHANGE', quality: data.quality });
|
||||
}
|
||||
});
|
||||
// wsManager.on('network-quality', (data) => {
|
||||
// console.log('Network quality changed:', data);
|
||||
// // 更新网络质量
|
||||
// const state = store.getState();
|
||||
// if (data.userId === state.session.remoteUser.id) {
|
||||
// state.session.remoteUser.networkQuality = data.quality;
|
||||
// store.notify({ type: 'NETWORK_CHANGE', quality: data.quality });
|
||||
// }
|
||||
// });
|
||||
|
||||
wsManager.on('call-ended', (data) => {
|
||||
console.log('Call ended:', data);
|
||||
store.endCall();
|
||||
showNotification('通话已结束', 3000);
|
||||
});
|
||||
// wsManager.on('call-ended', (data) => {
|
||||
// console.log('Call ended:', data);
|
||||
// store.endCall();
|
||||
// showNotification('通话已结束', 3000);
|
||||
// });
|
||||
|
||||
wsManager.on('call-request', (data) => {
|
||||
console.log('Call request received:', data);
|
||||
// 显示通话请求弹窗
|
||||
if (window.showCallRequest) {
|
||||
const caller = {
|
||||
name: mockCallSession.remoteUser.name,
|
||||
avatar: mockCallSession.remoteUser.avatar
|
||||
};
|
||||
window.showCallRequest(caller);
|
||||
connectionId = data.connectionId;
|
||||
}
|
||||
});
|
||||
//处理发送消息响应
|
||||
wsManager.on('chat-message', (data) => {
|
||||
console.log('chat-message:', data);
|
||||
// 显示消息
|
||||
store.addMessage({
|
||||
data: data.message,
|
||||
});
|
||||
});
|
||||
// wsManager.on('call-request', (data) => {
|
||||
// console.log('Call request received:', data);
|
||||
// // 显示通话请求弹窗
|
||||
// if (window.showCallRequest) {
|
||||
// const caller = {
|
||||
// name: mockCallSession.remoteUser.name,
|
||||
// avatar: mockCallSession.remoteUser.avatar
|
||||
// };
|
||||
// window.showCallRequest(caller);
|
||||
// connectionId = data.connectionId;
|
||||
// }
|
||||
// });
|
||||
// //处理发送消息响应
|
||||
// wsManager.on('chat-message', (data) => {
|
||||
// console.log('chat-message:', data);
|
||||
// // 显示消息
|
||||
// store.addMessage({
|
||||
// data: data.message,
|
||||
// });
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -216,8 +216,8 @@ function bindDomEvents() {
|
||||
|
||||
if (content) {
|
||||
const state = store.getState();
|
||||
const newMessage = {
|
||||
id: generateId(),
|
||||
const message = {
|
||||
id: state.id,
|
||||
senderId: state.session.localUser.id,
|
||||
senderName: state.session.localUser.name,
|
||||
senderAvatar: state.session.localUser.avatar,
|
||||
@@ -227,14 +227,25 @@ function bindDomEvents() {
|
||||
isSelf: true
|
||||
};
|
||||
|
||||
store.addMessage(newMessage);
|
||||
chatInput.value = '';
|
||||
const message = {
|
||||
connectionId: connectionId,
|
||||
message: newMessage
|
||||
store.addMessage(message);
|
||||
|
||||
|
||||
const newMessage = {
|
||||
id: state.id,
|
||||
senderId: state.session.remoteUser.id,
|
||||
senderName: state.session.remoteUser.name,
|
||||
senderAvatar: state.session.remoteUser.avatar,
|
||||
content: content,
|
||||
type: 'text',
|
||||
timestamp: new Date().toISOString(),
|
||||
isSelf: false
|
||||
|
||||
};
|
||||
chatInput.value = '';
|
||||
// 发送消息到服务器
|
||||
wsManager.send('chat-message', message);
|
||||
store.sendChatMessage(newMessage);
|
||||
|
||||
//wsManager.send('chat-message', message);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -367,4 +378,4 @@ window.addEventListener('DOMContentLoaded', async () => {
|
||||
});
|
||||
|
||||
// 导出全局变量
|
||||
export { store, renderer, apiClient, wsManager };
|
||||
export { store, renderer, apiClient };
|
||||
|
||||
Reference in New Issue
Block a user