您的位置:首页 > 其它

让你的 wowza 服务器提供 RESTful web 服务

2014-06-13 15:28 453 查看
有时我们 nginx 需要和 wowza 服务器交互以进行一些 LB 事宜;有时我们的管理员需要实时了解 wowza 服务器的一些其他状态信息(比如一些自定义对象的状态等等),而用 rtmp 不是太方便。这些情况下,如果我们的 wowza 服务器能提供一个 http api 就好了,就像 web 容器 tomcat 那样。本文以一个简单的 http 发送参数到 wowza,然后 wowza 返回一个 json 为例(经典的 REST 案例),介绍如何让你的 wowza 服务器提供 RESTful web 调用接口。
本文是在《取代 Ant:使用 Maven 管理 Wowza 插件开发》例子的基础上进一步进行研发,没有使用 wowza 官方的 Eclipse 插件(官方对 wowza 项目的管理使用的是 ant)。
1. 新建 maven 项目
参考《取代 Ant:使用 Maven 管理 Wowza 插件开发》步骤。新建的 maven 项目 defonds-server-module 如下:



新建项目的 debug 调试效果:



2. 编写 HTTProvider2Base 子类
package com.defonds.wms.module.server;

import java.io.IOException;
import java.io.OutputStream;

import com.wowza.wms.http.HTTProvider2Base;
import com.wowza.wms.http.IHTTPRequest;
import com.wowza.wms.http.IHTTPResponse;
import com.wowza.wms.logging.WMSLogger;
import com.wowza.wms.logging.WMSLoggerFactory;
import com.wowza.wms.vhost.IVHost;

public class ServerMonitorHttpInterface extends HTTProvider2Base {
	private static final WMSLogger logger = WMSLoggerFactory.getInstance().getLoggerObj(ServerMonitorHttpInterface.class.getName());

	@Override
	public void onHTTPRequest(IVHost ivHost, IHTTPRequest request, IHTTPResponse response) {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		logger.debug("ServerMonitorHttpInterface--http--request--id=" + id + ";name=" + name);
		
		// TODO do your own business logic here
		
		String jsonObject = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
		response.setHeader("Content-Type", "application/json");
		// Get the printwriter object from response to write the required json object to the output stream      
		OutputStream out = response.getOutputStream();
		try {
			out.write(jsonObject.getBytes());
			out.flush();
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}

}
3. 编辑 VHost.xml
编辑 %wowza%/conf/VHost.xml,添加一个 HTTPProvider:
<HTTPProvider>
    <BaseClass>com.defonds.wms.module.server.ServerMonitorHttpInterface</BaseClass>
    <RequestFilters>monitor*</RequestFilters>
    <AuthenticationMethod>none</AuthenticationMethod>
</HTTPProvider>

4. 项目重新打包部署
命令行切换到你的 defonds-server-module 项目目录下,执行
mvn package
检查 %wowza%/lib 目录,发现 defonds-server-module 已成功部署:



5. 访问接口
debug 启动 defonds-server-module,然后在浏览器访问 http://localhost:1935/monitor?id=9527&name=defonds 发现返回的是
Wowza Streaming Engine 4 Trial Edition



Eclipse 控制台也没有 ServerMonitorHttpInterface 本应该输出的 log 日志。
这是因为 com.wowza.wms.http.HTTPServerVersion 这个 HTTPProvider 把请求拦截了:
<HTTPProvider>
						<BaseClass>com.wowza.wms.http.HTTPServerVersion</BaseClass>
						<RequestFilters>*</RequestFilters>
						<AuthenticationMethod>none</AuthenticationMethod>
					</HTTPProvider>

因为它的配置是 *。可以改为其他,或者将其注释掉。就可以了。
注释掉 HTTPServerVersion 之后,重新启动 defonds-server-module,然后访问 http://localhost:1935/monitor?id=9527&name=defonds: 这次是返回的我们想要的信息:



Eclipse 控制台有 log 输出了:
DEBUG server comment - ServerMonitorHttpInterface--http--request--id=9527;name=defonds
成功了。嗯,就这些,就是这么简单:)本文示例源码已上传 CSDN,有兴趣的朋友可以去这里下载:http://download.csdn.net/detail/defonds/7493981
参考资料
http://stackoverflow.com/questions/10410854/call-a-wowza-server-from-android-app
http://stackoverflow.com/questions/2010990/how-do-you-return-a-json-object-from-a-java-servlet
http://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: