问:例如我现在5部手机,如何让1号手机,执行”script.js”代码
1. 网页端(HTML + JavaScript)
在网页端添加复选框,以便选择特定的手机设备。
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket Control</title>
</head>
<body>
<h1>WebSocket Control Panel</h1>
<!-- 设备选择复选框 -->
<label><input type="checkbox" class="device-checkbox" value="phone1"> Phone 1</label><br>
<label><input type="checkbox" class="device-checkbox" value="phone2"> Phone 2</label><br>
<label><input type="checkbox" class="device-checkbox" value="phone3"> Phone 3</label><br>
<label><input type="checkbox" class="device-checkbox" value="phone4"> Phone 4</label><br>
<label><input type="checkbox" class="device-checkbox" value="phone5"> Phone 5</label><br><br>
<!-- 输入脚本名称 -->
<label for="scriptName">Script Name:</label>
<input type="text" id="scriptName" value="闲鱼.js"><br><br>
<!-- 发送命令按钮 -->
<button onclick="sendCommand()">Send Command</button>
<script>
// 连接到WebSocket服务器
const ws = new WebSocket('ws://your_computer_ip:8080');
ws.onopen = function() {
console.log('Connected to server');
};
ws.onmessage = function(event) {
console.log('Message from server:', event.data);
};
ws.onclose = function() {
console.log('Disconnected from server');
};
ws.onerror = function(error) {
console.error('WebSocket Error:', error);
};
function sendCommand() {
// 获取所有选中的设备
const checkboxes = document.querySelectorAll('.device-checkbox:checked');
const scriptName = document.getElementById('scriptName').value;
// 对每个选中的设备发送命令
checkboxes.forEach(checkbox => {
const deviceID = checkbox.value;
const command = {
type: 'sendCommand',
deviceID: deviceID,
command: {
action: 'run_script',
scriptName: scriptName
}
};
ws.send(JSON.stringify(command));
});
}
</script>
</body>
</html>
2. 服务端(Node.js)
确保服务器能够处理来自网页端的命令,并将命令转发到相应的移动设备。
server.js:
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
let clients = {};
// 当有新的客户端连接时
wss.on('connection', function(ws) {
ws.on('message', function(message) {
let msg = JSON.parse(message);
if (msg.type === 'register') {
// 记录客户端的设备ID和连接对象
clients[msg.id] = ws;
console.log(`Device ${msg.id} connected`);
} else if (msg.type === 'sendCommand') {
// 转发命令到指定设备
let targetDeviceID = msg.deviceID;
let command = msg.command;
sendCommand(targetDeviceID, command);
}
});
// 当客户端断开连接时
ws.on('close', function() {
for (let id in clients) {
if (clients[id] === ws) {
delete clients[id];
console.log(`Device ${id} disconnected`);
break;
}
}
});
});
// 发送指令到特定设备
function sendCommand(deviceID, command) {
if (clients[deviceID] && clients[deviceID].readyState === WebSocket.OPEN) {
clients[deviceID].send(JSON.stringify({ type: 'command', command: command }));
} else {
console.log(`Device ${deviceID} is not connected`);
}
}
3. 移动端(Auto.js)
确保移动端能够连接到服务端,并接收并执行命令。
Auto.js脚本:
// Auto.js脚本连接WebSocket服务器并发送设备ID
var WebSocket = require('ws');
var ws = new WebSocket('ws://your_computer_ip:8080');
var deviceID = "phone1"; // 设备唯一标识符
// 当与服务器建立连接时,发送设备ID
ws.on('open', function() {
console.log('Connected to server');
ws.send(JSON.stringify({ type: 'register', id: deviceID }));
});
// 处理从服务器接收的消息
ws.on('message', function(data) {
var message = JSON.parse(data);
if (message.type == 'command') {
executeCommand(message.command);
}
});
// 根据指令执行操作
function executeCommand(command) {
if (command.action == 'run_script') {
var scriptName = command.scriptName;
if (files.exists(scriptName)) {
engines.execScriptFile(scriptName);
} else {
console.log('Script not found: ' + scriptName);
}
}
// 添加更多操作
}
// 当与服务器断开连接时,记录日志
ws.on('close', function() {
console.log('Disconnected from server');
});
运行步骤
1.网页端:将HTML文件保存为index.html,并在浏览器中打开。
2.服务端:将Node.js代码保存为server.js,然后在终端运行node server.js。
3.移动端:将Auto.js脚本保存并在手机上运行。
在浏览器中勾选需要控制的手机设备(如Phone 1),输入脚本名称,然后点击“Send Command”按钮。此时,服务器会接收到命令并转发给相应的手机,手机会执行指定的脚本(如script.js)。
通过这种方式,你可以实现从网页端控制多个手机执行脚本的功能。


获取资源前请仔细阅读一下声明:
重要提示
如有解压密码: 看下载页、看下载页、看下载页。
源码工具资源类具有可复制性: 建议具有一定思考和动手能力的用户购买。
请谨慎考虑: 小白用户和缺乏思考动手能力者不建议赞助。
虚拟商品购买须知: 虚拟类商品,一经打赏赞助,不支持退款。请谅解,谢谢合作!
声明: 本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
