2017-06-15 6 views
1

私はシンプルなテキストツールを持っているノードとソケットを使って共同アプリケーションをやっています。いずれかのユーザーがテキストを入力してキャンバスに適用すると、接続されている他のユーザーにもそのテキストが表示されます。node.jsとsocket.ioを使ったコラボレーティブテキストツール

これは私が試したものです:テキストツール事前に

$input.keyup(function(e) { 
    if (e.which === 13) { 
    e.preventDefault(); 
    ctx.font = (2 * texttool.lineWidth) + "px sans-serif"; 
    ctx.fillStyle = texttool.color; 
    //call fillText to push the content of input to the page 
    //this parses out the input's left and top coordinates and then sets the text to be at those coordinates 
    ctx.fillText($input.val(), parseInt($input.css("left")), parseInt($input.css("top"))); 
    //save the context 
    ctx.save(); 
    //set the display to none for the input and erase its value 
    $input.css("display", "none").val(""); 

    console.log("sendtxt: "); 
    socket.emit('txt', ctx.fillText()); 
    } 
}); 

socket.on('txt', function() { 
    console.log("Text"); 
    ctx.fillText(); 
}); 

サーバー

socket.on('txt', function() { 
    console.log("Text"); 
    socket.broadcast.emit('txt', ctx.fillText()); 
}); 

おかげで

クライアント。

答えて

0

完全にcoxのテキストが何をしているのか分かりません。サーバーコードでこれを試すことができますか?また、私はctx.fillText()paramsなしで現在のテキストを返すと仮定します。

クライアント

$input.keyup(function(e) { 
    if (e.which === 13) { 
    e.preventDefault(); 
    ctx.font = (2 * texttool.lineWidth) + "px sans-serif"; 
    ctx.fillStyle = texttool.color; 
    //call fillText to push the content of input to the page 
    //this parses out the input's left and top coordinates and then sets the text to be at those coordinates 
    ctx.fillText($input.val(), parseInt($input.css("left")), parseInt($input.css("top"))); 
    //save the context 
    ctx.save(); 
    //set the display to none for the input and erase its value 
    $input.css("display", "none").val(""); 

    console.log("sendtxt: "); 
    socket.emit('txt', $input.val()); 
    } 
}); 

socket.on('txt', function(msg) { 
    console.log(msg); 
    ctx.fillText(msg); 
}); 

サーバー

socket.on('txt', function(msg) { 
    console.log(msg); 
    socket.broadcast.emit('txt', msg); 
}); 
+0

テキストがキャンバスにテキストを適用するものでなければなりません埋めます。/ – Pedro

+0

send valを$ input.val()に変更しました。文字が送受信されているかどうかを確認してください。 –

+0

まだ動作しませんでしたが、 $ input.val()を送信してください。他のユーザーは、fillTextを取得します。 – Pedro

関連する問題