文章目录
概要
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后同步一下剪切板就行了,这样完美实现本地复制到远程操作~~~