讓你的 wowza server提供 RESTful web 服務

來源:互聯網
上載者:User

標籤:err   version   monitor   ati   call   type   client   服務   項目   

        有時我們 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());@Overridepublic 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 hereString 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/

讓你的 wowza server提供 RESTful web 服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.