codepush2之配置自己的服务器

来源:互联网 发布:淘宝达人帐号简介范例 编辑:程序博客网 时间:2024/06/06 09:44

这片文章是给使用过codepush的人看的如果没有用过codepush请参考http://blog.csdn.net/u014041033/article/details/73497708这篇文章。
codepush会去访问微软的服务器,在国内网速特别慢基本上和不能用没有什么区别。今天我们就来配置自己的服务器。
好处
1:网络不受限制
2:自是服务器端变化了codepush的命令依旧可以使用功能上不受影响。

配置步骤

这里我们以本地服务器为例。

服务器端:

一:下载mysql https://dev.mysql.com/downloads/mysql/数据库
E8892167-0BFE-4593-88EB-F6C879925402.png
然后运行状态:
106080F6-5874-4FAA-8040-E4D403BB0B37.png

二:
git clone https://github.com/lisong/code-push-server.git
cd code-push-server
npm install
修改code-push-server/config/config.js配置文件
1:配置db

 db: {    username: "root",    password: "xxx",    database: "codepush",    host: "127.0.0.1",    port: 3306,    dialect: "mysql",    logging: false  },

2:配置local

  local: {    // Binary files storage dir, Do not use tmpdir and it's public download dir.    storageDir: "/Users/wuyunqiang/Documents/db/storage",//自定义    // Binary files download host address which Code Push Server listen to. the files storage in storageDir.    downloadUrl: "http://127.0.0.1:3000/download",//这里ip+端口改成本地的    // public static download spacename.    public:'/download'  },

3:配置jwt

 jwt: {    // Recommended: 63 random alpha-numeric characters    // Generate using: https://www.grc.com/passwords.htm 随便写点什么 貌似没啥用 不过还是更具网站的随机生成一个吧    tokenSecret:'xxxxxxxx....'  },

4:配置common

  common: {    // data dir for caclulate diff files. it's optimization. 配置数据存储位置。    dataDir: "/Users/wuyunqiang/Documents/db/data",    storageType: "local",  },

三:初始化数据库
code-push-server-db init –dbhost localhost –dbuser root –dbpassword password//初始化数据库用户名和密码

四:启动数据库./bin/www 启动服务
binwwwsuccess.png

客户端

一:登录自己的服务器
因为我是本地服务器所以 code-push login http://127.0.0.1:3000/
logindb.png
登录窗口 用户名admin 密码:123456
获取token复制到终端。
36E4DA1F-897E-4336-9C8C-5D102A67AF1C.png
二:添加要热更新的APP
code-push app add appname android/ios react-native
三:将添加app获取的key配置到Android/iOS的代码中
setverkey.png
四:
Android配置src/java/名字/MainApplication

  new CodePush("xxxxxx",                    getApplicationContext(), BuildConfig.DEBUG,"http://172.28.108.3:3000/")

服务器的ip,我配置的是本地网址 http://172.28.108运行在手机上的不能是127.0.0.1。
ios:配置info.plist

<key>CodePushDeploymentKey</key>    <string>xxx</string>    <key>CodePushServerURL</key>    <string>http://172.28.108.3:3000/</string>

以上服务器和客户端配置完成。

使用

    InteractionManager.runAfterInteractions(() => {            CodePush.sync({                updateDialog: {                    appendReleaseDescription:true,                    descriptionPrefix:'更新内容:',                    mandatoryContinueButtonLabel:'更新',                    mandatoryUpdateMessage:'有新版本了,请您及时更新',                    optionalInstallButtonLabel: '立即更新',                    optionalIgnoreButtonLabel: '稍后',                    optionalUpdateMessage:'有新版本了,是否更新?',                    title: '提示'                },                installMode: CodePush.InstallMode.IMMEDIATE            });        })

改过后的应用上传:
code-push release-react <添加的APPName> android/ios –t 1.0.0(版本) –d Staging/Production –des “更新提示” -m false

上传文件成功后可以在/Users/wuyunqiang/Documents/db/data和/Users/wuyunqiang/Documents/db/storage看到文件
data.png

storage.png
运行截图:
ios.png

android.png

iosupdatesuccess.png

bug:
1:
codepushserverbug.png
这是数据库安装的问题。卸了再重新安装。

2:serverios.png
客户端地址错误。
iOS可能是info.plist的字段写错了,注意大小写.

3:android端可以收到更新提示框,但是点击下载更新就会报错如图。
codepushandroidrefused.png
原因:这个原因是因为config里面downloadUrl配置有问题,这里即便是自己本机作为服务器也不要127.0.0.1 换成电脑的ip即可。
ADF506B1FFFA0ECD3BFD1EAEF34B39EE.png
4:另外注意收集和电脑要在同一个网段先才能访问到你电脑。