code-push和code-push-server

要使用code-push-server,來替換react-native-code-push微軟的服務(wù)器,也可以搭建本地服務(wù),方便自測。

CodePush Server is a CodePush progam server! microsoft CodePush cloud is slow in China, we can use this to build our's. I use qiniu to store the files, because it's simple and quick! Or you can use local storage, just modify config.js file, it's simple configure.

這篇文章的目的:搭建本地的服務(wù),可以發(fā)布熱更新,方便本地化測試。

1 首先安裝code-push-server

npm install code-push-server -g
通過npm安裝code-push-server,當(dāng)然也有其它方式,在code-push-server寫的很清楚。

2.初始化mysql數(shù)據(jù)庫

code-push-server-db init --dbhost localhost --dbuser root --dbpassword

先了解下code-push-server-db的命令參數(shù):


code-push-server-db

這code-push-server-db init這條命令是操作MySQL數(shù)據(jù)庫的。

如果出現(xiàn)connect ECONNREFUSED錯誤:

connect ECONNREFUSED

說明是你的MySQL服務(wù)沒有開啟:

MySQL

如果安裝了的話,而且是通過dmg安裝包安裝的話,需要到 系統(tǒng)偏好設(shè)置 最下面的MySQL->Start MySQL Server。
如果是通過brew安裝的mysql的話:
啟動:mysql.server start 停止:mysql.server stop

如果出現(xiàn)Access denied for user 'root'@'localhost' (using password: NO)

密碼錯誤

這個是因?yàn)槊艽a錯誤,是訪問數(shù)據(jù)庫的密碼不對,而且是因?yàn)闆]有輸入密碼,如果是第一次通過dmg安裝,安裝成功后,會彈出一個小窗,告訴你默認(rèn)用戶名和密碼,當(dāng)然你也可以更改。
一般添加上自己的密碼之后就可以創(chuàng)建成功了,下圖的錯誤是因?yàn)槲乙呀?jīng)創(chuàng)建過codepush數(shù)據(jù)庫了,

exists

我特意又新建了一個數(shù)據(jù)庫newcodepush,創(chuàng)建成功如下:

success

3.起服務(wù)

起服務(wù)之前需要做一件特別重要的事情,就是修改config.js文件。目的是本地測試,
只需要關(guān)心db、local和common的配置就行。
config.js文件路徑:/usr/local/lib/node_modules/code-push-server/config/config.js

  // Config for database, only support mysql.
  db: {
    username: process.env.RDS_USERNAME || "root",
    password: process.env.RDS_PASSWORD || "123456",//你的MySQL訪問密碼,如果沒有就null
    database: process.env.DATA_BASE || "codepush",,//如果你init的時候指定了數(shù)據(jù)庫名字的話,也需要改
    host: process.env.RDS_HOST || "127.0.0.1",
    port: process.env.RDS_PORT || 3306,
    dialect: "mysql",
    logging: false
  },
  // Config for qiniu (http://www.qiniu.com/) cloud storage when storageType value is "qiniu".
  qiniu: {
    accessKey: "",
    secretKey: "",
    bucketName: "",
    downloadUrl: "" // Binary files download host address.
  },
  // Config for Amazon s3 (https://aws.amazon.com/cn/s3/) storage when storageType value is "s3".
  s3: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    sessionToken: process.env.AWS_SESSION_TOKEN, //(optional)
    bucketName: process.env.BUCKET_NAME,
    region: process.env.REGION,
    downloadUrl: process.env.DOWNLOAD_URL, // binary files download host address.
  },
  // Config for Aliyun OSS (https://www.aliyun.com/product/oss) when storageType value is "oss".
  oss: {
    accessKeyId: "",
    secretAccessKey: "",
    endpoint: "",
    bucketName: "",
    prefix: "", // Key prefix in object key
    downloadUrl: "", // binary files download host address.
  },
  // Config for local storage when storageType value is "local".
  local: {
    // Binary files storage dir, Do not use tmpdir and it's public download dir.
    storageDir: process.env.STORAGE_DIR || "/Users/tablee/workspaces/storage",//需要你自己創(chuàng)建一個文件路徑,把你的路徑填上去或者按給定的路徑創(chuàng)建文件夾
    // Binary files download host address which Code Push Server listen to. the files storage in storageDir.
    downloadUrl: process.env.LOCAL_DOWNLOAD_URL || "http://localhost:3000/download",//注意此地方是否是你的本機(jī)ip地址(如果是模擬器的話,無須更改)
    // public static download spacename.
    public: process.env.PUBLIC || '/download'
  },
  jwt: {
    // Recommended: 63 random alpha-numeric characters
    // Generate using: https://www.grc.com/passwords.htm
    tokenSecret: process.env.TOKEN_SECRET ||'INSERT_RANDOM_TOKEN_KEY'
  },
  common: {
    /*
     * tryLoginTimes is control login error times to avoid force attack.
     * if value is 0, no limit for login auth, it may not safe for account. when it's a number, it means you can
     * try that times today. but it need config redis server.
     */
    tryLoginTimes: 0,
    // CodePush Web(https://github.com/lisong/code-push-web) login address.
    //codePushWebUrl: "http://localhost:3001/login",
    // create patch updates's number. default value is 3
    diffNums: 3,
    // data dir for caclulate diff files. it's optimization.
    dataDir: process.env.DATA_DIR || "/Users/tablee/workspaces/data",,//需要你自己創(chuàng)建一個文件路徑,把你的路徑填上去或者按給定的路徑創(chuàng)建文件夾
    // storageType which is your binary package files store. options value is ("local" | "qiniu" | "s3")
    storageType: process.env.STORAGE_TYPE || "local",
    // options value is (true | false), when it's true, it will cache updateCheck results in redis.
    updateCheckCache: false
  },

