Skip to content

Commit

Permalink
daily-12-14-成功在手机上启动helloworld
Browse files Browse the repository at this point in the history
  • Loading branch information
snailuncle committed Dec 14, 2018
1 parent 578a5f7 commit 2d6d88c
Show file tree
Hide file tree
Showing 24 changed files with 697 additions and 62 deletions.
157 changes: 115 additions & 42 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const port = require("./config.json")
const config = require("./config.js")
const Koa = require('koa');
const render = require('./src/index');
const path = require('path');
const bodyParser = require('koa-bodyparser')
const controller = require('./controller');
const send = require('koa-send'); // "koa-send": "^4.1.0"

//引入子模块子路由
const router = require('koa-router')();
//websocket要导入的包
Expand Down Expand Up @@ -58,42 +61,7 @@ router.use('/user', user)
// 'GET /runIndex': fn_runIndex


var net = require("net");
var config = require('./config')
/**
* 创建server
*/

var server = net.createServer(function (socket) {
var t = config.getTime()
socket.write(t + "hello,i'm nodejs!"+"\r\n");
console.log(t + "client connected! %j:%j", socket.remoteAddress, socket.remotePort);
socket.on("data", function (data) {
var t = config.getTime()
console.log(t + "recived from autojs:", data.toString());
socket.write(t + "这是来自nodejs的数据->海贼王啥时候完结??"+"\r\n");
})
socket.on("close", function (had_error) {
if (!had_error) {
console.log("client closed success! %j:%j", socket.remoteAddress, socket.remotePort);
} else {
console.log("client close error! %j:%j", socket.remoteAddress, socket.remotePort);
}
})
socket.on("error", function (err) {
console.log("!!!err!!!", err);
});
//setTimeout(function(){
// socket.end("我结束了","utf8");
//},3000);
});
server.listen({
port: config.port
}, function () {
var t = config.getTime()
var address = server.address();
console.log(t + " opened server on address %j ", address);
});



