how to use y-websocket send a pong message to server side

153 views Asked by At

I am using y-websocket https://github.com/yjs/y-websocket as the server side to sync the online editor message, I am tried to send the pong message like this when receive the server side ping message:

const doWsConn = (ydoc: Y.Doc, docId: string): WebsocketProvider => {
    const wsProvider: WebsocketProvider = new WebsocketProvider(readConfig("wssUrl"), docId, ydoc, {
        maxBackoffTime: 1000000,
        params: {
            // https://self-issued.info/docs/draft-ietf-oauth-v2-bearer.html#query-param
            access_token: localStorage.getItem(WheelGlobal.ACCESS_TOKEN_NAME) ?? ""
        }
    });
    const uInfo = localStorage.getItem("userInfo");
    if (!uInfo) {
        console.error("user info is null", uInfo);
        return;
    };
    const user: UserModel = JSON.parse(uInfo);
    const ydocUser = {
        name: user.nickname,
        color: userColor.color,
        colorLight: userColor.light
    };
    const permanentUserData = new Y.PermanentUserData(ydoc);
    permanentUserData.setUserMapping(ydoc, ydoc.clientID, ydocUser.name)
    wsProvider.awareness.setLocalStateField('user', ydocUser);
    wsProvider.on('auth', (event: any) => {
        // https://discuss.yjs.dev/t/how-to-refresh-the-wsprovider-params-when-token-expire/2131
        handleWsAuth(event, wsProvider, ydoc, docId);
    });
    wsProvider.on('connection-error', (event: any) => {
        wsProvider.shouldConnect = false;
        wsProvider.ws?.close()
    });
    wsProvider.on('message', (event: MessageEvent) => {
        console.log(event.data);
    });
    wsProvider.on('ping', (event: MessageEvent) => {
        console.warn("recived ping message");
        wsProvider.ws.pong();
    });
    wsProvider.on('status', (event: any) => {
        if (event.status === 'connected') {
            if (wsProvider.ws) {

            }
        } else if (event.status === 'disconnected' && wsRetryCount < wsMaxRetries) {
            wsRetryCount++;
            setTimeout(() => {
                wsProvider.connect();
            }, 2000);
        } else {
            wsProvider.destroy();
            return;
        }
    });
    return wsProvider;
}

I already read the server side and found the y-websocket send ping message like this:

const pingInterval = setInterval(() => {
    if (!pongReceived) {
      if (doc.conns.has(conn)) {
        logger.warn('close connection pong, pong:' + pongReceived)
        closeConn(doc, conn)
      }
      clearInterval(pingInterval)
    } else if (doc.conns.has(conn)) {
      pongReceived = false
      try {
        conn.ping()
      } catch (e) {
        logger.error('close connection when ping,' + e)
        closeConn(doc, conn)
        clearInterval(pingInterval)
      }
    }
  }, pingTimeout)
  conn.on('close', () => {
    closeConn(doc, conn)
    clearInterval(pingInterval)
  })

but the client side did not recieve the ping message, Am I missing something? what should I do to implement the ping-pong mechenism?

0

There are 0 answers