最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

django - collaborative plateform Real time cursor problem in web socket - Stack Overflow

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论