Skip to content

Commit b8ff4ad

Browse files
committed
test 4
1 parent 4113b57 commit b8ff4ad

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

src/frontend/apps/impress/src/features/docs/doc-management/libs/CollaborationProvider.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import {
55
CompleteHocuspocusProviderWebsocketConfiguration,
66
HocuspocusProvider,
77
HocuspocusProviderConfiguration,
8+
WebSocketStatus,
89
onOutgoingMessageParameters,
910
onStatusParameters,
1011
} from '@hocuspocus/provider';
11-
import type { CloseEvent, MessageEvent } from 'ws';
12+
import type { MessageEvent } from 'ws';
1213
import * as Y from 'yjs';
1314

1415
import { isAPIError } from '@/api';
@@ -43,9 +44,8 @@ export class CollaborationProvider extends HocuspocusProvider {
4344
public seemsUnsyncCount = 0;
4445
public seemsUnsyncMaxCount = 5;
4546
protected sse: EventSource | null = null; // Server-Sent Events
47+
protected pollTimeout: NodeJS.Timeout | null = null;
4648
protected url = '';
47-
public websocketFailureCount = 0;
48-
public websocketMaxFailureCount = 2;
4949

5050
public constructor(configuration: CollaborationProviderConfiguration) {
5151
const withWS = false;
@@ -77,12 +77,15 @@ export class CollaborationProvider extends HocuspocusProvider {
7777
}
7878

7979
public setPollDefaultValues(): void {
80+
console.log('setPollDefaultValues:');
8081
this.isLongPollingStarted = false;
8182
this.isWebsocketFailed = false;
8283
this.seemsUnsyncCount = 0;
8384
this.sse?.close();
8485
this.sse = null;
85-
this.websocketFailureCount = 0;
86+
if (this.pollTimeout) {
87+
clearTimeout(this.pollTimeout);
88+
}
8689
}
8790

8891
public destroy(): void {
@@ -91,6 +94,7 @@ export class CollaborationProvider extends HocuspocusProvider {
9194
}
9295

9396
public onWebsocketConnect = () => {
97+
console.log('onWebsocketConnect:');
9498
this.setPollDefaultValues();
9599
};
96100

@@ -100,33 +104,44 @@ export class CollaborationProvider extends HocuspocusProvider {
100104

101105
public onStatus({ status }: onStatusParameters) {
102106
console.log('status:', status);
107+
if (status === WebSocketStatus.Connecting) {
108+
if (this.pollTimeout) {
109+
clearTimeout(this.pollTimeout);
110+
}
111+
this.pollTimeout = setTimeout(() => {
112+
this.initPolling();
113+
}, 5000);
114+
} else if (status === WebSocketStatus.Connected) {
115+
this.setPollDefaultValues();
116+
}
103117

104118
super.onStatus({ status });
105119
}
106120

107-
public onClose(event: CloseEvent): void {
108-
console.log('close:', event);
109-
this.isAuthenticated = false;
110-
this.synced = false;
121+
public initPolling() {
122+
this.isWebsocketFailed = true;
111123

112-
this.websocketFailureCount += 1;
124+
if (this.isLongPollingStarted) {
125+
return;
126+
}
113127

114-
if (
115-
!this.isWebsocketFailed &&
116-
this.websocketFailureCount > this.websocketMaxFailureCount
117-
) {
118-
this.isWebsocketFailed = true;
128+
console.log('initPolling:', {
129+
isLongPollingStarted: this.isLongPollingStarted,
130+
});
119131

120-
if (!this.isLongPollingStarted) {
121-
this.isLongPollingStarted = true;
122-
void this.pollSync(true);
123-
this.initCollaborationSSE();
124-
}
125-
} else if (!this.isWebsocketFailed) {
126-
super.onClose(event);
127-
}
132+
this.isLongPollingStarted = true;
133+
void this.pollSync(true);
134+
this.initCollaborationSSE();
128135
}
129136

137+
// public onClose(event: CloseEvent): void {
138+
// console.log('close:', event);
139+
140+
// if (!this.isWebsocketFailed) {
141+
// super.onClose(event);
142+
// }
143+
// }
144+
130145
protected toPollUrl(endpoint: string): string {
131146
let pollUrl = this.url.replace('ws:', 'http:');
132147
if (pollUrl.includes('wss:')) {

src/frontend/servers/y-provider/src/libs/PollSync.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import crypto from 'crypto';
33

44
import {
55
AwarenessUpdate,
6+
Connection,
67
Document,
78
Hocuspocus,
89
IncomingMessage,
@@ -11,6 +12,7 @@ import {
1112
} from '@hocuspocus/server';
1213
import { Request, Response } from 'express';
1314
import { v4 as uuid } from 'uuid';
15+
import { applyAwarenessUpdate } from 'y-protocols/awareness.js';
1416
import { readSyncMessage } from 'y-protocols/sync';
1517
import * as Y from 'yjs';
1618

@@ -124,6 +126,13 @@ export class PollSync<T> {
124126
if (type === MessageType.Sync) {
125127
message.writeVarUint(MessageType.Sync);
126128
readSyncMessage(message.decoder, message.encoder, hpDoc, null);
129+
} else if (type === MessageType.Awareness) {
130+
const awarenessUpdate = message.readVarUint8Array();
131+
applyAwarenessUpdate(
132+
hpDoc.awareness,
133+
awarenessUpdate,
134+
hpDoc.awareness.clientID,
135+
);
127136
} else {
128137
hpDoc.getConnections().forEach((connection) => {
129138
connection.handleMessage(messageBuffer);

0 commit comments

Comments
 (0)