测试
This commit is contained in:
@@ -15,25 +15,25 @@ let connectionId = "";
|
|||||||
/**
|
/**
|
||||||
* 初始化应用
|
* 初始化应用
|
||||||
*/
|
*/
|
||||||
function initApp() {
|
// function initApp() {
|
||||||
// 初始化渲染器
|
// // 初始化渲染器
|
||||||
renderer = new UIRenderer(store);
|
// renderer = new UIRenderer(store);
|
||||||
|
|
||||||
// 初始化WebSocket连接
|
// // 初始化WebSocket连接
|
||||||
wsManager.connect();
|
// wsManager.connect();
|
||||||
|
|
||||||
// 绑定WebSocket事件
|
// // 绑定WebSocket事件
|
||||||
bindWebSocketEvents();
|
// bindWebSocketEvents();
|
||||||
|
|
||||||
// 绑定DOM事件
|
// // 绑定DOM事件
|
||||||
bindDomEvents();
|
// bindDomEvents();
|
||||||
|
|
||||||
// 初始化WebRTC (如果需要)
|
// // 初始化WebRTC (如果需要)
|
||||||
// initWebRTC();
|
// // initWebRTC();
|
||||||
|
|
||||||
|
|
||||||
console.log('App initialized');
|
// console.log('App initialized');
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 绑定WebSocket事件
|
* 绑定WebSocket事件
|
||||||
@@ -92,14 +92,22 @@ function bindWebSocketEvents() {
|
|||||||
console.log('Call request received:', data);
|
console.log('Call request received:', data);
|
||||||
// 显示通话请求弹窗
|
// 显示通话请求弹窗
|
||||||
if (window.showCallRequest) {
|
if (window.showCallRequest) {
|
||||||
const caller = {
|
const caller = {
|
||||||
name: mockCallSession.remoteUser.name,
|
name: mockCallSession.remoteUser.name,
|
||||||
avatar:mockCallSession.remoteUser.avatar
|
avatar: mockCallSession.remoteUser.avatar
|
||||||
};
|
};
|
||||||
window.showCallRequest(caller);
|
window.showCallRequest(caller);
|
||||||
connectionId =data.connectionId;
|
connectionId = data.connectionId;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
//处理发送消息响应
|
||||||
|
wsManager.on('chat-message', (data) => {
|
||||||
|
console.log('chat-message:', data);
|
||||||
|
// 显示消息
|
||||||
|
store.addMessage({
|
||||||
|
data: data.message,
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -221,9 +229,12 @@ function bindDomEvents() {
|
|||||||
|
|
||||||
store.addMessage(newMessage);
|
store.addMessage(newMessage);
|
||||||
chatInput.value = '';
|
chatInput.value = '';
|
||||||
|
const message = {
|
||||||
|
connectionId: connectionId,
|
||||||
|
message: newMessage
|
||||||
|
};
|
||||||
// 发送消息到服务器
|
// 发送消息到服务器
|
||||||
// wsManager.send('send-message', newMessage);
|
wsManager.send('chat-message', message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,18 +2,11 @@
|
|||||||
* WebSocket管理
|
* WebSocket管理
|
||||||
* 管理WebSocket连接,处理WebSocket事件
|
* 管理WebSocket连接,处理WebSocket事件
|
||||||
*/
|
*/
|
||||||
|
import { RenderStreaming } from "../../module/renderstreaming.js"; // WebRTC连接管理
|
||||||
|
|
||||||
class WebSocketManager {
|
class WebSocketManager {
|
||||||
constructor(url = null) {
|
constructor() {
|
||||||
this.url = url || this.getDefaultWebSocketUrl();
|
|
||||||
this.socket = null;
|
|
||||||
this.isConnected = false;
|
this.isConnected = false;
|
||||||
this.listeners = new Map();
|
|
||||||
this.reconnectAttempts = 0;
|
|
||||||
this.maxReconnectAttempts = 5;
|
|
||||||
this.reconnectDelay = 1000;
|
|
||||||
this.connectionId = null;
|
|
||||||
this.heartbeatInterval = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,36 +21,33 @@ class WebSocketManager {
|
|||||||
/**
|
/**
|
||||||
* 连接WebSocket
|
* 连接WebSocket
|
||||||
*/
|
*/
|
||||||
connect() {
|
init() {
|
||||||
try {
|
try {
|
||||||
this.socket = new WebSocket(this.url);
|
|
||||||
|
|
||||||
this.socket.onopen = () => {
|
RenderStreaming.onConnect = () => {
|
||||||
console.log('WebSocket connected');
|
console.log('WebSocket connected');
|
||||||
this.isConnected = true;
|
this.isConnected = true;
|
||||||
this.reconnectAttempts = 0;
|
|
||||||
|
|
||||||
// 生成连接ID
|
|
||||||
this.connectionId = this.generateConnectionId();
|
|
||||||
|
|
||||||
// 发送连接消息
|
|
||||||
this.sendConnectMessage();
|
|
||||||
|
|
||||||
// 启动心跳
|
// // 发送连接消息
|
||||||
this.startHeartbeat();
|
// this.sendConnectMessage();
|
||||||
|
|
||||||
this.emit('connect');
|
// // 启动心跳
|
||||||
|
this.startHeartbeat();
|
||||||
|
|
||||||
|
// this.emit('connect');
|
||||||
};
|
};
|
||||||
|
|
||||||
this.socket.onclose = () => {
|
RenderStreaming.onDisconnect = () => {
|
||||||
console.log('WebSocket disconnected');
|
console.log('WebSocket disconnected');
|
||||||
this.isConnected = false;
|
this.isConnected = false;
|
||||||
this.stopHeartbeat();
|
this.stopHeartbeat();
|
||||||
this.emit('disconnect');
|
//this.emit('disconnect');
|
||||||
this.attemptReconnect();
|
//this.attemptReconnect();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.socket.onmessage = (event) => {
|
RenderStreaming.onmessage = (event) => {
|
||||||
try {
|
try {
|
||||||
const message = JSON.parse(event.data);
|
const message = JSON.parse(event.data);
|
||||||
this.handleMessage(message);
|
this.handleMessage(message);
|
||||||
@@ -126,6 +116,7 @@ class WebSocketManager {
|
|||||||
*/
|
*/
|
||||||
handleMessage(message) {
|
handleMessage(message) {
|
||||||
if (message.type) {
|
if (message.type) {
|
||||||
|
console.log('Received message:', message);
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case 'user-joined':
|
case 'user-joined':
|
||||||
this.emit('user-joined', message.data);
|
this.emit('user-joined', message.data);
|
||||||
@@ -165,6 +156,9 @@ class WebSocketManager {
|
|||||||
case 'candidate':
|
case 'candidate':
|
||||||
this.emit('candidate', message.data);
|
this.emit('candidate', message.data);
|
||||||
break;
|
break;
|
||||||
|
case 'chat-message':
|
||||||
|
this.emit('chat-message', message.data);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
// 处理旧格式消息
|
// 处理旧格式消息
|
||||||
if (message.event) {
|
if (message.event) {
|
||||||
|
|||||||
@@ -361,8 +361,26 @@ function onCandidate(ws: WebSocket, message: any): void {
|
|||||||
// }));
|
// }));
|
||||||
return connectionIds;
|
return connectionIds;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 处理chat-message信令
|
||||||
|
* @param ws WebSocket连接实例
|
||||||
|
* @param message 消息数据
|
||||||
|
*/
|
||||||
|
function onChatMessage(ws: WebSocket, message: any): void {
|
||||||
|
// 获取连接ID
|
||||||
|
const connectionId = message.connectionId;
|
||||||
|
const chatMessage = message.message;
|
||||||
|
if (connectionPair.has(connectionId)) {
|
||||||
|
const pair = connectionPair.get(connectionId);
|
||||||
|
// 找到另一个WebSocket实例
|
||||||
|
const otherSessionWs = pair[0] == ws ? pair[1] : pair[0];
|
||||||
|
if (otherSessionWs) {
|
||||||
|
// 发送chat-message消息
|
||||||
|
otherSessionWs.send(JSON.stringify({ from: connectionId, to: "", type: "chat-message", data: chatMessage }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 导出WebSocket处理器函数
|
* 导出WebSocket处理器函数
|
||||||
*/
|
*/
|
||||||
export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onCallConnectionId, onBroadcast, onGetAllConnectionIds, AddHeartbeat, RemoveHeartbeat };
|
export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onCallConnectionId, onBroadcast, onGetAllConnectionIds, AddHeartbeat, RemoveHeartbeat ,onChatMessage};
|
||||||
|
|||||||
@@ -108,6 +108,10 @@ export default class WSSignaling {
|
|||||||
// 处理callConnectionId信令
|
// 处理callConnectionId信令
|
||||||
handler.onCallConnectionId(ws, msg.data);
|
handler.onCallConnectionId(ws, msg.data);
|
||||||
break;
|
break;
|
||||||
|
case 'chat-message'://接受连接ConnectionId
|
||||||
|
// 处理chat-message信令
|
||||||
|
handler.onChatMessage(ws, msg.data);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
// 忽略未知消息类型
|
// 忽略未知消息类型
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user