配置好config.js文件后,就可以啟動服務(wù)code-push-server,在瀏覽器中打開 http://127.0.0.1:3000,點(diǎn)擊登錄,輸入用戶名:admin和密碼:123456點(diǎn)擊登錄,然后點(diǎn)擊獲取token:

獲取token
token

到此為止,code-push-server的事情已經(jīng)配置好了,然后就是code-push了。

使用code-push login http://127.0.0.1:3000,這個地方會自動打開剛才的http://127.0.0.1:3000網(wǎng)頁,如果剛才沒有在瀏覽器中獲取token,在這個地方也是同樣的步驟。
獲取token后,復(fù)制上去就行了。

code-push login

然后的步驟就是要添加app了,添加之前先了解下code-push的相關(guān)命令:

code-push command

code-push app 的相關(guān)命令:

code-push app command

code-push app add 的使用方式:

app add usage

好了,我們需要添加一個應(yīng)用:code-push app add CodePushDemo-ios ios react-native

app add

我們獲取了Deployment Key,可以去配置info.plist中配置了,

Name String
CodePushDeploymentKey DeploymentKey
CodePushServerURL http://127.0.0.1:3000
info.plist

在info.plist中添加上面2個字段(這個demo中已經(jīng)有了)把我們獲取的Staging對應(yīng)的Deployment Key填到上面就行了。

然后我們先運(yùn)行code-push-demo-app-master->ios->CodePushDemoApp.xcodeproj工程,如圖:


iOS熱更新前

我們稍微修改點(diǎn)demo.js中的文字,然后code-push release-react CodePushDemo-ios ios
可以使用code-push release-react --help查看release-react的使用方法:

release-react help

發(fā)布成功:(忽略Could not parse response)

release-react

然后點(diǎn)擊模擬器上的Press for dialog-driven sync會出現(xiàn)彈窗:

點(diǎn)擊 dialog-driven

點(diǎn)擊install后,更新應(yīng)用:

iOS熱更新后

如果是真機(jī)測試的話,需要把config.js文件中common下的sownloadUrl地址和Xcode->info.plist中的CodePushServerURL都改成你當(dāng)前Wifi下的IP地址

ip

info.plist中

info.plist

config.js中

config.js

中間可能會出現(xiàn)的其它問題:

1.
報(bào)錯

上圖問題可能是node版本的問題,我升級node版本后就好了。

2.發(fā)布的時候,Cannot find module 'AccessibilityInfo'。這個問題搜了一下,好多人都有,因?yàn)槲野裷eact-native從0.44.3升級到了0.51.0,同事那里pull下來之后就一直出現(xiàn)這個問題,他被坑了半天,才解決的,我這邊改著改著也出現(xiàn)了,請教一下,只需要把react-native的版本降回去,然后項(xiàng)目能運(yùn)行,再升上來就行了??。

參考:
code-push使用教程。
React Native應(yīng)用部署/熱更新-CodePush最新集成總結(jié)(新)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容