Room under red5

Source: Internet
Author: User
Tags arraycon sendmsg

Package com. test;

Import java. util. arraylist;
Import java. util. hashmap;
Import
Java. util. iterator;
Import java. util. List;

Import org. red5.server. Adapter. applicationadapter;
Import
Org. red5.server. API. iclient;
Import
Org. red5.server. API. iconnection;
Import org. red5.server. API. iseries;
Import
Org. red5.server. API. Service. iservicecapableconnection;
Import
Org. red5.server. API. red5;
Public class roomapplication extends
Applicationadapter {

Private Static hashmap <string, iseries> scopemap
= New hashmap <string, ismap> ();
Private Static
Hashmap <string, list> usermap = new
Hashmap <string, list> ();
Private Static hashmap <string, string>
Videomap = new hashmap <string, string> ();
@ Override
Public Boolean
Roomconnect (iconnection Conn, object [] arg1 ){
// Todo auto-generated
Method stub
String username = arg1 [0]. tostring ();
String roomname =
Arg1 [1]. tostring ();
If (usermap. containskey (roomname )){
List list =
Usermap. Get (roomname );
List. Add (username );
} Else {

List <string> List = new arraylist <string> ();

List. Add (username );
Usermap. Put (roomname, list );
}

Conn. getclient (). setattribute ("username", username );

Return
Super. roomconnect (Conn, arg1 );
}

@ Override
Public void roomdisconnect (iconnection client ){
// Todo
Auto-generated method stub
Super. roomdisconnect (client );
}

@ Override
Public Boolean roomjoin (iclient client, isw.s ){
//
Todo auto-generated method stub
System. Out. println ("room join" +
S. getname ());
Updateuserlist (S. getname ());
String roomname =
S. getname ();
String username =
Client. getattribute ("username"). tostring ();

Sendvideo (username, roomname );
Return super. roomjoin (client,
S );
}

Public void sendvideo (string username, string roomname ){

If (videomap. containskey (roomname )){

System. Out. println ("I want to get the video ");

Iscope S =
Scopemap. Get (roomname );
System. Out. println (s );
Iterator
<Iconnection> iterator = scope. getconnections ();

While (iterator. hasnext ()){
System. Out. println ("connection ");

Iconnection conn = iterator. Next ();

If (conn. getclient (). getattribute ("username"). tostring (). inclusignorecase (username )){

If (conn instanceof iservicecapableconnection)

{

// Forward messages
System. Out. println ("conn =" +
Conn );
Iservicecapableconnection SC = (iservicecapableconnection)
Conn;
SC. Invoke ("netstream", new
Object [] {videomap. Get (roomname )});
}
}
}

}
}

@ Override
Public void roomleave (iclient client, iscope s ){
// Todo
Auto-generated method Stu
String username =
Client. getattribute ("username"). tostring ();
String roomname =
S. getname ();
List list = usermap. Get (roomname );

List. Remove (username );

Updateuserlist (roomname );

If (videomap. containskey (roomname )){

If (videomap. Get (roomname). tostring (). inclusignorecase (username )){

Videomap. Remove (roomname );
}
}
Super. roomleave (client,
S );
}

@ Override
Public Boolean roomstart (iscope s ){
// Todo
Auto-generated method stub
System. Out. println ("room start" +
S. getname ());
Scopemap. Put (S. getname (), S );
Return
Super. roomstart (s );
}

@ Override
Public void roomstop (iscope S)
{
// Todo auto-generated method stub

Scopemap. Remove (S. getname ());
System. Out. println ("the room is completely closed ");

Super. roomstop (s );
}
Public void sendtoroom (string MSG, string
Roomname, string username, string tousername ){
Isw.scope =
Scopemap. Get (roomname );
Iterator <iconnection> iterator =
Scope. getconnections ();
While (iterator. hasnext ()){
Iconnection Conn
= Iterator. Next ();
String connusername =
Conn. getclient (). getattribute ("username"). tostring ();
If (conn instanceof
Iservicecapableconnection)
{
Iservicecapableconnection SC =
(Iservicecapableconnection) Conn;

If ("all". inclusignorecase (tousername )){
// Forward messages


String STR = username + "said:" + MSG;

SC. Invoke ("sendtoroom", new object [] {STR });

} Else
If (connusername. inclusignorecase (username )){
String STR = "you pair" +
Tousername + "said:" + MSG;
SC. Invoke ("sendtoroom", new
Object [] {STR });
} Else
If (connusername. inclusignorecase (tousername )){
String STR = username +
"Say to you:" + MSG;
SC. Invoke ("sendtoroom", new object [] {STR });

}
}
}
}
Public void updateuserlist (string roomname ){

System. Out. println ("Send userlist =" + roomname );
List list =
Usermap. Get (roomname );
System. Out. println ("with" + list. Size () + "sub ");

Iscope S = scopemap. Get (roomname );
System. Out. println (s );
Iterator
<Iconnection> iterator = scope. getconnections ();

While (iterator. hasnext ()){
System. Out. println ("connection ");

Iconnection conn = iterator. Next ();
If (conn instanceof
Iservicecapableconnection)
{
// Forward messages

System. Out. println ("conn =" + conn );
Iservicecapableconnection SC =
(Iservicecapableconnection) Conn;
SC. Invoke ("updatelist", new
Object [] {list });
}
}

}
Public void playvideo (string
Username, string roomname ){
Isdomainscope = scopemap. Get (roomname );

Videomap. Put (roomname, username );
Iterator <iconnection> iterator =
Scope. getconnections ();
While (iterator. hasnext ()){
Iconnection Conn
= Iterator. Next ();
If (conn instanceof
Iservicecapableconnection)
{
Iservicecapableconnection SC =
(Iservicecapableconnection) Conn;
SC. Invoke ("netstream", new
Object [] {username });
}
}
}
Public void escvideo (string
Roomname ){
Isdomainscope = scopemap. Get (roomname );

Videomap. Remove (roomname );
Iterator <iconnection> iterator =
Scope. getconnections ();
While (iterator. hasnext ()){
Iconnection Conn
= Iterator. Next ();
If (conn instanceof
Iservicecapableconnection)
{
Iservicecapableconnection SC =
(Iservicecapableconnection) Conn;
SC. Invoke ("escvideo", new
Object [] {});
}
}
}
Public void checkvideo (string
Username, string roomname ){
System. Out. println ("checkvideo ");

Isdomainscope = scopemap. Get (roomname );
Iterator
<Iconnection> iterator = scope. getconnections ();

While (iterator. hasnext ()){
Iconnection conn = iterator. Next ();

String connusername =
Conn. getclient (). getattribute ("username"). tostring ();

System. Out. println ("conusername ");

If (username. inclusignorecase (connusername )){
If (conn instanceof
Iservicecapableconnection)
{
Iservicecapableconnection SC =
(Iservicecapableconnection) Conn;

If (videomap. containskey (roomname )){


SC. Invoke ("cannotvideo", new object [] {});
} Else {

System. Out. println ("canvideo ");
SC. Invoke ("canvideo", new
Object [] {});
}
}
}
}
}

}

 

