mirror of
https://github.com/adminlove520/AI-Account-Toolkit.git
synced 2026-05-16 09:26:46 +08:00
- 新增 GPT_register+duckmail+CPA+autouploadsub2api (DuckMail + OAuth + Sub2Api 注册工具) - 新增 team_all-in-one (ChatGPT Team 一键注册工具) - 新增 Code-Patch 项目 - 新增 ABCard 子模块 (ChatGPT Business/Plus 自动开通) - 新增 cloudflare_temp_email 子模块 (Cloudflare 临时邮箱服务) - 添加 .gitignore 文件 - 更新 README.md (新增项目导航、子模块说明) - 添加 CHANGELOG.md
41 lines
951 B
JavaScript
41 lines
951 B
JavaScript
import { onUnmounted } from 'vue'
|
|
|
|
/**
|
|
* Composable for managing a WebSocket connection.
|
|
* Automatically closes the socket when the component is unmounted.
|
|
*
|
|
* @param {string} path - Path template, e.g. '/ws/sessions/{id}'
|
|
* @param {object} handlers - { onMessage(msg), onError(e) }
|
|
* @returns {{ open(id: string|number): void, close(): void }}
|
|
*/
|
|
export function useWebSocket(path, handlers = {}) {
|
|
let ws = null
|
|
|
|
function open(id) {
|
|
close()
|
|
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:'
|
|
const url = `${proto}//${location.host}${path.replace('{id}', id)}`
|
|
ws = new WebSocket(url)
|
|
|
|
ws.onmessage = (e) => {
|
|
const msg = JSON.parse(e.data)
|
|
if (msg.type !== 'ping') {
|
|
handlers.onMessage?.(msg)
|
|
}
|
|
}
|
|
|
|
ws.onerror = (e) => handlers.onError?.(e)
|
|
}
|
|
|
|
function close() {
|
|
if (ws) {
|
|
ws.close()
|
|
ws = null
|
|
}
|
|
}
|
|
|
|
onUnmounted(close)
|
|
|
|
return { open, close }
|
|
}
|