74 lines
1.7 KiB
JavaScript
74 lines
1.7 KiB
JavaScript
/**
|
||
* WebSocket配置文件
|
||
* 统一管理WebSocket相关配置(环境与 config/app-env.js 一致)
|
||
*/
|
||
|
||
import { getDoctorWxWsUrls, isDoctorWxDev } from './app-env.js';
|
||
|
||
const wsUrls = getDoctorWxWsUrls();
|
||
|
||
/**
|
||
* WebSocket配置
|
||
*/
|
||
export const websocketConfig = {
|
||
/**
|
||
* WebSocket URL配置
|
||
* dev: 开发环境WebSocket地址
|
||
* prod: 生产环境WebSocket地址
|
||
*/
|
||
url: wsUrls,
|
||
|
||
/**
|
||
* 获取当前环境的WebSocket URL
|
||
* @returns {string} WebSocket URL
|
||
*/
|
||
getUrl() {
|
||
return isDoctorWxDev() ? wsUrls.dev : wsUrls.prod;
|
||
},
|
||
|
||
/**
|
||
* 由 WebSocket URL 推导 IM 服务 HTTP 基址(与 WS 同 host,用于 Go API)
|
||
* 例:ws://127.0.0.1:12080/ws -> http://127.0.0.1:12080
|
||
*/
|
||
getHttpBaseUrl() {
|
||
const ws = this.getUrl();
|
||
if (!ws || typeof ws !== 'string') return '';
|
||
let base = ws
|
||
.replace(/^wss:\/\//i, 'https://')
|
||
.replace(/^ws:\/\//i, 'http://');
|
||
base = base.replace(/\/?ws\/?$/i, '');
|
||
return base.replace(/\/+$/, '') || base;
|
||
},
|
||
|
||
/** Go IM:POST /api/send-to-user */
|
||
getSendToUserApiUrl() {
|
||
const base = this.getHttpBaseUrl();
|
||
if (!base) return '';
|
||
return `${base}/api/send-to-user`;
|
||
},
|
||
|
||
/**
|
||
* 重连配置
|
||
*/
|
||
reconnect: {
|
||
maxAttempts: 5, // 最大重连次数
|
||
initialDelay: 1000, // 初始重连延迟(毫秒)
|
||
maxDelay: 30000 // 最大重连延迟(毫秒)
|
||
},
|
||
|
||
/**
|
||
* 心跳配置
|
||
*/
|
||
heartbeat: {
|
||
interval: 300000 // 心跳间隔(毫秒),5分钟
|
||
},
|
||
|
||
/**
|
||
* 用户标识配置
|
||
*/
|
||
user: {
|
||
prefix: 'doctor-', // 医生小程序使用doctor-前缀
|
||
platform: 'doctor-miniprogram' // 平台标识(医生小程序)
|
||
}
|
||
};
|