天天看點

跨平台視訊通信項目-OpenTok

1 理論知識

1.1 OpenTok平台簡介

即時視訊通信日益成為主流服務,通過WebRTC,開發者可以輕松地将即時視訊通信功能加入到應用中。視訊聊天創業公司TokBox推出OpenTok,可實作浏覽器與iOS裝置間跨平台視訊聊天。

OpenTok平台可以輕松實作以下功能:

- 高品質的互動視訊

- 消息傳遞

- 螢幕分享

1.2 基本概念

1.2.1 用戶端SDK

一組可用于Web(JavaScript),IOS和Android的代碼庫,用于設定用戶端(處理大多數OpenTok的功能能),功能包括

- 釋出會話中的流

- 訂閱會話中的流

- 監聽會話事件

1.2.2 伺服器SDK

伺服器SDK是部署于應用伺服器上,可以使用Node,PHP,Java,.NET,Python和Ruby語言分裝OpenTok REST API,為用戶端生成新的會話和令牌。

1.2.3 OpenTok REST API

HTTP接口,通過調用該接口可以調用OpenTok伺服器SDK的許多方法,其餘建立會話和處理進階功能需要由OpenTok Cloud來完成,如歸檔和廣播。

1.2.4 OpenTok Cloud

管理會話,用戶端連接配接,API調用,信令,事件以及用戶端SDK或伺服器SDK未處理的其他所有内容。

1.3 OpenTok該平台包含庫

- Web client libraries(浏覽器用戶端庫)

- IOS client libraries(IOS用戶端庫)

- Android client libraries(安卓用戶端庫)

- Server-Side SDKs(伺服器端SDK)

- Server-Side REST API(伺服器端REST接口)

1.4 系統架構

1.4.1 伺服器

- 伺服器端SDK支援語言有

 -- Node

 --  PHP

 --  Java

 --  .NET

 -- Python

 -- Ruby

- 伺服器端功能生成憑據

1.4.2 用戶端

 -- 處理大部分的OpenTok功能

 -- 連接配接到會話

 -- 将音頻視訊流釋出到會話

 -- 訂閱其他用戶端的流

1.5 功能闡述

<a href="https://s5.51cto.com/wyfs02/M01/9A/69/wKiom1lVp9HiWp-AAAFddK5G9ig702.png" target="_blank"></a>

1.5.1 用戶端

 -- 調用用戶端SDK

 -- 從伺服器擷取會話ID和令牌

 -- 使用令牌連接配接到會話

 -- 訂閱音頻視訊流

 -- 監聽會話事件

1.5.2 應用伺服器

- 調用伺服器SDK

- 在OpenTok雲中建立會話

- 為用戶端生成令牌

- 向用戶端發送會話ID和令牌

1.5.3 會議

- OpenTok雲中的聊天室

- 将用戶端彼此連接配接

- 向用戶端發送時間

1.6 OpenTok的通訊過程分析

1.6.1 連接配接和釋出訂閱

Step1 由應用伺服器建立會話

<a href="https://s3.51cto.com/wyfs02/M00/9A/69/wKiom1lVqA-wioBGAAFzfcJavqA426.png" target="_blank"></a>

應用伺服器調用OpenTok伺服器SDK代碼,通過OpenTok REST API在雲中建立一個會話,并取得會話ID,将會議當做視訊聊天的房間。

Step2 用戶端加載應用程式,伺服器建立令牌

<a href="https://s1.51cto.com/wyfs02/M02/9A/69/wKiom1lVqCTh0O-6AAF6X53c8Js392.png" target="_blank"></a>

當使用者加載OpenTok Client SDK建構用戶端應用程式時,用戶端(網頁或移動應用程式)從伺服器擷取會話資訊(包括伺服器建立的唯一身份驗證令牌,相當于用戶端與伺服器校驗的密碼)

step3用戶端連接配接并開始流會話流

<a href="https://s4.51cto.com/wyfs02/M01/9A/69/wKiom1lVqDTQPFwtAAGV-iklD-Q381.png" target="_blank"></a>

- 用戶端使用會話ID和令牌建立會話連接配接

- 用戶端将音頻視訊流釋出到會話并監聽重要事件(例如加入會話的新使用者)

