阅读量:0
思想:例如我现在5部手机,如何让1号手机,执行"闲鱼.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'); });