Flash/red5/helloworld
提供: 初心者エンジニアの簡易メモ
コンパイルに使用するライブラリのダウンロード
red5-1.0.0の以下ファイルを作業PCにダウンロード
red5.jar lib/*
当サイトではD:/flex/Red5-1.0.0
eclipseインストール
java/eclipse [ショートカット]
サーバサイド(Java
- ファイル/新規/JavaプロジェクトでRed5Sampleプロジェクトを作成
- WEB-INFというフォルダを作成して、その中にsrcフォルダとclassesフォルダを作成します。
- プロジェクト/プロパティ/javaのビルドパス/ソースをWEB-INF/srcに変更
- デフォルト出力フォルダにRed5Sample/WEB-INF/classesに指定
- 外部クラス・フォルダーの追加より以下を追加
D:\flex\red5-1.0.0\lib D:\flex\red5-1.0.0\red5.jar
- WEB-INFに以下ファイルを作成
red5-web.properties red5-web.xml web.xml
WEB-INF/red5-web.properties
webapp.contextPath=/Red5Sample webapp.virtualHosts=*, localhost, localhost:8088, 127.0.0.1:8088
WEB-INF/red5-web.xmlの最下に追加
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd"> <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="/WEB-INF/red5-web.properties" /> </bean> <bean id="web.context" class="org.red5.server.Context" autowire="byType" /> <bean id="web.scope" class="org.red5.server.WebScope" init-method="register"> <property name="server" ref="red5.server" /> <property name="parent" ref="global.scope" /> <property name="context" ref="web.context" /> <property name="handler" ref="web.handler" /> <property name="contextPath" value="${webapp.contextPath}" /> <property name="virtualHosts" value="${webapp.virtualHosts}" /> </bean> <!-- upd --> <bean id="web.handler" class="jp.example.Red5Sample.Application" /> <!-- /upd --> </beans>
WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>Red5Sample</display-name> <!-- upd --> <context-param> <param-name>webAppRootKey</param-name> <param-value>/Red5Sample</param-value> </context-param> <!-- /upd --> </web-app>
src/jp/example/Red5Sample/Application.java
package jp.example.Red5Sample;
import org.red5.server.adapter.ApplicationAdapter;
public class Application extends ApplicationAdapter {
public String getMemberCount(String name) {
return "member count=" + getClients().size() + " name=" + name;
}
}
クライアントサイド(as3
MainConnection.as
package
{
import flash.display.Sprite;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.Responder;
public class MainConnect extends Sprite
{
private var nc:NetConnection;
public function MainConnect()
{
// 接続
nc=new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
nc.connect("rtmp://localhost/Red5Sample");
}
private function onStatus(evt:NetStatusEvent):void
{
for (var i:String in evt.info) {
trace(i + "=" + evt.info[i]);
}
switch(evt.info.code)
{
// 接続成功
case "NetConnection.Connect.Success":
nc.call("getMemberCount", new Responder(_listener), "name1");
break;
case "NetConnection.Connect.Closed":
trace("Closed");
break;
case "NetConnection.Connect.Failed":
trace("Failed");
break;
case "NetConnection.Connect.Rejected":
trace("Rejected");
break;
default:
trace("Error");
trace(evt.info.code);
}
}
// レスポンスメソッド
private function _listener(str:String):void
{
trace(str); // member count=1 name=name1
}
}
}
エラー対応
- FlashMediaServerやFlashCommunicationServerとの併用はできないので、起動してれば切る
- Application.javaを更新した後はRed5を再起動する
- javaは実行する必要はない、コンパイルのみ(.javaをeclipseで保存)でOK
- サーバサイドのプログラムが間違っているときはサンプルデモが動かないので、そこも確認する
参考
http://coelacanth.heteml.jp/site/flash_red5/article_4