step4 新的用戶端連接配接到會話

<a href="https://s2.51cto.com/wyfs02/M02/9A/69/wKiom1lVqELwZuz5AAGiNM_pgE8647.png" target="_blank"></a>

- 當心使用者在單獨的網頁或移動端(用戶端2)中加載用戶端應用程式時

- 新用戶端從應用伺服器連接配接收到會話ID和唯一的令牌

- 用戶端使用這些資訊來建立會話連接配接。

step5 客戶訂閱對方的流

<a href="https://s4.51cto.com/wyfs02/M00/9A/69/wKiom1lVqFCDqrmeAAGfCj-vwyo729.png" target="_blank"></a>

- 新的用戶端連接配接到會話

- 用戶端2可以訂閱用戶端1的流

- 用戶端2将自己的視訊流釋出到會中并且用戶端1訂閱該視訊流

- 兩個用戶端都一對一地訂閱對方視訊流,且兩端都在傾聽新的事件(如新的會話使用者)

1.7 開發中心的資源

1.7.1 Helo World

快速示範最基本的OpenTok功能。

<a href="https://tokbox.com/developer/quickstart/" target="_blank">https://tokbox.com/developer/quickstart/</a>

1.7.2 教程

建構OpenTok應用程式并添加進階功能逐漸演練。

<a href="https://tokbox.com/developer/tutorials/" target="_blank">https://tokbox.com/developer/tutorials/</a>

1.7.3 代碼示例

使用示例應用程式GitHub repos清單,可幫助你更快地建構應用程式。

<a href="https://tokbox.com/developer/samples/" target="_blank">https://tokbox.com/developer/samples/</a>

1.7.4 視訊聊天嵌入

以最少代碼将OpenTok功能內建到你的網站的最快方式。

<a href="https://tokbox.com/developer/embeds/" target="_blank">https://tokbox.com/developer/embeds/</a>

1.7.5 開發人員指南

有關所有OpenTok特點和功能的全面文檔,可參考關于Web,IOS和Android的用戶端SDK的特定類,方法和事件資訊。

<a href="https://tokbox.com/developer/guides/" target="_blank">https://tokbox.com/developer/guides/</a>

1.7.6 REST API參考文檔

使用OpenTok REST API和伺服器SDK指南

<a href="https://tokbox.com/developer/rest/" target="_blank">https://tokbox.com/developer/rest/</a>

1.7.7 開發人員工具

調試會話的有用工具和測試API調用等。

<a href="https://tokbox.com/developer/tools/" target="_blank">https://tokbox.com/developer/tools/</a>

1.7.8 測試版程式

一個列出所有OpenTok公開Beta以及如何加入他們的頁面。

<a href="https://tokbox.com/developer/beta/" target="_blank">https://tokbox.com/developer/beta/</a>

1.7.9 支援中心

如遇到問題找不到答案,請通路支援中心。

<a href="https://support.tokbox.com/" target="_blank">https://support.tokbox.com/</a>

2 實踐部分

2.1 部署環境

2.1.1 系統部署

1)最小化安裝CentOS 7.3 x86_64,系統資訊如下

OS = CentOS 7.3 x86_64

IP Address = 10.168.0.55

HostName = openTok.cmdschool.org

2)名稱解析服務

Windows用戶端運作裡面輸入如下指令

1

<code>notepad \Windows\System32\drivers\etc\hosts</code>

并增加如下記錄

10.168.0.55 www.cmdschool.org

2.1.2 安裝nginx

2

<code>yum </code><code>install</code> <code>-y http:</code><code>//nginx</code><code>.org</code><code>/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0</code><code>.el7.ngx.noarch.rpm</code>

<code>yum </code><code>install</code> <code>-y nginx</code>

2.1.3 建立項目目錄

<code>mkdir</code> <code>-p </code><code>/usr/share/nginx/html/opentok</code>

2.1.4 确認配置

<code>cp</code> <code>/etc/nginx/conf</code><code>.d</code><code>/default</code><code>.conf </code><code>/etc/nginx/conf</code><code>.d</code><code>/ssl_default</code><code>.conf</code>

<code>vim </code><code>/etc/nginx/conf</code><code>.d</code><code>/ssl_default</code><code>.conf</code>

