共有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脚本。

通过这种方式,你可以实现对特定设备的精确控制,并根据需求扩展其他命令和功能。
相关推荐
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ2 小时前
Linux 查询某进程文件所在路径 命令
linux·运维·服务器
用头发抵命3 小时前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript
蓝冰凌3 小时前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js
奔跑的呱呱牛3 小时前
generate-route-vue基于文件系统的 Vue Router 动态路由生成工具
前端·javascript·vue.js
05大叔3 小时前
网络基础知识 域名,JSON格式,AI基础
运维·服务器·网络
安当加密3 小时前
无需改 PAM!轻量级 RADIUS + ASP身份认证系统 实现 Linux 登录双因子认证
linux·运维·服务器
柳杉3 小时前
从动漫水面到赛博飞船:这位开发者的Three.js作品太惊艳了
前端·javascript·数据可视化
TON_G-T4 小时前
day.js和 Moment.js
开发语言·javascript·ecmascript
Irene19915 小时前
JavaScript 中 this 指向总结和箭头函数的作用域说明(附:call / apply / bind 对比总结)
javascript·this·箭头函数