fix: attempt to fix sse
This commit is contained in:
+14
-9
@@ -55,15 +55,19 @@ export class PubSubManager {
|
|||||||
function ping() {
|
function ping() {
|
||||||
clearTimeout(heartbeatTimeout);
|
clearTimeout(heartbeatTimeout);
|
||||||
heartbeatTimeout = setTimeout(() => {
|
heartbeatTimeout = setTimeout(() => {
|
||||||
void writer.write(":)\n\n").catch(() => {});
|
try {
|
||||||
|
void writer.write(":)\n\n").catch(() => { });
|
||||||
|
} catch { }
|
||||||
ping();
|
ping();
|
||||||
}, 90000); // cloudflare timeout = 100s
|
}, 90000); // cloudflare timeout = 100s
|
||||||
}
|
}
|
||||||
|
|
||||||
const write = (payload: PubPayload) => {
|
const write = (payload: PubPayload) => {
|
||||||
void writer
|
try {
|
||||||
.write(this.constructMessage(payload.data, payload.event))
|
void writer
|
||||||
.catch(() => {});
|
.write(this.constructMessage(payload.data, payload.event))
|
||||||
|
.catch(() => { });
|
||||||
|
} catch { }
|
||||||
ping();
|
ping();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -83,6 +87,7 @@ export class PubSubManager {
|
|||||||
console.log(` DSC ${topic}`);
|
console.log(` DSC ${topic}`);
|
||||||
|
|
||||||
sub.unsubscribe(`${this.prefix}${topic}`, handler);
|
sub.unsubscribe(`${this.prefix}${topic}`, handler);
|
||||||
|
void writer.close().catch(() => { });
|
||||||
clearInterval(heartbeatTimeout);
|
clearInterval(heartbeatTimeout);
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
};
|
};
|
||||||
@@ -122,7 +127,7 @@ export class EventSourceEndpoint<T extends EventSourceEventMap> {
|
|||||||
constructor(
|
constructor(
|
||||||
private endpoint: string,
|
private endpoint: string,
|
||||||
private eventMap: T,
|
private eventMap: T,
|
||||||
) {}
|
) { }
|
||||||
|
|
||||||
pub<K extends keyof T>(event: K, data: z.infer<T[K]>) {
|
pub<K extends keyof T>(event: K, data: z.infer<T[K]>) {
|
||||||
if (!this.manager)
|
if (!this.manager)
|
||||||
@@ -139,8 +144,8 @@ export class EventSourceEndpoint<T extends EventSourceEventMap> {
|
|||||||
events: Partial<{ [K in keyof T]: (data: z.infer<T[K]>) => void }>,
|
events: Partial<{ [K in keyof T]: (data: z.infer<T[K]>) => void }>,
|
||||||
{
|
{
|
||||||
endpoint = this.defaultEndpointUrl,
|
endpoint = this.defaultEndpointUrl,
|
||||||
onerror = () => {},
|
onerror = () => { },
|
||||||
onopen = () => {},
|
onopen = () => { },
|
||||||
}: SubOption = {},
|
}: SubOption = {},
|
||||||
) {
|
) {
|
||||||
const es = new ReconnectingEventSource(endpoint);
|
const es = new ReconnectingEventSource(endpoint);
|
||||||
@@ -159,8 +164,8 @@ export class EventSourceEndpoint<T extends EventSourceEventMap> {
|
|||||||
callback: (data: z.infer<T[K]>) => void,
|
callback: (data: z.infer<T[K]>) => void,
|
||||||
{
|
{
|
||||||
endpoint = this.defaultEndpointUrl,
|
endpoint = this.defaultEndpointUrl,
|
||||||
onerror = () => {},
|
onerror = () => { },
|
||||||
onopen = () => {},
|
onopen = () => { },
|
||||||
}: SubOption = {},
|
}: SubOption = {},
|
||||||
) {
|
) {
|
||||||
const listener = (e: MessageEvent<string>) => callback(JSON.parse(e.data));
|
const listener = (e: MessageEvent<string>) => callback(JSON.parse(e.data));
|
||||||
|
|||||||
Reference in New Issue
Block a user