Package echat;
Import org. red5.server. Adapter. applicationadapter;
Import org. red5.server. API. iconnection;
Import org. red5.server. API. iseries;
Import org. red5.server. API. red5;
Import org. red5.server. API. Service. iservicecapableconnection;
Import org. slf4j. Logger;
Import org. red5.logging. red5loggerfactory;
Import java. util .*;
Public class application extends applicationadapter
{
Private Static logger log = red5loggerfactory. getlogger (application. class, "echat ");
/**
15 * called when each new client is connected!
16 * Here we cover the implementation of the parent class.
17 */
Public Boolean appconnect (iconnection con, object [] Params)
{
Log. debug ("new client connectting chat room ");
Return true;
}
/**
* Called when the client is disconnected!
* Here we cover the implementation of the parent class.
*/
Public void appdisconnect (iconnection conn)
{
Log. debug (conn. getclient (). GETID () + "Disconnect ");
}
/**
* The user name must be included in the chat room. If the user name is blank, messages cannot be sent or received.
* @ Param Params the client calls parameters on the server.
*/
Public void jionchatroom (object [] Params)
{
String nickname = Params [0]. tostring ();
If (null = nickname | "". Equals (nickname ))
{
Return;
}
Else
{
// Set the user nickname.
Iconnection conn = red5.getconnectionlocal ();
Conn. setattribute ("nickname", nickname );
}
// Send a notification to all the users in the chat room. A new user joins.
Iscope scope = red5.getconnectionlocal (). getscope ();
Iterator it = scope. getconnections (). iterator ();
Int x = 0;
For (; it. hasnext ();)
{
Set connections = (SET) it. Next ();
Iconnection tempconn = (iconnection) connections. iterator (). Next ();
If (tempconn instanceof iservicecapableconnection)
{
Iservicecapableconnection SC = (iservicecapableconnection) tempconn;
// The server calls the client flash method.
SC. Invoke ("showjoinininfo", new object [] {nickname });
}
}
}
/**
* Send messages to all users in the chat room
* @ Param Params
*/
Public void saytoall (object [] Params)
{
Iconnection conn = red5.getconnectionlocal ();
// Conn. setattribute ("nickname", nickname );
String nickname = conn. getattribute ("nickname"). tostring ();
String saywhat = Params [0]. tostring ();
// Send a message to the chatting room owner.
Iscope scope = red5.getconnectionlocal (). getscope ();
Iterator it = scope. getconnections (). iterator ();
For (; it. hasnext ();)
{
Set connections = (SET) it. Next ();
Iconnection tempconn = (iconnection) connections. iterator (). Next ();
If (tempconn instanceof iservicecapableconnection)
{
Iservicecapableconnection SC = (iservicecapableconnection) tempconn;
// The server calls the client flash method.
SC. Invoke ("showmessage", new object [] {nickname + ":" + saywhat });
}
}
}
}
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application fontsize = "12" xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute">
<Mx: SCRIPT>
<! [CDATA [
Private var NC: netconnection;
Private function connectandjoinroom (E: Event): void
{
VaR nickname: String = nicknametextinput. text;
If (nickname = "")
{
Return;
}
Else
{
If (NC = NULL)
{
Initialnetconnection ();
}
}
}
Private function initialnetconnection (): void
{
NC = new netconnection ();
NC. addeventlistener (netstatusevent. net_status, connectstatus );
NC. Client = this;
NC. Connect ("rtmp: // localhost/echat", null );
}
Private function connectstatus (Event: netstatusevent): void
{
If (event.info. Code = "netconnection. Connect. Success ")
{
NC. Call ("jionchatroom", null, nicknametextinput. Text );
Sendbutton. Enabled = true;
}
}
Private function sendmessage (): void
{
VaR sendstring: String = inputwhatyouwantsay. text;
Inputwhatyouwantsay. Text = "";
NC. Call ("saytoall", null, sendstring );
}
Public Function showjoinininfo (message: string): void
{
Roomarea. Text + = message + "join chat room" + "/N ";
}
Public Function showmessage (message: string): void
{
Roomarea. Text + = message + "/N ";
}
]>
</MX: SCRIPT>
<Mx: vbox x = "20" Y = "20">
<Mx: hbox>
<Mx: Label text = "nickname:"> </MX: Label>
<Mx: textinput id = "nicknametextinput">
</MX: textinput>
<Mx: button click = "connectandjoinroom (event)" label = "join chat room">
</MX: button>
</MX: hbox>
<Mx: hbox width = "100%" Height = "300">
<Mx: textarea Height = "100%" width = "100%" id = "roomarea">
</MX: textarea>
</MX: hbox>
<Mx: hbox width = "100%">
<Mx: textinput width = "100%" id = "inputwhatyouwantsay">
</MX: textinput>
<Mx: button enabled = "false" id = "sendbutton" label = "send" Click = "sendmessage ()">
</MX: button>
</MX: hbox>
</MX: vbox>
</MX: Application>