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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

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();

2

3

4

5

6