确認存在如下内容:

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<code>server {</code>

<code>    </code><code>listen       443;</code>

<code>    </code><code>server_name  localhost;</code>

<code>    </code><code>ssl on;</code>

<code>    </code><code>ssl_certificate 1_www.cmdschool.org_bundle.crt;</code>

<code>    </code><code>ssl_certificate_key 2_www.cmdschool.org.key;</code>

<code>    </code><code>ssl_session_timeout 5m;</code>

<code>    </code><code>ssl_protocols TLSv1 TLSv1.1 TLSv1.2;</code>

<code>    </code><code>ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;</code>

<code>    </code><code>ssl_prefer_server_ciphers on;</code>

<code>    </code><code>location / {</code>

<code>        </code><code>root   </code><code>/usr/share/nginx/html</code><code>;</code>

<code>        </code><code>index  index.html index.htm;</code>

<code>    </code><code>}</code>

<code>    </code><code>error_page   500 502 503 504  </code><code>/50x</code><code>.html;</code>

<code>    </code><code>location = </code><code>/50x</code><code>.html {</code>

<code>}</code>

2.1.5 配置nginx服務

<code>systemctl </code><code>enable</code> <code>nginx</code>

<code>systemctl restart nginx</code>

2.1.6 配置防火牆

<code>firewall-cmd --permanent --add-service https</code>

<code>firewall-cmd --reload</code>

<code>firewall-cmd --list-all</code>

2.1.7 nginx內建PHP fastCGI

1)安裝基礎軟體包

<code>yum </code><code>install</code> <code>-y php-fpm php</code>

2)啟動并配置服務自啟動

<code>systemctl </code><code>enable</code> <code>php-fpm.service</code>

<code>systemctl start php-fpm.service</code>

3)配置nginx內建php fastCGI

增加如下代碼

<code>    </code><code># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</code>

<code>    </code><code>#</code>

<code>    </code><code>location ~ \.php$ {</code>

<code>        </code><code>root           </code><code>/usr/share/nginx/html</code><code>;</code>

<code>        </code><code>fastcgi_pass   127.0.0.1:9000;</code>

<code>        </code><code>fastcgi_index  index.php;</code>

<code>        </code><code>fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;</code>

<code>        </code><code>include        </code><code>/etc/nginx/fastcgi_params</code><code>;</code>

重新開機nginx服務

4)測試php fastCGI

添加調試代碼

<code>echo</code> <code>'&lt;?php phpinfo(); ?&gt;'</code> <code>&gt; </code><code>/usr/share/nginx/html/index</code><code>.php</code>

通路以下連結測試fastCGI

<a href="https://www.cmdschool.org/index.php" target="_blank">https://www.cmdschool.org/index.php</a>

<a href="https://s2.51cto.com/wyfs02/M01/9A/6B/wKioL1lVr6LwKcIUAABhGXFAmok269.png" target="_blank"></a>

5)如下日志可以協助你排錯

<code>tail</code> <code>-f </code><code>/var/log/nginx/error</code><code>.log</code>

<code>tail</code> <code>-f </code><code>/var/log/php-fpm/error</code><code>.log</code>

2.1.8 安裝輔助工具包

<code>yum </code><code>install</code> <code>-y unzip</code>

2.2 了解Web用戶端會話

2.2.1 配置Index頁引用OpenTok.js庫

<code>vim </code><code>/usr/share/nginx/html/opentok/index</code><code>.html</code>

輸入如下内容:

<code>&lt;html&gt;</code>

<code>&lt;body&gt;</code>

<code>&lt;!-- OpenTok.js library --&gt;</code>

<code>&lt;script src=</code><code>"https://static.opentok.com/v2/js/opentok.js"</code><code>&gt;&lt;</code><code>/script</code><code>&gt;</code>

<code>&lt;script&gt;</code>

<code>//</code> <code>credentials</code>

<code>//</code> <code>connect to session</code>

<code>  </code> 

<code>//</code> <code>create subscriber</code>

<code>&lt;</code><code>/script</code><code>&gt;</code>

<code>&lt;</code><code>/body</code><code>&gt;</code>

<code>&lt;</code><code>/html</code><code>&gt;</code>

