red5的共享对象,使服务器和客户端的交互变的更简单和直接,服务器把处理完的业务逻辑放在共享对象(SharedObject)中,客户端只需要调用(SharedObject),就可以直接操作后台所返回的数据。所有的red5应用程序都继承自ApplicationAdapter类:
1
import java.util.HashMap;
2
import java.util.Map;
3
4
import org.red5.server.adapter.ApplicationAdapter;
5
import org.red5.server.api.IClient;
6
import org.red5.server.api.IConnection;
7
import org.red5.server.api.IScope;
8
import org.red5.server.api.so.ISharedObject;
9
10
11
/** *//**
12
* red5后台服务器与flash前台交互类
13
* @author zhanghh
14
*
15
*/
16
public class Application extends ApplicationAdapter
{
17
18
private Map<String, IConnection> onLineClient = new HashMap<String, IConnection>();
19
20
21
// 属性
22
private IScope appScope;
23
24
private String username;
25
26
private ISharedObject so;
27
28
private String sharedName;
29
30
/** *//**
31
* 客户端连接服务器的方法
32
*/
33
public String createSharedObjectPond(Object[] params)
{
34
//接收客户端传过来的参数值
35
sharedName = (String) params[0];
36
37
//创建共享对象
38
createSharedObject(appScope,sharedName,true);
39
so = getSharedObject(appScope, sharedName);
40
41
//引用共享对象的监听器
42
so.addSharedObjectListener(new SampleSharedObjectListener());
43
44
45
//将此三个属性值放入共享对象中
46
so.beginUpdate();
47
so.setAttribute("temperature", 20);
48
so.setAttribute("humidity", 50);
49
so.setAttribute("nutrition", 85);
50
so.endUpdate();
51
52
return sharedName;
53
}
54
55
/** *//**
56
* 此应用于开始运行时触发的方法
57
*/
58
public boolean appStart(IScope app)
{
59
System.out.println("red5系统已启动~!");
60
if (!super.appStart(app))
{
61
return false;
62
}
63
appScope = app;
64
return true;
65
66
}
67
68
/** *//**
69
* 连接服务器
70
*/
71
public boolean connect(IConnection conn, IScope scope, Object[] params)
{
72
username = "";
73
// 登入时将连接ID和连接信息形成对应关系并存入在线列表
74
String link_id = conn.getClient().getId();
75
System.out.println("连接--->link_id:"+link_id);
76
onLineClient.put(username, conn);
77
78
return true;
79
80
}
81
82
/** *//**
83
* 断开服务器
84
*/
85
public void disconnect(IConnection conn, IScope scope)
{
86
super.disconnect(conn, scope);
87
System.out.println("欢迎下次光临,再见!");
88
}
89
90
}
91
为了得到客户端共享对象的改变通报,监听器必须继承接口ISharedObjectListener:
1
import org.red5.server.api.so.ISharedObjectBase;
2
import org.red5.server.api.so.ISharedObjectListener;
3
4
5
/** *//**
6
* 共享对象的监听实现类
7
* @author zhanghh
8
*
9
*/
10
public class SampleSharedObjectListener implements ISharedObjectListener
{
11
12
/** *//**
13
* 监听客户端属性的变化值
14
*/
15
@Override
16
public void onSharedObjectUpdate(ISharedObjectBase so, String key, Object value)
{
17
//共享对象so的属性<key>
18
//被修改成<value>
19
//客户端有任何的属性值改变,都会调用onSharedObjectUpdate()方法
20
}
21
@Override
22
public void onSharedObjectConnect(ISharedObjectBase so)
{
23
//red5每次连接的时候调用此方法
24
}
25
@Override
26
public void onSharedObjectClear(ISharedObjectBase arg0)
{
27
// TODO Auto-generated method stub
28
}
29
@Override
30
public void onSharedObjectDelete(ISharedObjectBase arg0, String arg1)
{
31
// TODO Auto-generated method stub
32
}
33
@Override
34
public void onSharedObjectDisconnect(ISharedObjectBase arg0)
{
35
// TODO Auto-generated method stub
36
}
37
@Override
38
public void onSharedObjectSend(ISharedObjectBase so, String method, List params)
{
39
// TODO Auto-generated method stub
40
}
41
@Override
42
public void onSharedObjectUpdate(ISharedObjectBase arg0, IAttributeStore arg1)
{
43
// TODO Auto-generated method stub
44
}
45
@Override
46
public void onSharedObjectUpdate(ISharedObjectBase arg0, Map<String, Object> arg1)
{
47
// TODO Auto-generated method stub
48
}
49
}
50
共享对象在被服务器处理完业务逻辑之后,要把处理后的值广播给客户端,最好作为注册句柄被通报。
使用beginUpdate() 和 endUpdate()方法:
1
//将此三个属性值放入共享对象中
2
so.beginUpdate();
3
so.setAttribute("temperature", 20);
4
so.setAttribute("humidity", 50);
5
so.setAttribute("nutrition", 85);
6
so.endUpdate();
posted on 2009-05-26 18:09
kenzhang 阅读(3891)
评论(3) 编辑 收藏