天天看点

跨平台视频通信项目-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,如需转载请自行联系原作者