2.2.2 模拟建立伺服器憑據

<code>vim </code><code>/var/www/html/opentok/index</code><code>.html</code>

内容加入到注釋“credentials”下:

<code>var apiKey = </code><code>'45828062'</code><code>;</code>

<code>var sessionId = </code><code>'2_MX40NTgyODA2Mn5-MTQ5NzgzNTA0MjA1NX54WnZGd2VKc3JWTkF3aW04ZlBjTXpKTFV-UH4'</code><code>;</code>

<code>var token = </code><code>'T1==cGFydG5lcl9pZD00NTgyODA2MiZzaWc9YjVlNjc2ZjJlOTgxOTY1YTJiNGQzMzc1NDZlZmRhYmViZDU1NTNlZjpzZXNzaW9uX2lkPTJfTVg0ME5UZ3lPREEyTW41LU1UUTVOemd6TlRBME1qQTFOWDU0V25aR2QyVktjM0pXVGtGM2FXMDRabEJqVFhwS1RGVi1VSDQmY3JlYXRlX3RpbWU9MTQ5NzgzNTEwMSZub25jZT0wLjA0NDA3NzI3NzgxOTc2MjY3JnJvbGU9cHVibGlzaGVyJmV4cGlyZV90aW1lPTE0OTc5MjE1MDE='</code><code>;</code>

注:必須由伺服器建立有效的API秘鑰和會話以及令牌

2.2.3 模拟初始化會話

内容加入到注釋“create subscriber”下:

<code>var session = OT.initSession(apiKey, sessionId)</code>

<code>session.connect(token, </code><code>function</code><code>(error) {</code>

<code> </code><code>//</code> <code>create publisher</code>

<code> </code> 

<code>})</code>

2.2.4 模拟釋出視訊流

内容加入到注釋“create publisher”下:

<code>var publisher = OT.initPublisher();</code>

<code>   </code><code>session.publish(publisher);</code>

代碼允許你将網絡攝像頭和麥克風的音頻和視訊流釋出到會話

2.2.5 建立訂閱

<code>session.on(</code><code>'streamCreated'</code><code>, </code><code>function</code><code>(event) {</code>

<code> </code><code>session.subscribe(event.stream);</code>

<code>});</code>

代碼允許你在會話中訂閱其他用戶端的視訊流

2.3 配置基本的用戶端

2.3.1 建立項目目錄結構

<code>mkdir</code> <code>-p </code><code>/usr/share/nginx/html/opentok/</code><code>{js,css}</code>

2.3.2 建立用戶端頁面

加入如下内容:

<code>&lt;</code><code>head</code><code>&gt;</code>

<code>    </code><code>&lt;title&gt; OpenTok Getting Started &lt;</code><code>/title</code><code>&gt;</code>

<code>    </code><code>&lt;link href=</code><code>"css/app.css"</code> <code>rel=</code><code>"stylesheet"</code> <code>type</code><code>=</code><code>"text/css"</code><code>&gt;</code>

<code>    </code><code>&lt;script src=</code><code>"https://static.opentok.com/v2/js/opentok.min.js"</code><code>&gt;&lt;</code><code>/script</code><code>&gt;</code>

<code>&lt;</code><code>/head</code><code>&gt;</code>

<code>    </code><code>&lt;div </code><code>id</code><code>=</code><code>"videos"</code><code>&gt;</code>

<code>        </code><code>&lt;div </code><code>id</code><code>=</code><code>"subscriber"</code><code>&gt;&lt;</code><code>/div</code><code>&gt;</code>

<code>        </code><code>&lt;div </code><code>id</code><code>=</code><code>"publisher"</code><code>&gt;&lt;</code><code>/div</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>/div</code><code>&gt;</code>

<code>    </code><code>&lt;script </code><code>type</code><code>=</code><code>"text/javascript"</code> <code>src=</code><code>"js/app.js"</code><code>&gt;&lt;</code><code>/script</code><code>&gt;</code>

代碼作用如下:

- 引用openTok.js庫以及自己建立的JS和CSS檔案

- 包含釋出者與訂閱者的DIV

- 将包含視訊流

2.3.3 配置認證

<code>vim </code><code>/usr/share/nginx/html/opentok/js/app</code><code>.js</code>

<code>//</code> <code>replace these values with those generated </code><code>in</code> <code>your TokBox Account</code>

<code>var apiKey = </code><code>"YOUR_API_KEY"</code><code>;</code>

<code>var sessionId = </code><code>"YOUR_SESSION_ID"</code><code>;</code>

<code>var token = </code><code>"YOUR_TOKEN"</code><code>;</code>

<code>//</code> <code>(optional) add server code here</code>

<code>initializeSession();</code>

注:将2.2的apiKey/sessinoId/token值替換變量值。

2.3.4 連接配接會話并建立釋出者

在目前代碼後面加入如下内容:

22

23

24

25

26

27

28

29

<code>//</code> <code>Handling all of our errors here by alerting them</code>

<code>function</code> <code>handleError(error) {</code>

<code>  </code><code>if</code> <code>(error) {</code>

<code>    </code><code>alert(error.message);</code>

<code>  </code><code>}</code>

<code>function</code> <code>initializeSession() {</code>

<code>  </code><code>var session = OT.initSession(apiKey, sessionId);</code>

<code>  </code><code>//</code> <code>Subscribe to a newly created stream</code>

<code>  </code><code>//</code> <code>Create a publisher</code>

<code>  </code><code>var publisher = OT.initPublisher(</code><code>'publisher'</code><code>, {</code>

<code>    </code><code>insertMode: </code><code>'append'</code><code>,</code>

<code>    </code><code>width: </code><code>'100%'</code><code>,</code>

<code>    </code><code>height: </code><code>'100%'</code>

<code>  </code><code>}, handleError);</code>

<code>  </code><code>//</code> <code>Connect to the session</code>

<code>  </code><code>session.connect(token, </code><code>function</code><code>(error) {</code>

<code>    </code><code>//</code> <code>If the connection is successful, publish to the session</code>

<code>    </code><code>if</code> <code>(error) {</code>

<code>      </code><code>handleError(error);</code>

<code>    </code><code>} </code><code>else</code> <code>{</code>

<code>      </code><code>session.publish(publisher, handleError);</code>

<code>  </code><code>});</code>

代碼完成以下功能:

- initializeSession函數使用OT.initPubulisher方法建立一個publisher對象,該方法有三個參數, -- publisher視訊代替文檔對象模型的元素(指ID為publisher的DIV)

 -- publisher屬性(insertMode/height/width屬性)

 -- 指定完成錯誤處理

- initalizeSession方法建立一個session對象,該方法需要兩個參數,

 -- apiKey,

 -- sessionId

- session.connec方法将用戶端程式連接配接到openTok會話

- 如果連接配接openTok會話有錯誤,則将錯誤對象傳遞給connect事件處理程式(使用錯誤消息向控制台發送console.error)

2.3.5 初始化使用者

在注釋後面加入如下内容:

<code>//</code> <code>Subscribe to a newly created stream</code>

<code>  </code><code>session.subscribe(event.stream, </code><code>'subscriber'</code><code>, {</code>

- 在會話中建立新流,會話對象排程streamCreated事件。

- 用戶端檢測到流,我們使用session.subscribe()方法訂閱流,該方法需要四個參數,

 -- 用戶端訂閱的Stream對象(event.stream)

-- 訂閱視訊代替文檔對象模型元素或ID(指ID為subscriber的DIV)

-- 訂閱視圖的外觀屬性(指inserMode,height,width屬性)

--  錯誤處理函數(當subscribe方法傳回成功或失敗時調用)

2.3.6 初始化使用者

<code>vim </code><code>/usr/share/nginx/html/opentok/css/app</code><code>.css</code>

30

31

32

<code>body, html {</code>

<code>    </code><code>background-color: gray;</code>

<code>    </code><code>height: 100%;</code>

<code>#videos {</code>

<code>    </code><code>position: relative;</code>

<code>    </code><code>width: 100%;</code>

<code>    </code><code>margin-left: auto;</code>

<code>    </code><code>margin-right: auto;</code>

<code>#subscriber {</code>

<code>    </code><code>position: absolute;</code>

<code>    </code><code>left: 0;</code>

<code>    </code><code>top</code><code>: 0;</code>

<code>    </code><code>z-index: 10;</code>

<code>#publisher {</code>

<code>    </code><code>width: 360px;</code>

<code>    </code><code>height: 240px;</code>

<code>    </code><code>bottom: 10px;</code>

<code>    </code><code>left: 10px;</code>

<code>    </code><code>z-index: 100;</code>

<code>    </code><code>border: 3px solid white;</code>

<code>    </code><code>border-radius: 3px;</code>

2.4 伺服器的部署安裝方式概述

2.4.1 選項一Heroku上啟動簡單的REST伺服器

需要簡單部署請參閱以下連結:

<a href="https://heroku.com/deploy?template=https://github.com/opentok/learning-opentok-php" target="_blank">https://heroku.com/deploy?template=https://github.com/opentok/learning-opentok-php</a>

2.4.2 選項二使用伺服器SDK建構(高定制)

需要進階定制請參閱:

<a href="https://tokbox.com/developer/sdks/server/" target="_blank">https://tokbox.com/developer/sdks/server/</a>

開發者指南請參閱:

SDK下載下傳位址:

<a href="https://github.com/opentok/opentok-php-sdk/releases" target="_blank">https://github.com/opentok/opentok-php-sdk/releases</a>

PHP語言參考:

<a href="http://www.w3school.com.cn/php/php_syntax.asp" target="_blank">http://www.w3school.com.cn/php/php_syntax.asp</a>

2.4.3 選項3伺服器執行個體應用程式

需要執行個體程式部署請參閱:

2.5 使用SDK建構伺服器

2.5.1 安裝composer

1)配置主程式

<code>php -r </code><code>"copy('https://getcomposer.org/installer', 'composer-setup.php');"</code>

<code>php -r </code><code>"if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"</code>

<code>php composer-setup.php</code>

<code>php -r </code><code>"unlink('composer-setup.php');"</code>

<code>mv</code> <code>composer.phar </code><code>/usr/local/bin/composer</code>

2)設定國内源

<code>composer config repo.packagist composer https:</code><code>//packagist</code><code>.phpcomposer.com</code>

注:其他使用方式請參閱,

https://pkg.phpcomposer.com/

2.5.2 部署基礎包

<code>cd</code> <code>/usr/share/nginx/html</code>

<code>mkdir</code> <code>backup</code>

<code>mv</code> <code>opentok backup</code>

<code>composer require opentok</code><code>/opentok</code> <code>2.3.1</code>

<code>composer require slim</code><code>/slim</code> <code>2.*</code>

<code>composer require gregwar</code><code>/cache</code> <code>1.0.*</code>

 配置基礎包的權限

<code>chown</code> <code>-R nginx:apache </code><code>/usr/share/nginx/html</code>

<code>chmod</code> <code>-R 770 </code><code>/usr/share/nginx/html</code>

2.5.3 配置php-fpm的啟動腳本環境變量

<code>cat</code> <code>/usr/lib/systemd/system/php-fpm</code><code>.service | </code><code>grep</code> <code>EnvironmentFile</code>

确認包含如下參數:

<code>EnvironmentFile=</code><code>/etc/sysconfig/php-fpm</code>

2.5.4 定義環境變量的參數

<code>vim </code><code>/etc/sysconfig/php-fpm</code>

輸入如下配置:

<code>API_KEY = 0000000</code>

<code>API_SECRET = abcdef1234567890abcdef01234567890abcdef</code>

注:以上隻是範例,API_KEY和API_SECRET需要到雲端新增賬號申請。

https://tokbox.com/account

2.5.5 systemd的環境變量通過腳本傳給bash

1)配置變量傳遞腳本

<code>vim </code><code>/etc/profile</code><code>.d</code><code>/opentok</code><code>.sh</code>

<code>#!/bin/bash</code>

<code>if</code> <code>[ -f </code><code>/etc/sysconfig/php-fpm</code> <code>]; </code><code>then</code>

<code>        </code><code>oldifs=$IFS</code>

<code>        </code><code>IFS=$</code><code>'\n'</code>

<code>        </code><code>for</code> <code>i </code><code>in</code> <code>`</code><code>cat</code> <code>/etc/sysconfig/php-fpm</code> <code>| </code><code>egrep</code> <code>-</code><code>v</code> <code>"^#|^$|^;"</code> <code>| </code><code>sed</code> <code>'s/ //g'</code><code>`; </code><code>do</code>

<code>                </code><code>export</code> <code>"$i"</code>

<code>        </code><code>done</code>

<code>        </code><code>IFS=$oldifs</code>

<code>fi</code>

2)導入bash的環境變量:

<code>source</code> <code>/etc/profile</code>

3)确認環境變量生效:

<code>echo</code> <code>$API_KEY;</code><code>echo</code> <code>$API_SECRET</code>

顯示如下:

<code>0000000</code>

<code>abcdef1234567890abcdef01234567890abcdef</code>

2.5.6 将systemd環境變量傳給php-fpm

1)配置變量傳遞