Expand All @@ -107,9 +75,50 @@ router.get('/runIndex/:projectName',async (ctx,next) => {
var folder='./projectList/'+projectName
var zipFilePath=zipFolder(folder)
//告诉手机下载指定项目的压缩包
tellMobileDownloadProjectZipFile(zipFilePath)
//下载完毕手机自动运行该项目中的index.js
var 版本号=1
// 版本号读取所选项目的index.js文件,
// 查看scriptVersionNumber字段
var fs = require('fs');
var indexFilePath=folder+'/index.js'
if (!fs.existsSync(indexFilePath)) {
// Do something
console.log('请在项目中建立index.js文件');
console.log('文件中必须有一行内容来指定项目版本号,如下');
console.log('scriptVersionNumber=1');

process.exit(1)
}



console.log('--------读取index.js文件开始--------');

var fileContent = fs.readFileSync(indexFilePath, 'utf-8');

console.log('--------读取index.js文件结束--------');

//检查版本号
var reg=/scriptVersionNumber=(\d+)/
var 版本号=fileContent.match(reg)[1]









var 服务器下载端口=port.httpPort
project={
"projectName":projectName,
"scriptVersionNumber":版本号,
'port':服务器下载端口
}
tellMobileDownloadProjectZipFile(project)
//下载完毕手机自动运行该项目中的index.js
//默认版本号为1 如果在index中发现版本号,以index中的版本号为准
//如果手机上的版本号小于当前版本号,那么就更新脚本.



Expand All @@ -123,12 +132,14 @@ function zipFolder(folder){
}


function tellMobileDownloadProjectZipFile(zipFilePath){
function tellMobileDownloadProjectZipFile(projectName){
var t = config.getTime()
console.log(t)
// console.log('6秒后通知手机更新脚本');

// setTimeout(childSendMsg,6000)
childSendMsg(projectName)

socket.write(t + "hello,i'm nodejs!"+"\r\n");
socket.write(t + "please download zipFile!"+"\r\n");
socket.write(t + "zipFilePathIs"+zipFilePath+"\r\n");

}

Expand All @@ -153,6 +164,21 @@ function tellMobileDownloadProjectZipFile(zipFilePath){
// })


//下载文件
router.get('/download/:fileName', async function (ctx) {
// 为了方便演示,这里直接下载index页面


var fileName = ctx.params.fileName+'.zip';
console.log('要下载的文件的名字='+fileName)
// Set Content-Disposition to "attachment" to signal the client to prompt for download.
// Optionally specify the filename of the download.
// 设置实体头(表示消息体的附加信息的头字段),提示浏览器以文件下载的方式打开
// 也可以直接设置 ctx.set("Content-disposition", "attachment; filename=" + fileName);
ctx.attachment(fileName);
await send(ctx, fileName, { root: __dirname + '/zipFolder' });
});


app.use(router.routes()).use(router.allowedMethods())
// app.use(async function (ctx) {
Expand All @@ -164,7 +190,54 @@ app.use(router.routes()).use(router.allowedMethods())
console.log('port=')
console.log(port)
app.listen(port.httpPort);
console.log('open http://localhost:' + port.httpPort);
console.log('open http://localhost:' + port.httpPort);
app.on('error', function (err) {
console.log(err.stack);
});



//以下是socket部分,fork一个子进程,以便与手机通信===================================================================================


const child = require('child_process').fork('./socketServer.js');


child.on('message', (msg) => {
console.log('大头儿子说->'+msg)
});

function childSendMsg(projectName){
console.log('启动childSendMsg函数');
console.log('现在通知手机更新脚本')
child.send('小头爸爸说->大头儿子,让他们更新脚本吧');

// setTimeout(发送项目更新信息,6000)

发送项目更新信息(projectName)
}
function 发送项目更新信息(project){
console.log("发送项目更新信息");

// var projectName=project.projectName
// var scriptVersionNumber=project.scriptVersionNumber
// var 项目更新信息={
// "projectName":projectName,
// "scriptVersionNumber":scriptVersionNumber
// }
项目更新信息=JSON.stringify(project)
child.send('项目更新信息'+项目更新信息)
}






// function clock(){
// var t = new Date();
// console.log(t);

// }
// setInterval(clock, 5000);

8 changes: 8 additions & 0 deletions autojsTest/1111111.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enginesAll = engines.all()
log(enginesAll)
enginesAll.map((ScriptEngine) => {
if (engines.myEngine() == ScriptEngine) {} else {
console.log('即将停止的脚本引擎' + ScriptEngine)
ScriptEngine.forceStop()
}
})
10 changes: 10 additions & 0 deletions autojsTest/aaa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

fn=()=>{
// var s=ScriptEngine.cwd()
var s=engines.myEngine().getSource()
console.log(s)
}

setInterval(fn,2000)


7 changes: 7 additions & 0 deletions autojsTest/bbb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn=()=>{
// var s=ScriptEngine.cwd()
var s=engines.myEngine().getSource()
console.log(s)
}

setInterval(fn,2000)
9 changes: 9 additions & 0 deletions autojsTest/ccc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn=()=>{
// var s=ScriptEngine.cwd()
var s=engines.myEngine().getSource()
console.log(s)
console.log('ccc的fn')
}

setInterval(fn,2000)
console.log('ccc存在过')
18 changes: 18 additions & 0 deletions autojsTest/downloadFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Response.body
{Object}
当前响应的内容。他有以下属性和函数:

bytes() {Array} 以字节数组形式返回响应内容


console.show();
http.get("www.baidu.com", {}, function(res, err){
if(err){
console.error(err);
return;
}
log("code = " + res.statusCode);
var bodyBytes=res.body.bytes()
var path='./'
files.writeBytes(path, bytes)
});
20 changes: 20 additions & 0 deletions autojsTest/停止除了自身的脚本.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
脚本数组 = ['aaa', 'bbb', 'ccc']
脚本数组.map((file) => {
engines.execScriptFile(files.cwd() + "/" + file + ".js")
})
end = () => {
enginesAll = engines.all()
log(enginesAll)
enginesAll.map((ScriptEngine) => {
if (engines.myEngine().toString() != ScriptEngine.toString()) {
console.log('即将停止的脚本引擎' + ScriptEngine)
ScriptEngine.forceStop()
}
})
for (let i = 0; i < 10; i++) {
sleep(1000)
console.log(213);

}
}
setTimeout(end, 5000)
6 changes: 6 additions & 0 deletions autojsTest/解压文件.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

function 解压文件(fileName){
var fileName=files.path('./'+fileName)
var unzipFileName=fileName.replace('.zip','')
com.stardust.io.Zip.unzip(new java.io.File(fileName), new java.io.File(unzipFileName))
}
1 change: 1 addition & 0 deletions clientInfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"xiaomi/ugglite/ugglite:7.1.2/N2G47H/8.3.1:user/release-keys":{"屏幕宽度":720,"屏幕高度":1280,"buildId":"N2G47H","主板":"QC_Reference_Phone","制造商":"xiaomi","型号":"Redmi Note 5A","产品名称":"ugglite","硬件名称":"qcom","唯一标识码":"xiaomi/ugglite/ugglite:7.1.2/N2G47H/8.3.1:user/release-keys","AndroidId":"eff4c619a4f92b20","API":25,"ip":"::ffff:192.168.12.100","port":51615,"客户端登记时间":"2018-12-14 12:50:40"}}
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function getTime() {
// 2018-08-02 21:22:07
}
module.exports = {
"serverIP": "192.168.12.104",
"serverIP": "192.168.12.101",
"port": "8811",
"getTime": getTime
}
33 changes: 33 additions & 0 deletions downloadFileTest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>__小简__下载测试</title>
<style>
div {
width: 100%;
text-align: center;
margin-top: 50px;
}
</style>
<script>
window.onload=function(){
var oBtnDownload=document.getElementById('download');
oBtnDownload.onclick=function(){
window.open('http://localhost:3000/download', '_blank')
}
}
</script>
</head>

<body>
<div>
<button id="download">点击下载</button>
</div>
</body>

</html>

29 changes: 29 additions & 0 deletions downloadFileTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const koa = require('koa'); // "koa": "^2.2.0"
const app = new koa();
const router = require('koa-router')(); // "koa-router": "^7.2.0"
const send = require('koa-send'); // "koa-send": "^4.1.0"

router.get('/', async function (ctx) {
var fileName = 'index.html';
await send(ctx, fileName, { root: __dirname + '/public' });
});

router.get('/download', async function (ctx) {
// 为了方便演示,这里直接下载index页面
var fileName = 'index.html';
// Set Content-Disposition to "attachment" to signal the client to prompt for download.
// Optionally specify the filename of the download.
// 设置实体头(表示消息体的附加信息的头字段),提示浏览器以文件下载的方式打开
// 也可以直接设置 ctx.set("Content-disposition", "attachment; filename=" + fileName);
ctx.attachment(fileName);
await send(ctx, fileName, { root: __dirname + '/public' });
});

app
.use(router.routes())
.use(router.allowedMethods());

port=3000
app.listen(port);

console.log('open http://localhost:' + port);
10 changes: 5 additions & 5 deletions git常用命令.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ git status -s
git branch

创建分支
git checkout master -b daily-12-14-服务器和手机正常收发脚本更新信息
git checkout master -b daily-12-14-成功在手机上启动helloworld

提交到暂存区
git add .

提交评论
git commit -m 'daily-12-14-服务器和手机正常收发脚本更新信息'
git commit -m 'daily-12-14-成功在手机上启动helloworld'

推送到分支
git push origin daily-12-14-服务器和手机正常收发脚本更新信息
git push origin daily-12-14-成功在手机上启动helloworld


切换主干||分支
git checkout master || git checkout daily-10-1

本地合并分支到主干
git merge daily-12-14-服务器和手机正常收发脚本更新信息
git merge daily-12-14-成功在手机上启动helloworld

远端合并分支到主干
git merge origin/daily-12-14-服务器和手机正常收发脚本更新信息
git merge origin/daily-12-14-成功在手机上启动helloworld

推送主干
git push origin master
Expand Down
Loading

0 comments on commit 2d6d88c

Please sign in to comment.