共有5部手机,如果通过服务器让1号手机执行打开 “闲鱼.js“ 脚本

1.手机端Auto.js脚本:

每部手机需要在连接时发送一个唯一的标识符(如设备ID),并接收服务器发送的指令以执行指定的脚本。

cpp 复制代码
// 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');
});
2.电脑端Node.js WebSocket服务器:

在服务器中,维护一个客户端连接列表,并根据设备ID发送指令。

cpp 复制代码
// Node.js WebSocket服务器
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`);
        }
    });

    // 当客户端断开连接时
    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`);
    }
}

// 示例:向手机1发送执行脚本的指令
sendCommand('phone1', { action: 'run_script', scriptName: '闲鱼.js' });
3.运行服务器和测试:

确保每部手机上的Auto.js脚本正常运行并能连接到服务器。在电脑上运行Node.js服务器。使用sendCommand函数向特定设备发送指令,测试是否手机1能接收到并执行闲鱼.js脚本。

通过这种方式,你可以实现对特定设备的精确控制,并根据需求扩展其他命令和功能。
相关推荐
QC班长4 小时前
Maven公司私库配置踩坑点
java·服务器·maven·intellij-idea
猫3285 小时前
v-cloak
前端·javascript·vue.js
AC赳赳老秦5 小时前
OpenClaw二次开发实战:编写专属办公自动化技能,适配个性化需求
linux·javascript·人工智能·python·django·测试用例·openclaw
旷世奇才李先生5 小时前
Vue 3\+Vite\+Pinia实战:企业级前端项目架构设计
前端·javascript·vue.js
Ulyanov6 小时前
《PySide6 GUI开发指南:QML核心与实践》 第二篇:QML语法精要——构建声明式UI的基础
java·开发语言·javascript·python·ui·gui·雷达电子对抗系统仿真
聚美智数7 小时前
企业实际控制人查询-公司实控人查询
android·java·javascript
mfxcyh8 小时前
使用MobaXterm配置nginx
java·服务器·nginx
pengyi8710159 小时前
动态IP池快速更换实操方案,5分钟完成IP替换
服务器·网络·tcp/ip
herinspace9 小时前
管家婆实用贴-如何分离和附加数据库
开发语言·前端·javascript·数据库·语音识别
不做无法实现的梦~9 小时前
Linux 上使用 CLion 开发嵌入式,并用 Codex CLI
linux·运维·服务器