<code>vim </code><code>/etc/php-fpm</code><code>.d</code><code>/www</code><code>.conf</code>

注釋後加入兩行傳參變量定義:

<code>; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from</code>

<code>; the current environment.</code>

<code>; Default Value: clean </code><code>env</code>

<code>;</code><code>env</code><code>[HOSTNAME] = $HOSTNAME</code>

<code>;</code><code>env</code><code>[PATH] = </code><code>/usr/local/bin</code><code>:</code><code>/usr/bin</code><code>:</code><code>/bin</code>

<code>;</code><code>env</code><code>[TMP] = </code><code>/tmp</code>

<code>;</code><code>env</code><code>[TMPDIR] = </code><code>/tmp</code>

<code>;</code><code>env</code><code>[TEMP] = </code><code>/tmp</code>

<code>env</code><code>[API_KEY] = $API_KEY</code>

<code>env</code><code>[API_SECRET] = $API_SECRET</code>

2)通過PHPINFO函數檢查傳參

<a href="https://s5.51cto.com/wyfs02/M02/9A/6B/wKioL1lVr_TQ73G8AAAdkEdPAuY634.png" target="_blank"></a>

注:配置方法詳見“2.1.7 nginx內建PHP fastCGI”

2.5.7 修改PHP.ini參數

