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

1.手机端Auto.js脚本:

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

// 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发送指令。

// 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脚本。

通过这种方式,你可以实现对特定设备的精确控制,并根据需求扩展其他命令和功能。
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值