forked from CherryHQ/cherry-studio-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseWebSocket.ts
More file actions
192 lines (161 loc) · 6.29 KB
/
useWebSocket.ts
File metadata and controls
192 lines (161 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { File } from 'expo-file-system'
import { useEffect, useRef, useState } from 'react'
import type { Socket } from 'socket.io-client'
import { io } from 'socket.io-client'
import { DEFAULT_BACKUP_STORAGE } from '@/constants/storage'
import { loggerService } from '@/services/LoggerService'
import type { ConnectionInfo } from '@/types/network'
const logger = loggerService.withContext('useWebSocket')
// 定义 WebSocket 连接状态的枚举
export enum WebSocketStatus {
IDLE = 'idle',
CONNECTING = 'connecting',
CONNECTED = 'connected',
ZIP_FILE_START = 'zip_file_start',
ZIP_FILE_END = 'zip_file_end',
DISCONNECTED = 'disconnected',
ERROR = 'error'
}
export function useWebSocket() {
const socket = useRef<Socket | null>(null)
const zipFileInfo = useRef<{ filename: string; totalSize: number }>({ filename: '', totalSize: 0 })
const zipFileChunk = useRef<{ chunk: Uint8Array[]; size: number }>({ chunk: [], size: 0 })
const [status, setStatus] = useState<WebSocketStatus>(WebSocketStatus.IDLE)
const [progress, setProgress] = useState<number>(0)
const [filename, setFilename] = useState<string>('')
useEffect(() => {
return () => {
socket.current?.disconnect()
socket.current = null
}
}, [])
// 写入文件的函数
const writeZipFile = async () => {
try {
// 确保父目录存在
if (!DEFAULT_BACKUP_STORAGE.exists) {
DEFAULT_BACKUP_STORAGE.create({ intermediates: true })
}
// 文件路径 Paths.cache + zipFileInfo.current.filename
const file = new File(DEFAULT_BACKUP_STORAGE, zipFileInfo.current.filename)
if (file.exists) {
file.delete()
}
// 合并所有 chunk 为一个完整的 Uint8Array
const totalSize = zipFileChunk.current.size
const completeData = new Uint8Array(totalSize)
let offset = 0
for (const chunk of zipFileChunk.current.chunk) {
completeData.set(chunk, offset)
offset += chunk.length
}
// 创建文件并写入数据
file.create()
file.write(completeData)
logger.info(`File ${file.name} saved successfully`)
logger.info(`File Path: ${file.uri}`)
// 清理缓存数据
zipFileChunk.current = { chunk: [], size: 0 }
zipFileInfo.current = { filename: '', totalSize: 0 }
} catch (error) {
logger.error('Failed to write zip file:', error)
setStatus(WebSocketStatus.ERROR)
}
}
const connect = async (connectionInfo: ConnectionInfo) => {
if (socket.current) {
return
}
try {
setStatus(WebSocketStatus.CONNECTING)
// Sort candidates by priority (lower number = higher priority)
const sortedCandidates = [...connectionInfo.candidates].sort((a, b) => a.priority - b.priority)
// Try each candidate until one succeeds
for (const candidate of sortedCandidates) {
try {
const connectionUrl = `http://${candidate.host}:${connectionInfo.port}`
// If we reach here, this candidate worked, use it for the actual connection
socket.current = io(connectionUrl, {
timeout: 5000,
reconnection: false, // 禁用自动重连,因为这是一次性文件传输
transports: ['websocket', 'polling'] // Try both transports
})
break // Exit the loop when we find a working candidate
} catch (error) {
logger.warn(`Failed to connect to ${candidate.host} via ${candidate.interface}:`, error)
continue // Try next candidate
}
}
// If no candidates worked
if (!socket.current) {
throw new Error('Failed to connect to any IP candidate')
}
// 连接客户端
socket.current.on('connect', () => {
logger.info('connected to WebSocket server')
setStatus(WebSocketStatus.CONNECTED)
socket.current?.emit('message', 'This is from iPhone')
})
// 接收连接成功消息
socket.current.on('message_received', (data: { success: boolean }) => {
logger.info('message received from WebSocket server:', data)
})
// 断开连接
socket.current.on('disconnect', () => {
logger.info('disconnected from WebSocket server')
// 如果是文件传输完成后的断开,不要改变状态
setStatus(prevStatus => {
if (prevStatus === WebSocketStatus.ZIP_FILE_END) {
logger.debug('File transfer completed, keeping ZIP_FILE_END status')
return prevStatus
}
return WebSocketStatus.DISCONNECTED
})
socket.current = null
})
// 连接错误
socket.current.on('connect_error', error => {
logger.error('WebSocket connection error:', error)
setStatus(WebSocketStatus.ERROR)
socket.current = null
})
// 文件接收开始
socket.current.on('zip-file-start', (data: { filename: string; totalSize: number }) => {
logger.info('zip-file-start:', data)
setStatus(WebSocketStatus.ZIP_FILE_START)
zipFileInfo.current = data
setFilename(data.filename)
})
// 文件接收过程
// todo: progress无法实时更新
socket.current.on('zip-file-chunk', (chunk: ArrayBuffer) => {
const chunkData = new Uint8Array(chunk)
zipFileChunk.current.chunk.push(chunkData)
zipFileChunk.current.size += chunkData.length
const progress = Math.min((zipFileChunk.current.size / zipFileInfo.current.totalSize) * 100, 100)
setProgress(progress)
logger.silly('zip-file-chunk:', Math.floor(progress), '%')
})
// 文件接收结束
socket.current.on('zip-file-end', async () => {
logger.info('zip-file-end')
// 写入文件
await writeZipFile()
setStatus(WebSocketStatus.ZIP_FILE_END)
setProgress(100)
// 主动断开连接,避免服务端关闭后客户端持续重连
socket.current?.disconnect()
socket.current = null
logger.info('WebSocket connection closed after file transfer completed')
})
} catch (error) {
logger.error('Failed to get IP address:', error)
setStatus(WebSocketStatus.ERROR)
}
}
const disconnect = () => {
socket.current?.disconnect()
socket.current = null
}
return { connect, status, progress, filename, disconnect }
}