【发布时间】:2016-02-13 03:59:01
【问题描述】:
我需要创建一个 NodeJS 脚本,该脚本将从我的 VPS 上托管的脚本自动更新。为此,我需要从我的 VPS 获取代码并将其发送到客户端,客户端将在客户端执行代码,就好像它在客户端中一样。我不知道如何实现这一目标... 我有什么:
客户:
WebSocket = require('ws')
ws = new WebSocket('ws://localhost:8720');
var getinit_init_key = "jN*&gbhh*&G8ihae8rwgh78g&*G&*G&GFUibg&GB*&GVBWG";
var getINITKey = JSON.stringify({ init: getinit_init_key, userip: 'server', reason: 'getUpdate' });
ws.on('open', function open() {
ws.send(getINITKey);
});
ws.on('message', function(data, flags) {
d = JSON.parse(data)
d.x();
});
服务器:
update1 = require('./socks.js') //socks.js is my code
update2 = update1.init
update = JSON.stringify({ x: update2.toString() });
var ws = require("nodejs-websocket");
var server = ws.createServer(function(conn) {
console.log("Got new connection!");
conn.on("text", function(data) {
try {
conn.sendText(update)
console.log(JSON.parse(update).x);
}
catch (error) {
console.log("RECOVERED FROM ERROR: " +error)
}
});
conn.on("close", function(code, reason) {
console.log('Sent Update to User');
});
conn.on("error", function(error) {
console.log("Recovered from Error");
});
}).listen(8720);
【问题讨论】:
标签: javascript node.js get