注:以下全部修改配置檔案“/etc/php.ini”,以下配置都需重新開機“php-fpm”服務

1)顯示調試頁面

<code>display_errors = stderr</code>

2)屏蔽時區錯誤

<code>date</code><code>.timezone = </code><code>"Asia/Shanghai"</code>

2.5.8 代碼調試

測試以下連結的代碼:

<a href="https://www.cmdschool.org/vendor/opentok/opentok/sample/HelloWorld/web/index.php" target="_blank">https://www.cmdschool.org/vendor/opentok/opentok/sample/HelloWorld/web/index.php</a>

如果有錯誤請根據提示修改:

<code>vim vendor</code><code>/opentok/opentok/sample/HelloWorld/web/index</code><code>.php</code>

2.6 部署項目Demo

2.6.1 下載下傳Demo

如果你不想調試,請使用筆者已經調試過的demo,下載下傳位址如下

<a href="http://down.51cto.com/data/2323312" target="_blank">http://down.51cto.com/data/2323312</a>

2.6.2 将代碼下載下傳到如下目錄

<code>/usr/share/nginx/html</code>

2.6.3 解壓

<code>unzip opentok.zip</code>

2.6.4 修改權限

<code>chown</code> <code>-R nginx:apache </code><code>/usr/share/nginx/html/opentok</code>

<code>chmod</code> <code>-R 770 </code><code>/usr/share/nginx/html/opentok</code>

2.6.5 準備調試環境

電腦中需插入如下裝置:

- 攝像頭(可選)

- 麥克風(必須)

- 耳麥或音箱(可選)

2.6.6 測試運作

使用兩個以上的頁面打開如下位址:

<a href="https://www.cmdschool.org/opentok/index.php" target="_blank">https://www.cmdschool.org/opentok/index.php</a>

可見如下視窗:

<a href="https://s5.51cto.com/wyfs02/M00/9A/6C/wKioL1lVtYjxC1y4AADIgYUOv58978.png" target="_blank"></a>

本文轉自 tanzhenchao 51CTO部落格,原文連結:http://blog.51cto.com/cmdschool/1943341,如需轉載請自行聯系原作者