Skip to content

Commit

Permalink
test 4
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoLC committed Feb 14, 2025
1 parent 4113b57 commit b8ff4ad
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
CompleteHocuspocusProviderWebsocketConfiguration,
HocuspocusProvider,
HocuspocusProviderConfiguration,
WebSocketStatus,
onOutgoingMessageParameters,
onStatusParameters,
} from '@hocuspocus/provider';
import type { CloseEvent, MessageEvent } from 'ws';
import type { MessageEvent } from 'ws';
import * as Y from 'yjs';

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

public constructor(configuration: CollaborationProviderConfiguration) {
const withWS = false;
Expand Down Expand Up @@ -77,12 +77,15 @@ export class CollaborationProvider extends HocuspocusProvider {
}

public setPollDefaultValues(): void {
console.log('setPollDefaultValues:');
this.isLongPollingStarted = false;
this.isWebsocketFailed = false;
this.seemsUnsyncCount = 0;
this.sse?.close();
this.sse = null;
this.websocketFailureCount = 0;
if (this.pollTimeout) {
clearTimeout(this.pollTimeout);
}
}

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

public onWebsocketConnect = () => {
console.log('onWebsocketConnect:');
this.setPollDefaultValues();
};

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

public onStatus({ status }: onStatusParameters) {
console.log('status:', status);
if (status === WebSocketStatus.Connecting) {
if (this.pollTimeout) {
clearTimeout(this.pollTimeout);
}
this.pollTimeout = setTimeout(() => {
this.initPolling();
}, 5000);
} else if (status === WebSocketStatus.Connected) {
this.setPollDefaultValues();
}

super.onStatus({ status });
}

public onClose(event: CloseEvent): void {
console.log('close:', event);
this.isAuthenticated = false;
this.synced = false;
public initPolling() {
this.isWebsocketFailed = true;

this.websocketFailureCount += 1;
if (this.isLongPollingStarted) {
return;
}

if (
!this.isWebsocketFailed &&
this.websocketFailureCount > this.websocketMaxFailureCount
) {
this.isWebsocketFailed = true;
console.log('initPolling:', {
isLongPollingStarted: this.isLongPollingStarted,
});

if (!this.isLongPollingStarted) {
this.isLongPollingStarted = true;
void this.pollSync(true);
this.initCollaborationSSE();
}
} else if (!this.isWebsocketFailed) {
super.onClose(event);
}
this.isLongPollingStarted = true;
void this.pollSync(true);
this.initCollaborationSSE();
}

// public onClose(event: CloseEvent): void {
// console.log('close:', event);

// if (!this.isWebsocketFailed) {
// super.onClose(event);
// }
// }

protected toPollUrl(endpoint: string): string {
let pollUrl = this.url.replace('ws:', 'http:');
if (pollUrl.includes('wss:')) {
Expand Down
9 changes: 9 additions & 0 deletions src/frontend/servers/y-provider/src/libs/PollSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import crypto from 'crypto';

import {
AwarenessUpdate,
Connection,
Document,
Hocuspocus,
IncomingMessage,
Expand All @@ -11,6 +12,7 @@ import {
} from '@hocuspocus/server';
import { Request, Response } from 'express';
import { v4 as uuid } from 'uuid';
import { applyAwarenessUpdate } from 'y-protocols/awareness.js';
import { readSyncMessage } from 'y-protocols/sync';
import * as Y from 'yjs';

Expand Down Expand Up @@ -124,6 +126,13 @@ export class PollSync<T> {
if (type === MessageType.Sync) {
message.writeVarUint(MessageType.Sync);
readSyncMessage(message.decoder, message.encoder, hpDoc, null);
} else if (type === MessageType.Awareness) {
const awarenessUpdate = message.readVarUint8Array();
applyAwarenessUpdate(
hpDoc.awareness,
awarenessUpdate,
hpDoc.awareness.clientID,
);
} else {
hpDoc.getConnections().forEach((connection) => {
connection.handleMessage(messageBuffer);
Expand Down

0 comments on commit b8ff4ad

Please sign in to comment.