Skip to content

Commit

Permalink
🐛fix(websocket): 断开情况
Browse files Browse the repository at this point in the history
  • Loading branch information
vacuityv committed May 10, 2024
1 parent 3c14014 commit e71fc8b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 44 deletions.
8 changes: 4 additions & 4 deletions appcast.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"identifier": "me.vacuity.chat.gptranslate",
"versions": [
{
"version": "1.2.0",
"desc": "https://github.com/vacuityv/bob-plugin-vac-gptranslate/releases/tag/1.2.0",
"sha256": "d6c7b0ed99922910b5f06b6ed8d5c1d94b4b2dbc027e48f91fa5c9131e54cba5",
"url": "https://github.com/vacuityv/bob-plugin-vac-gptranslate/releases/download/1.2.0/bob-plugin-vac-gptranslate.bobplugin",
"version": "1.3.0",
"desc": "https://github.com/vacuityv/bob-plugin-vac-gptranslate/releases/tag/1.3.0",
"sha256": "4cc790362cb9b13cca8ce50100ad4bb4c5cddc3ed914fa72d428ea5161ae7a58",
"url": "https://github.com/vacuityv/bob-plugin-vac-gptranslate/releases/download/1.3.0/bob-plugin-vac-gptranslate.bobplugin",
"minBobVersion": "0.5.0"
}
]
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"identifier": "me.vacuity.chat.gptranslate",
"version": "1.2.0",
"version": "1.3.0",
"category": "translate",
"name": "GPTranslate",
"summary": "调用chatgpt/gemini/claude实现翻译功能",
Expand Down
58 changes: 19 additions & 39 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,18 @@ function oldTranslate(query, completion) {
}



var websocket = null;

var count = 0;
var timerId = 0;
var signal = $signal.new()

function initWebsocket() {
function initWebsocket(msg) {

vacUrl = usaWss;

if (websocket == null) {
$log.info(`initWebsocket`+ vacUrl);
$log.info(`initWebsocket` + vacUrl);
websocket = $websocket.new({
url: vacUrl,
allowSelfSignedSSLCertificates: true,
Expand All @@ -131,6 +130,7 @@ function initWebsocket() {
websocket.open();
websocket.listenOpen(function (socket) {
$log.info(`did open`);
websocket.sendString(msg);

websocket.listenError(function (socket, error) {
$log.info(`did error: code=${error.code}; message=${error.message}; type=${error.type}`);
Expand All @@ -151,27 +151,6 @@ function initWebsocket() {
})

count = 0;

if (timerId != 0) {
$timer.invalidate(timerId);
}

timerId = $timer.schedule({
interval: 10,
repeats: true,
handler: function () {
websocket.ping()
count += 1;
$log.info(`count=${count}`)
// 空闲 1h 后关闭
if (count > 60 * 6) {
$timer.invalidate(timerId);
if (websocket != null) {
websocket.close();
}
}
}
});
}
}

Expand All @@ -180,23 +159,24 @@ function sendSocketMsg(msg) {
count = 0;
if (websocket == null || websocket.readyState == 2 || websocket.readyState == 3) {
websocket = null;
initWebsocket();
}
if (websocket.readyState == 1) {
$log.info('readyState == 1' + msg)
websocket.sendString(msg);
initWebsocket(msg);
} else {
var stateTimerId = $timer.schedule({
interval: 1,
repeats: true,
handler: function () {
$log.info(`checkready...state=${websocket.readyState}`)
if (websocket.readyState == 1) {
$timer.invalidate(stateTimerId);
websocket.sendString(msg);
if (websocket.readyState == 1) {
$log.info('readyState == 1' + msg)
websocket.sendString(msg);
} else {
var stateTimerId = $timer.schedule({
interval: 1,
repeats: true,
handler: function () {
$log.info(`checkready...state=${websocket.readyState}`)
if (websocket.readyState == 1) {
$timer.invalidate(stateTimerId);
websocket.sendString(msg);
}
}
}
});
});
}
}
}

Expand Down

0 comments on commit e71fc8b

Please sign in to comment.