socket 设置重连间隔 和捕获异常错误

This commit is contained in:
wkj 2024-09-16 08:36:17 +08:00
parent aba7823e04
commit 71caafe333
20 changed files with 163 additions and 41 deletions

View File

@ -1,9 +1,9 @@
export default OakComponent({
isList: false,
data: {
slideWidth: 0, //小程序使用
slideLeft: 0, //小程序使用
slideShow: false, //小程序使用
slideWidth: 0,
slideLeft: 0,
slideShow: false,
commonAction: [
'create',
'update',

View File

@ -61,7 +61,7 @@ export default OakComponent({
let currentUrl = event.currentTarget.dataset.src;
let urlList = event.currentTarget.dataset.list;
wx.previewImage({
current: currentUrl, // 当前显示图片的http链接
current: currentUrl,
urls: urlList, // 需要预览的图片http链接列表
});
},

View File

@ -37,7 +37,7 @@ export default OakComponent({
code: '',
title: '',
desc: '',
icon: '', //web独有
icon: '',
imagePath: '', //小程序独有
},
lifetimes: {

View File

@ -93,7 +93,7 @@ export default OakComponent({
attribute: {},
options: [],
inputType: '',
timeStartStr: '', // 小程序选择时间显示
timeStartStr: '',
timeEndStr: '',
selectedLabel: '',
minDateMp: new Date(1980, 1, 1).getTime(),

View File

@ -13,7 +13,7 @@ export default OakComponent({
tablePagination: undefined,
rowSelection: undefined,
hideHeader: false,
disableSerialNumber: false, //是否禁用序号 默认启用
disableSerialNumber: false,
size: 'large',
scroll: undefined,
empty: undefined,

View File

@ -1,4 +1,5 @@
/// <reference types="wechat-miniprogram" />
/// <reference types="react" />
declare const _default: (props: import("../..").ReactComponentProps<import("oak-domain/lib/types").EntityDict & import("oak-domain/lib/base-app-domain").EntityDict, string | number, false, WechatMiniprogram.Component.DataOption>) => import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>>;
declare const _default: (props: import("../..").ReactComponentProps<import("oak-domain/lib/types").EntityDict & import("oak-domain/lib/base-app-domain").EntityDict, string | number, false, {
key: string;
}>) => import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>>;
export default _default;

View File

@ -22,4 +22,7 @@ export default OakComponent({
return {};
},
features: ['message'],
properties: {
key: ''
}
});

View File

@ -3,5 +3,6 @@ import { MessageProps } from '../../types/Message';
export default function Render(props: {
data: {
data: MessageProps;
key?: string;
};
}): React.JSX.Element;

View File

@ -2,10 +2,10 @@ import React, { useEffect } from 'react';
import { message } from 'antd';
export default function Render(props) {
const [messageApi, contextHolder] = message.useMessage();
const { data } = props.data;
const { data, key } = props.data;
useEffect(() => {
if (data) {
messageApi[data.type](data);
messageApi[data.type](Object.assign(data, { key }));
}
}, [data]);
return <>{contextHolder}</>;

View File

@ -9,7 +9,7 @@ export default OakComponent({
multiple: false,
entityId: '',
entityIds: [],
pickerRender: {}, // OakAbsRefAttrPickerRender
pickerRender: {},
onChange: (() => undefined),
},
formData() {

View File

@ -5,7 +5,7 @@ export default OakComponent({
return this.props.entity;
},
properties: {
helps: {}, // Record<string, string>;
helps: {},
entity: '',
attributes: [],
layout: 'horizontal',

View File

@ -383,7 +383,7 @@ export class Cache extends Feature {
opers.forEach((oper) => {
const { entity, operation } = oper;
this.cacheStore.operate(entity, operation, this.context, {
checkerTypes: ['logical'], // 这里不能检查data不然在数据没填完前会有大量异常
checkerTypes: ['logical'],
dontCollect: true,
});
});

View File

@ -14,6 +14,9 @@ export declare class SubScriber<ED extends EntityDict & BaseEntityDict> extends
private path?;
private socket?;
private halted;
private reconnectInterval;
private reconnectTimer;
private count;
private socketState;
private eventCallbackMap;
constructor(cache: Cache<ED>, message: Message, getSubscribePointFn: () => Promise<{

View File

@ -1,5 +1,6 @@
import { assert } from 'oak-domain/lib/utils/assert';
import { pull, unset } from 'oak-domain/lib/utils/lodash';
import { OakSocketConnectException } from 'oak-domain/lib/types/Exception';
import io from '../utils/socket.io/socket.io';
import { Feature } from '../types/Feature';
export class SubScriber extends Feature {
@ -11,6 +12,9 @@ export class SubScriber extends Feature {
path;
socket;
halted;
reconnectInterval;
reconnectTimer;
count;
socketState = 'unconnected';
eventCallbackMap = {
connect: [],
@ -22,6 +26,9 @@ export class SubScriber extends Feature {
this.message = message;
this.getSubscribePointFn = getSubscribePointFn;
this.halted = false;
this.reconnectInterval = 10 * 1000; // 每隔10秒重连
this.reconnectTimer = undefined;
this.count = 0;
}
on(event, callback) {
this.eventCallbackMap[event].push(callback);
@ -33,9 +40,16 @@ export class SubScriber extends Feature {
this.eventCallbackMap[event].forEach((ele) => ele(data));
}
async initSocketPoint() {
const { url, path } = await this.getSubscribePointFn();
this.url = url;
this.path = path;
try {
const { url, path } = await this.getSubscribePointFn();
this.url = url;
this.path = path;
}
catch (err) {
this.url = undefined;
this.path = undefined;
throw new OakSocketConnectException();
}
}
async connect() {
if (this.halted) {
@ -46,6 +60,7 @@ export class SubScriber extends Feature {
if (!this.url) {
await this.initSocketPoint();
optionInited = true;
this.count = 0;
}
const url = this.url;
const path = this.path;
@ -104,17 +119,40 @@ export class SubScriber extends Feature {
}
});
if (!optionInited) {
let count = 0;
socket.on('connect_error', async (err) => {
count++;
if (count > 50) {
this.count++;
if (this.count > 50) {
// 可能socket地址改变了刷新重连
this.url = undefined;
}
socket.removeAllListeners();
socket.disconnect();
this.url = undefined;
await this.connect();
this.socket = undefined;
// 清除之前的重连定时器
if (this.reconnectTimer) {
clearInterval(this.reconnectTimer);
}
// 根据重连次数设置不同的重连间隔
if (this.count <= 10) {
this.reconnectInterval = 10 * 1000; // 10秒
}
else if (this.count <= 20) {
this.reconnectInterval = 30 * 1000; // 30秒
}
else if (this.count <= 30) {
this.reconnectInterval = 60 * 1000; // 60秒
}
else if (this.count <= 40) {
this.reconnectInterval = 90 * 1000; // 90秒
}
else {
this.reconnectInterval = 120 * 1000; // 2分钟
}
// 设置新的定时器
this.reconnectTimer = setInterval(async () => {
await this.connect();
// resolve(undefined);
}, this.reconnectInterval);
resolve(undefined);
});
}

View File

@ -1,7 +1,7 @@
import React from 'react';
export const keys = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'];
export const values = {
xs: 576, //小于576
xs: 576,
sm: 576,
md: 768,
lg: 992,

View File

@ -386,7 +386,7 @@ class Cache extends Feature_1.Feature {
opers.forEach((oper) => {
const { entity, operation } = oper;
this.cacheStore.operate(entity, operation, this.context, {
checkerTypes: ['logical'], // 这里不能检查data不然在数据没填完前会有大量异常
checkerTypes: ['logical'],
dontCollect: true,
});
});

View File

@ -14,6 +14,9 @@ export declare class SubScriber<ED extends EntityDict & BaseEntityDict> extends
private path?;
private socket?;
private halted;
private reconnectInterval;
private reconnectTimer;
private count;
private socketState;
private eventCallbackMap;
constructor(cache: Cache<ED>, message: Message, getSubscribePointFn: () => Promise<{

View File

@ -4,6 +4,7 @@ exports.SubScriber = void 0;
const tslib_1 = require("tslib");
const assert_1 = require("oak-domain/lib/utils/assert");
const lodash_1 = require("oak-domain/lib/utils/lodash");
const Exception_1 = require("oak-domain/lib/types/Exception");
const socket_io_1 = tslib_1.__importDefault(require("../utils/socket.io/socket.io"));
const Feature_1 = require("../types/Feature");
class SubScriber extends Feature_1.Feature {
@ -15,6 +16,9 @@ class SubScriber extends Feature_1.Feature {
path;
socket;
halted;
reconnectInterval;
reconnectTimer;
count;
socketState = 'unconnected';
eventCallbackMap = {
connect: [],
@ -26,6 +30,9 @@ class SubScriber extends Feature_1.Feature {
this.message = message;
this.getSubscribePointFn = getSubscribePointFn;
this.halted = false;
this.reconnectInterval = 10 * 1000; // 每隔10秒重连
this.reconnectTimer = undefined;
this.count = 0;
}
on(event, callback) {
this.eventCallbackMap[event].push(callback);
@ -37,9 +44,16 @@ class SubScriber extends Feature_1.Feature {
this.eventCallbackMap[event].forEach((ele) => ele(data));
}
async initSocketPoint() {
const { url, path } = await this.getSubscribePointFn();
this.url = url;
this.path = path;
try {
const { url, path } = await this.getSubscribePointFn();
this.url = url;
this.path = path;
}
catch (err) {
this.url = undefined;
this.path = undefined;
throw new Exception_1.OakSocketConnectException();
}
}
async connect() {
if (this.halted) {
@ -50,6 +64,7 @@ class SubScriber extends Feature_1.Feature {
if (!this.url) {
await this.initSocketPoint();
optionInited = true;
this.count = 0;
}
const url = this.url;
const path = this.path;
@ -108,17 +123,40 @@ class SubScriber extends Feature_1.Feature {
}
});
if (!optionInited) {
let count = 0;
socket.on('connect_error', async (err) => {
count++;
if (count > 50) {
this.count++;
if (this.count > 50) {
// 可能socket地址改变了刷新重连
this.url = undefined;
}
socket.removeAllListeners();
socket.disconnect();
this.url = undefined;
await this.connect();
this.socket = undefined;
// 清除之前的重连定时器
if (this.reconnectTimer) {
clearInterval(this.reconnectTimer);
}
// 根据重连次数设置不同的重连间隔
if (this.count <= 10) {
this.reconnectInterval = 10 * 1000; // 10秒
}
else if (this.count <= 20) {
this.reconnectInterval = 30 * 1000; // 30秒
}
else if (this.count <= 30) {
this.reconnectInterval = 60 * 1000; // 60秒
}
else if (this.count <= 40) {
this.reconnectInterval = 90 * 1000; // 90秒
}
else {
this.reconnectInterval = 120 * 1000; // 2分钟
}
// 设置新的定时器
this.reconnectTimer = setInterval(async () => {
await this.connect();
// resolve(undefined);
}, this.reconnectInterval);
resolve(undefined);
});
}

View File

@ -5,7 +5,7 @@ const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
exports.keys = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'];
exports.values = {
xs: 576, //小于576
xs: 576,
sm: 576,
md: 768,
lg: 992,

View File

@ -2,6 +2,7 @@ import { assert } from 'oak-domain/lib/utils/assert';
import { EntityDict, OpRecord } from 'oak-domain/lib/types';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
import { pull, unset } from 'oak-domain/lib/utils/lodash';
import { OakSocketConnectException } from 'oak-domain/lib/types/Exception';
import { Cache } from './cache';
import { Message } from './message';
import io, { Socket } from '../utils/socket.io/socket.io';
@ -27,6 +28,9 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
private path?: string;
private socket?: Socket;
private halted: boolean;
private reconnectInterval: number;
private reconnectTimer: any;
private count: number;
private socketState: 'connecting' | 'connected' | 'unconnected' = 'unconnected';
@ -48,6 +52,9 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
this.message = message;
this.getSubscribePointFn = getSubscribePointFn;
this.halted = false;
this.reconnectInterval = 10 * 1000; // 每隔10秒重连
this.reconnectTimer = undefined
this.count = 0;
}
on(event: SubscribeEvent, callback: (...data: any) => void) {
@ -63,9 +70,16 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
}
private async initSocketPoint() {
const { url, path } = await this.getSubscribePointFn();
this.url = url;
this.path = path;
try {
const { url, path } = await this.getSubscribePointFn();
this.url = url;
this.path = path;
}
catch(err) {
this.url = undefined;
this.path = undefined;
throw new OakSocketConnectException();
}
}
private async connect(): Promise<void> {
@ -77,6 +91,7 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
if (!this.url) {
await this.initSocketPoint();
optionInited = true;
this.count = 0;
}
const url = this.url!;
@ -147,17 +162,37 @@ export class SubScriber<ED extends EntityDict & BaseEntityDict> extends Feature
});
if (!optionInited) {
let count = 0;
socket.on('connect_error', async (err) => {
count++;
if (count > 50) {
this.count++;
if (this.count > 50) {
// 可能socket地址改变了刷新重连
this.url = undefined;
}
socket.removeAllListeners();
socket.disconnect();
this.url = undefined;
await this.connect();
this.socket = undefined;
// 清除之前的重连定时器
if (this.reconnectTimer) {
clearInterval(this.reconnectTimer);
}
// 根据重连次数设置不同的重连间隔
if (this.count <= 10) {
this.reconnectInterval = 10 * 1000; // 10秒
} else if (this.count <= 20) {
this.reconnectInterval = 30 * 1000; // 30秒
} else if (this.count <= 30) {
this.reconnectInterval = 60 * 1000; // 60秒
} else if (this.count <= 40) {
this.reconnectInterval = 90 * 1000; // 90秒
} else {
this.reconnectInterval = 120 * 1000; // 2分钟
}
// 设置新的定时器
this.reconnectTimer = setInterval(async () => {
await this.connect();
// resolve(undefined);
}, this.reconnectInterval);
resolve(undefined);
});
}