Проблема с курсором платформы совместной работы в режиме реального времени в веб-сокете

Моя проблема заключается в позиции курсора в моем проекте. Когда веб-сокет открывается и в setTimeout отправляется код в другую комнату подключения. тогда мое положение курсора и текст в данный момент не отображаются, и мой курсор возвращается назад. и я сталкиваюсь с такими проблемами, как проблемы с игровым пингом. Я также отслеживаю курсор.

Мой код скрипта таков:

//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
    }));
}

пожалуйста, проверьте и помогите мне

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