The application object allows you to accept or reject client connection requests, and register the classes and methods of client events. Generally, related events are defined in the main. ASC file on the server.
The server-side program consists of the following event programs: 1: Application. onappstart = function (){
2: // the program to be executed when the application is started for the first time
3: // and will only be executed once
4:} 1: Application. onconnect = function (){
2: // the program to be executed whenever a User links to this application
3: // you can read user data here
4: // create a method that allows the client to execute
5: // or call the custom method of the client.
6 :}
If you use the communication server component, you can also use the application. onconnectaccept and application. onconnectreject events.
You can view the following instance program: 01: // a user has connected
02: application. onconnect = function (newclient, username)
03 :{
04: If (username = 'admin ')
05 :{
06: newclient. adminfunc = function (PARAM)
07 :{
08: // some code that's only useful to the admin
09: newclient. myadminproperty = Param;
10 :}
11:} else
12 :{
13: // code for most cases
14 :}
15: // allow the logon
16: application. acceptconnection (newclient );
17 :}
18: // This part cocould be in a separate file
19: // every client (including admin) is going to have this function
20: // (because of the way 'prototype' works ).
21: client. Prototype. commonfunction = function (mycolor)
22 :{
23: // some code
24: // use this to refer to the client instead of newclient
25: This. Color = mycolor;
26 :}
Or 01: application. onconnect = function (clientobj, name, passwd)
02 :{
03: // first accept the connection
04: application. acceptconnection (clientobj );
05: // After client is registered with the application instance
06: // you can use 'call' Method
07: clientobj. Call ('onwelcome ', 'you are now connected !!! ');
08: return;
09: // once you call acceptconnection or
10: // rejectconnection within onconnect, return value is ignored.
11:}, client, name );
12 :}
13 :}
14: // on the client side you wowould have the following
15: NC = new netconnection ();
16: NC. userdisconnects = function (name ){
17: trace (name + 'quits ');
18 :}
19: NC. Connect ('rtmp:/app_name ', username );
Note: If you want to call the client method, make sure that application. acceptconnection has been called to ensure normal client connection.
1: Application. ondisconnect = function (){
2: // the program to be executed when the user is offline
3: // you can view the user data here.
4: // other related programs
5 :}
You can refer to the following procedure: 01: // on the server side you wocould have the following
02: application. onconnect = function (newclient, name)
03 :{
04: newclient. Name = Name;
05: Return true;
06 :}
07: application. ondisconnect = function (client)
08 :{
09: For (VAR I = 0; I <application. Clients. length; I ++)
10 :{
11: application. Clients [I]. Call ('userdisconnects' 1: Application. onappstop = function (){
2: // the program to be executed when the application ends
3: // after all online users are offline
4: // the program will be executed
5 :}
The main. the ASC file may not contain any of the above events, because other Component definitions may be loaded through the load method, but we can still. the ASC file defines the required events and methods.