- 第一步,去 https://tokbox.com
注册账号 ,然后新建一个project , 这样就得到了 api key , 和 secret , 如图
- 第二步,建server , 以 python 为例 :
sample 程序在 https://github.com/opentok/Opentok-Python-SDK
先建立一个 python 虚拟目录, 这样得到干净的初始环境 。
然后, 安装 flask 和 opentok 库:
1 |
pip install flask |
1 |
pip install opentok |
安装完成后把例子程序的helloworld项目的 helloworld.py, 根据自己的情况修改为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
from flask import Flask, render_template from opentok import OpenTok import os import logging logging.basicConfig(filename='app.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s') try: api_key = '46419702' api_secret = 'b8f17a054ccde74e68453464ad6b1c879a75d030' except Exception: raise Exception('You must define API_KEY and API_SECRET environment variables') app = Flask(__name__) opentok = OpenTok(api_key, api_secret) session = opentok.create_session() #print(session.session_id) @app.route("/") def hello(): key = api_key session_id = session.session_id token = opentok.generate_token(session_id) logging.info('session_is is %s', session_id) logging.info('token is %s', token) return render_template('index.html', api_key=key, session_id=session_id, token=token) if __name__ == "__main__": app.debug = True app.run() |
然后在虚拟环境内执行 ,本机的虚拟环境目录在: F:\Programs\Python\py37virtualenvs\tokbox
如果能出现下图就说明服务启动成功了
有一个常见的错误就是: 当试图 create session时 , 出现
tokbox Failed to create session, invalid credentials 的错误, 这可能是因为测试机器时间和标准时间不一致 ,解决方法是: setting–>datetime setting –>set time automatically on
成功启动服务后, http://127.0.0.1:5000
如果有摄像头, 就能看到图像。
服务启动后会在页面显示 sessionid 和 生成的 token
- 客户端 , web client 例子程序在 https://github.com/opentok/opentok-web-samples
首先找出刚才建立的server 的 api_key , session_id 和 token :
46419702
1_MX40NjQxOTcwMn5-MTU2ODI3MTIwOTMzNH5QcEY4aE0ra0owaC9DTTAzdnRoRTlxK2J-UH4
T1==cGFydG5lcl9pZD00NjQxOTcwMiZzaWc9ZDJmZmRmMGU1MmE4YjBkMmQyOTEzMjgzNDBmZjZjYzMwN2EwY2U0ZTpzZXNzaW9uX2lkPTFfTVg0ME5qUXhPVGN3TW41LU1UVTJPREkzTVRJd09UTXpOSDVRY0VZNGFFMHJhMG93YUM5RFRUQXpkblJvUlRseEsySi1VSDQmY3JlYXRlX3RpbWU9MTU2ODI3MjU4OCZleHBpcmVfdGltZT0xNTY4MzU4OTg4JnJvbGU9cHVibGlzaGVyJm5vbmNlPTIwNTQyNiZpbml0aWFsX2xheW91dF9jbGFzc19saXN0PQ==
打开例子程序的 Basic Video Chat 子项目
修改 /js/config.js , 把 api_key , session_id 和 token 等都修改为和之前服务器建立的一致 ,并且把 server_url 的那句注释掉:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* eslint-disable no-unused-vars */ // Make a copy of this file and save it as config.js (in the js directory). // Set this to the base URL of your sample server, such as 'https://your-app-name.herokuapp.com'. // Do not include the trailing slash. See the README for more information: //var SAMPLE_SERVER_BASE_URL = 'http://YOUR-SERVER-URL'; // OR, if you have not set up a web server that runs the learning-opentok-php code, // set these values to OpenTok API key, a valid session ID, and a token for the session. // For test purposes, you can obtain these from https://tokbox.com/account. var API_KEY = '46419702'; var SESSION_ID = '1_MX40NjQxOTcwMn5-MTU2ODI3MTIwOTMzNH5QcEY4aE0ra0owaC9DTTAzdnRoRTlxK2J-UH4'; var TOKEN = 'T1==cGFydG5lcl9pZD00NjQxOTcwMiZzaWc9ZDJmZmRmMGU1MmE4YjBkMmQyOTEzMjgzNDBmZjZjYzMwN2EwY2U0ZTpzZXNzaW9uX2lkPTFfTVg0ME5qUXhPVGN3TW41LU1UVTJPREkzTVRJd09UTXpOSDVRY0VZNGFFMHJhMG93YUM5RFRUQXpkblJvUlRseEsySi1VSDQmY3JlYXRlX3RpbWU9MTU2ODI3MjU4OCZleHBpcmVfdGltZT0xNTY4MzU4OTg4JnJvbGU9cHVibGlzaGVyJm5vbmNlPTIwNTQyNiZpbml0aWFsX2xheW91dF9jbGFzc19saXN0PQ=='; |
然后打开 Basic Video Chat 的 index.html 程序, 就可以与服务器互通视频和语音。