Collaborative plateform Real time cursor problem in web socket

My problem is a cursor position in my project. When the web socket open and at setTimeout send code to the another connection room. then my cursor position and text is not showing currently and my cursor go back. and i am facing experience like gaming ping issues., i am also keep track cursor.

My script code is:

//This is onmessage

if (data.action === "code_update") {
            if (editor.getValue() !== data.code) {  // Prevent redundant updates
                const cursorPosition = editor.getCursorPosition();
                console.log("Cursor: ",cursorPosition)
                editor.setValue(data.code,-1);  // Update without moving cursor to the end
                editor.moveCursorTo(cursorPosition.row, cursorPosition.column);
            }
        }
      // Send code updates on editor changes (debounced to reduce spam).
let debounceTimeout;
let lastSentCode = '';
editor.session.on("change", function () {
    clearTimeout(debounceTimeout);
    debounceTimeout = setTimeout(() => {
        const currentCode = editor.getValue();
        if (currentCode.trim() !== lastSentCode.trim()) {
            sendCodeUpdate(currentCode);
            lastSentCode = currentCode.trim();  // Update the last sent code
        }
    },100);
});

    // Send code updates on the WebSocket
async function sendCodeUpdate(code) {
    const cursorPosition = editor.getCursorPosition();  // Store cursor position
    await chatSocket.send(JSON.stringify({
        action: "code_update",
        username: username,
        code: code,
        cursor_position: cursorPosition,  // Send cursor position
    }));
}

please check and help me

Вернуться на верх