正在显示
1 个修改的文件
包含
37 行增加
和
16 行删除
| @@ -28,13 +28,13 @@ when srs get some event, for example, when client connect | @@ -28,13 +28,13 @@ when srs get some event, for example, when client connect | ||
| 28 | to srs, srs can invoke the http api of the api-server | 28 | to srs, srs can invoke the http api of the api-server |
| 29 | """ | 29 | """ |
| 30 | 30 | ||
| 31 | -import sys; | 31 | +import sys |
| 32 | # reload sys model to enable the getdefaultencoding method. | 32 | # reload sys model to enable the getdefaultencoding method. |
| 33 | -reload(sys); | 33 | +reload(sys) |
| 34 | # set the default encoding to utf-8 | 34 | # set the default encoding to utf-8 |
| 35 | # using exec to set the encoding, to avoid error in IDE. | 35 | # using exec to set the encoding, to avoid error in IDE. |
| 36 | -exec("sys.setdefaultencoding('utf-8')"); | ||
| 37 | -assert sys.getdefaultencoding().lower() == "utf-8"; | 36 | +exec("sys.setdefaultencoding('utf-8')") |
| 37 | +assert sys.getdefaultencoding().lower() == "utf-8" | ||
| 38 | 38 | ||
| 39 | import json, datetime, cherrypy | 39 | import json, datetime, cherrypy |
| 40 | 40 | ||
| @@ -66,25 +66,46 @@ class RESTClients(object): | @@ -66,25 +66,46 @@ class RESTClients(object): | ||
| 66 | exposed = True | 66 | exposed = True |
| 67 | 67 | ||
| 68 | def GET(self): | 68 | def GET(self): |
| 69 | - enable_crossdomain(); | ||
| 70 | - | ||
| 71 | - clients = {}; | ||
| 72 | - return json.dumps(clients); | 69 | + enable_crossdomain() |
| 73 | 70 | ||
| 74 | - # for SRS hook: on_connect | 71 | + clients = {} |
| 72 | + return json.dumps(clients) | ||
| 73 | + | ||
| 74 | + ''' | ||
| 75 | + for SRS hook: on_connect | ||
| 76 | + when client connect to vhost/app, call the hook, | ||
| 77 | + the request in the POST data string is a object encode by json: | ||
| 78 | + { | ||
| 79 | + "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live", | ||
| 80 | + "pageUrl": "http://www.test.com/live.html" | ||
| 81 | + } | ||
| 82 | + if valid, the hook must return HTTP code 200(Stauts OK) and response | ||
| 83 | + an int value specifies the error code(0 corresponding to success): | ||
| 84 | + 0 | ||
| 85 | + ''' | ||
| 75 | def POST(self): | 86 | def POST(self): |
| 76 | - enable_crossdomain(); | 87 | + enable_crossdomain() |
| 77 | 88 | ||
| 78 | - req = cherrypy.request.body.read(); | ||
| 79 | - trace("post to clients, req=%s"%(req)); | 89 | + # return the error code in str |
| 90 | + ret = Error.success | ||
| 91 | + | ||
| 92 | + req = cherrypy.request.body.read() | ||
| 93 | + trace("post to clients, req=%s"%(req)) | ||
| 80 | try: | 94 | try: |
| 81 | json_req = json.loads(req) | 95 | json_req = json.loads(req) |
| 82 | except Exception, ex: | 96 | except Exception, ex: |
| 83 | - trace("parse the request to json failed, req=%s, ex=%s"%(req, ex)) | ||
| 84 | - return str(Error.system_parse_json); | 97 | + ret = Error.system_parse_json |
| 98 | + trace("parse the request to json failed, req=%s, ex=%s, ret=%s"%(req, ex, ret)) | ||
| 99 | + return str(ret) | ||
| 100 | + | ||
| 101 | + trace("srs on_connect: client ip=%s, vhost=%s, app=%s, pageUrl=%s"%( | ||
| 102 | + json_req["ip"], json_req["vhost"], json_req["app"], json_req["pageUrl"] | ||
| 103 | + )) | ||
| 104 | + | ||
| 105 | + # TODO: valid the client. | ||
| 85 | 106 | ||
| 86 | trace("valid clients post request success.") | 107 | trace("valid clients post request success.") |
| 87 | - return str(Error.success); | 108 | + return str(ret) |
| 88 | 109 | ||
| 89 | def OPTIONS(self): | 110 | def OPTIONS(self): |
| 90 | enable_crossdomain() | 111 | enable_crossdomain() |
| @@ -108,7 +129,7 @@ main code start. | @@ -108,7 +129,7 @@ main code start. | ||
| 108 | ''' | 129 | ''' |
| 109 | # donot support use this module as library. | 130 | # donot support use this module as library. |
| 110 | if __name__ != "__main__": | 131 | if __name__ != "__main__": |
| 111 | - raise Exception("embed not support"); | 132 | + raise Exception("embed not support") |
| 112 | 133 | ||
| 113 | # check the user options | 134 | # check the user options |
| 114 | if len(sys.argv) <= 1: | 135 | if len(sys.argv) <= 1: |
-
请 注册 或 登录 后发表评论