catch error

This commit is contained in:
pompurin404 2024-08-26 16:36:18 +08:00
parent 4104b15a64
commit 6abba81187
No known key found for this signature in database

View File

@ -7,7 +7,7 @@ import WebSocket from 'ws'
import { tray } from '../resolve/tray' import { tray } from '../resolve/tray'
import { calcTraffic } from '../utils/calc' import { calcTraffic } from '../utils/calc'
import { getRuntimeConfig } from './factory' import { getRuntimeConfig } from './factory'
import { nativeImage } from 'electron' import { dialog, nativeImage } from 'electron'
import parseSvg from '../utils/parseSvg' import parseSvg from '../utils/parseSvg'
const icon = nativeImage.createFromPath(svgIcon) const icon = nativeImage.createFromPath(svgIcon)
@ -192,6 +192,8 @@ const mihomoTraffic = async (): Promise<void> => {
mihomoTrafficWs.onmessage = async (e): Promise<void> => { mihomoTrafficWs.onmessage = async (e): Promise<void> => {
const data = e.data as string const data = e.data as string
const json = JSON.parse(data) as IMihomoTrafficInfo const json = JSON.parse(data) as IMihomoTrafficInfo
trafficRetry = 10
mainWindow?.webContents.send('mihomoTraffic', json)
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
if (showTraffic) { if (showTraffic) {
if (drawing) return if (drawing) return
@ -204,12 +206,17 @@ const mihomoTraffic = async (): Promise<void> => {
<text x='156' y='15' font-size='18' font-family="PingFang SC" font-weight='bold' text-anchor='end'>${calcTraffic(json.up)}/s</text> <text x='156' y='15' font-size='18' font-family="PingFang SC" font-weight='bold' text-anchor='end'>${calcTraffic(json.up)}/s</text>
<text x='156' y='34' font-size='18' font-family="PingFang SC" font-weight='bold' text-anchor='end'>${calcTraffic(json.down)}/s</text> <text x='156' y='34' font-size='18' font-family="PingFang SC" font-weight='bold' text-anchor='end'>${calcTraffic(json.down)}/s</text>
</svg>` </svg>`
const buffer = await parseSvg(svgContent) try {
const image = nativeImage.createFromBuffer(buffer).resize({ height: 16 }) const buffer = await parseSvg(svgContent)
image.setTemplateImage(true) const image = nativeImage.createFromBuffer(buffer).resize({ height: 16 })
tray?.setImage(image) image.setTemplateImage(true)
drawing = false tray?.setImage(image)
hasShowTraffic = true hasShowTraffic = true
} catch (e) {
dialog.showErrorBox('Parse SVG Error', JSON.stringify(e))
} finally {
drawing = false
}
} else { } else {
if (hasShowTraffic) { if (hasShowTraffic) {
const icon = nativeImage.createFromPath(templateIcon) const icon = nativeImage.createFromPath(templateIcon)
@ -227,9 +234,6 @@ const mihomoTraffic = async (): Promise<void> => {
`${calcTraffic(json.down)}/s`.padStart(9) `${calcTraffic(json.down)}/s`.padStart(9)
) )
} }
trafficRetry = 10
mainWindow?.webContents.send('mihomoTraffic', json)
} }
mihomoTrafficWs.onclose = (): void => { mihomoTrafficWs.onclose = (): void => {