-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotEngine.class.php
More file actions
executable file
·74 lines (65 loc) · 2.11 KB
/
DotEngine.class.php
File metadata and controls
executable file
·74 lines (65 loc) · 2.11 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Library;
use Firebase\JWT\JWT;
class DotEngine{
private $appkey = "";
private $appsecret = "";
private $connectTimeout = 10; //单位s
/**
* 架构方法 设置参数
* @param $appkey
* @param $appsecret
*/
public function __construct($appkey, $appsecret){
$this->appsecret = $appsecret;
$this->appkey = $appkey;
}
/**
* default 10s
* @param connectTimeout connect timeout
*/
public function setConnectTimeout($connectTimeout){
$this->connectTimeout = $connectTimeout;
}
/**依赖:https://github.com/firebase/php-jwt/blob/master/src/JWT.php
* @param $room 房间号
* @param $user 用户名
* @param $expires 房间有效时间 默认36000*24
* @return string token
*/
public function createToken($room, $user, $expires){
vendor('JWT.JWT');
$params['room'] = $room;
$params['user'] = $user;
$params['appkey'] = $this->appkey;
$params['expires'] = $expires;
$params['nonce'] = rand(0,9999999);
$sign = JWT::encode($params, $this->appsecret);
$post_data = array(
'appkey'=>$this->appkey,
'sign'=>$sign
);
$url = 'https://janus.dot.cc/api/createToken';
return $this->send_post($url, $post_data);
}
/**
* POST方法获取token
* @param $url https://janus.dot.cc/api/createToken
* @param $post_data array('appkey'=>$appkey,'sign'=>$sign)
* @return bool|string token
*/
private function send_post($url, $post_data){
$post_data = http_build_query($post_data);
$options = array(
'http'=>array(
'method'=>'POST',
'header'=>'Content-type:application/x-www-form-urlencoded',
'content'=>$post_data,
'timeout'=>$this->connectTimeout,
)
);
$context = stream_context_create($options);
$token = file_get_contents($url, false, $context);
return $token;
}
}