websocket.js 381 B

12345678910
  1. export const getWebSocketUrl = (endpoint = '') => {
  2. const protocol = window.location.protocol;
  3. const host = window.location.host;
  4. // 自动识别协议:HTTPS用wss,HTTP用ws
  5. const wsProtocol = protocol === 'https:' ? 'wss:' : 'ws:';
  6. const websocketPath = import.meta.env.VITE_APP_WEBSOCKET_PATH;
  7. return `${wsProtocol}//${host}${websocketPath}${endpoint}`;
  8. };