Mxml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application
Xmlns: MX = "http://www.adobe.com/2006/mxml"
Layout = "absolute" creationcomplete = "Con ()">
<Mx: SCRIPT>

<! [CDATA [
Import MX. Events. listevent;
Import
Flash. utils. flash_proxy;

Import MX. Core. uicomponent;
Import
MX. messaging. abstractconsumer;
Import
MX. Collections. arraycollection;
Import flash. Events .*;
Import
Flash. Media. video;
Import flash.net .*;
Import
Flash. External. externalinterface;
[Bindable]
Private VaR
Net: netconnection;
Import MX. Controls. Alert;
Private VaR
Username: string;
Private var roomname: string;
[Bindable]

Private var arraycon: arraycollection;
[Bindable]
Private VaR
Comboarraycon: arraycollection;
VaR ns: netstream;
VaR
Nsplay: netstream;
Public Function con (): void {


// Chat. addeventlistener (event. enter_frame, onenterframe );
Username =
Parameters. Name;
Roomname = parameters. roomname;

// Username = "22 ";

// Roomname = "11 ";

If (comboarraycon = NULL ){
Comboarraycon = new arraycollection ();

}
Comboarraycon. additem ("all ");

// Alert. Show (username, roomname );
L. Text = "you are in the" + roomname + "chat room! ";

If (net = NULL ){
Net = new netconnection ();

Net. addeventlistener (netstatusevent. net_status, netstatus );

Net. addeventlistener (asyncerrorevent. async_error, onasyncerror );

Net. Connect ("rtmp: // 192.168.1.32/mychat/" + roomname + "/", username, roomname );

Net. Client = this;
}
}
Private function
Onasyncerror (Event: asyncerrorevent): void {
Trace ("asyncerror ");

}
Private function onenterframe (Event: Event): void {


}
Public Function netstatus (Event: netstatusevent): void {

Trace (event.info. Code );
Switch (event.info. Code ){
Case
"Netconnection. Connect. Success ":
NS = new netstream (net );

VaR mic: microphone = microphone. getmicrophone ();

Mic. setuseechosuppression (true );
Mic. setsilencelevel (0 );

NS. attachaudio (MIC );
Try {
VaR cam: Camera =
Camera. getcamera ();
Cam. setmode (271,241, 15 );

Cam. setquality (144000,85 );

NS. attachcamera (CAM );

} Catch (error: Error ){
Trace (error );
}

Getvideo ();
Break;
}

}

 

Protected function image1_clickhandler (Event: mouseevent): void

{
Externalinterface. Call ("geturl ");
}

Private function onenterclick (Event: keyboardevent): void {

If (event. keycode = 13 ){
Sendmsg ();
}
}
Private
Function onsendclick (Event: Event): void {
Sendmsg ();
}

Private function sendmsg (): void {
If (msg. Text = ""){

Alert. Show ("Enter the content! ");
} Else {

Net. Call ("sendtoroom", null, MSG. Text, roomname, username, O. selecteditem );

MSG. Text = "";
This. setfocus ();

This. focusmanager. setfocus (MSG );
MSG. setselection (0, 0 );


}
}
Public Function sendtoroom (STR: string): void {
C. Text =
C. Text + STR + "/N ";
This. focusmanager. setfocus (MSG );
}

Public Function updatelist (User: string): void {
Try {

Trace ("Update ");
VaR arr: array;
If (user = ""){
Arr =
New array ();
} Else {
Arr = user. Split (",");

}
Arraycon = new arraycollection (ARR );

} Catch (ERR: Error ){
Trace (ERR );
}

}

Protected Function
Userlist_itemclickhandler (Event: listevent): void
{

If (! Comboarraycon. Contains (userlist. selecteditem )){

Comboarraycon. additem (userlist. selecteditem );
O. selectedindex =
Comboarraycon. length;
} Else {
O. selectedindex =
Comboarraycon. getitemindex (userlist. selecteditem );



}


}
Private function
Olabel (Object: Object): String {
Return object. tostring ();
}

Public Function netstream (Name: string): void {

Nsplay = new
Netstream (net );
Nsplay. Play (name );
VaR video: Video = new
Video (V. Width, V. Height );
V. addchild (video );

Video. attachnetstream (nsplay );
Video. width = V. width;

Video. Height = V. height;



}
Public Function
Playvideo (): void {

Net. Call ("checkvideo", null, username, roomname );

}
Public
Function getvideo (): void {

Net. Call ("sendvideo", null, username, roomname );

}
Public
Function canvideo (): void {
NS. Publish (username );
W. Enabled =
False;
S. Enabled = true;

Net. Call ("playvideo", null, username, roomname );
}
Public Function
Cannotvideo (): void {
Alert. Show ("someone takes the lead, please wait ");

}

Public Function escvideo (): void {
Trace ("escvideo ");

Nsplay. Close ();
W. Enabled = true;
S. Enabled = false;

}


]>
</MX: SCRIPT>
& Lt; mx: Label width = "168"
Id = "L"/>
<Mx: textarea width = "472" Height = "239" id = "C"
Editable = "false" x = "5" Y = "26"/>
<Mx: textinput x = "67" Y = "273"
Width = "335" id = "MSG" keyup = "onenterclick (event)"/>
& Lt; mx: List x = "484"
Y = "24" width = "122" Height = "241" id = "userlist" dataprovider = "{arraycon }"
Itemclick = "userlist_itemclickhandler (event)"/>
<Mx: ComboBox x = "-1"
Y = "273" editable = "false" width = "60" id = "O" dataprovider = "{comboarraycon }"
/>
<Mx: button x = "414" Y = "273" label = "send" width = "63" id = "send"
Click = "onsendclick (event)"/>
<Mx: button x = "485" Y = "273" label = "I Want To video"
Click = "playvideo ()" id = "W"/>
<Mx: videodisplay x = "623" Y = "24"
Width = "271" Height = "241" id = "v"/>
<Mx: button x = "563" Y = "273"
Label = "no video" Click = "net. Call ('ecscvideo', null, roomname)" enabled = "false"
Id = "S"/>

</MX: Application>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.