Guacamole client 如何真正完美实现本地和远程桌面双向复制

文章目录

概要

Guacamole client 如何真正完美实现本地和远程桌面双向复制,非残疾版本地向远程复制还要在浏览器复制后才能复制到远程

搭建服务器什么的就不说了,以前端JS上代码:

Guacamole 版本用的1.5.5

         // 添加剪切板事件处理程序
          guac = new Guacamole.Client(new Guacamole.WebSocketTunnel(url));
          //省略连接等。。。。
          //连接完成后先调用一次,同步本地剪切板到远程
          if (document.hasFocus()) {
            const current_text = await navigator.clipboard.readText();
            let stream = guac.createClipboardStream('text/plain');
            let writer = new Guacamole.StringWriter(stream);
            writer.sendText(current_text);
            writer.sendEnd();
          }
          //远程复制到本地
          guac.onclipboard = function (stream, mimetype) {
            let reader = new Guacamole.StringReader(stream);
            let receivedData = '';
            reader.ontext = function (text) {
              receivedData += text;
            };
            reader.onend = function () {
              // 将接收到的数据放到本地剪切板
              navigator.clipboard.writeText(receivedData).then(function () {
              }).catch(function (err) {
              });
            };
          };
          //本地复制到远程
          let text = ""
          window.addEventListener('focus', async () => {
            const current_text = await navigator.clipboard.readText();
            if (text != current_text) {
              text = current_text;
              let stream = guac.createClipboardStream('text/plain');
              let writer = new Guacamole.StringWriter(stream);
              writer.sendText(text);
              writer.sendEnd();
            }
          });

代码核心思想是本地复制动作浏览器无法监听,另辟思路,当远程桌面获取focus后同步一下剪切板就行了,这样完美实现本地复制到远程操作~~~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值