1 Star 0 Fork 31

moxiaonai/Aircontroller-scrcpy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
connectDevice.js 2.96 KB
一键复制 编辑 原始数据 按行查看 历史
yanghang 提交于 2021-12-25 19:32 . 首次提交
function connectDevice(deviceId) {
if (app.connectMap[deviceId]) {
try {
app.connectMap[deviceId].end();
} catch (error) {
}
}
var buf = '';
var contentLength = 0;
app.devices[deviceId].source = "123";
try {
var _client = net.connect(
{
port: app.devices[deviceId].port, host: '127.0.0.1'
}, function (err) {
app.connectMap[deviceId] = _client;
if (err) {
console.log('connect socket server error');
app.connectMap[deviceId] = null;
return;
}
_client.on('data', function (msg) {
// _client.end()
if (msg.length > 10) {
buf += msg;
if (buf.length == contentLength) {
if (app.screensMap[deviceId] == null) {
return;
}
try {
app.screens[app.screensMap[deviceId]].source = buf;
} catch (error) {
console.debug(app.screens[app.screensMap[deviceId]])
}
app.screens[app.screensMap[deviceId]].source = buf;
buf = null;
}
}
else {
buf = 'data:image/jpeg;base64,'
contentLength = parseInt(msg) + 23;
}
})
_client.on('error', function (err) {
console.error("连接失败:重试");
setTimeout(() => {
if (app.devices[deviceId]) {
connectDevice(deviceId);
} else {
app.connectMap[deviceId] = null;
}
}, 10000);
})
_client.on('timeout', function (err) {
console.error("连接超时:重试");
setTimeout(() => {
if (app.devices[deviceId]) {
connectDevice(deviceId);
} else {
app.connectMap[deviceId] = null;
}
}, 10000);
})
_client.on('connect', function (err) {
console.log("连接设备成功");
app.connectMap[deviceId] = _client;
})
})
} catch (error) {
console.error(error);
console.error("连接失败:重试");
setTimeout(() => {
if (app.devices[deviceId]) {
connectDevice(deviceId);
} else {
app.connectMap[deviceId] = null;
}
}, 10000);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/moxiaonai/aircontroller-scrcpy.git
git@gitee.com:moxiaonai/aircontroller-scrcpy.git
moxiaonai
aircontroller-scrcpy
Aircontroller-scrcpy
master

搜索帮助