Using Communication Objects > Application object > Application.onConnect |
![]() ![]() ![]() |
Application.onConnect
The application.onConnect
event handler is invoked on the server when a client calls the NetConnection.connect
method. The handler is automatically passed a reference to a server-side Client
object, representing the client that's attempting to connect, as well as any developer-defined parameters passed to the NetConnection.onConnect
method.
Setting up methods in application.onConnect
is a good approach when you want to set up different methods based on user login information. However, if you want to set up a method that is available to all clients, use the prototype
property of the Client object:
// A user has connected application.onConnect = function( newClient, userName ) { if (userName == "admin") { newClient.adminFunc= function(param) { // some code that's only useful to the admin newClient.myAdminProperty = param; } } else { // code for most cases } // Allow the logon application.acceptConnection( newClient ); } // this part could be in a separate file // Every client (including admin) is going to have this function // (because of the way "prototype" works). Client.prototype.commonFunction = function (myColor) { // some code // use this to refer to the client instead of newClient this.color = myColor; }
Also, if you want to use the call
method on the client object that is connecting, make sure you call application.acceptConnection
and know that the client is connected before issuing any additional commands:
application.onConnect = function(clientObj,name,passwd) { // First accept the connection application.acceptConnection(clientObj); // After client is registered with the application instance // you can use "call" method clientObj.call("onWelcome", "You are now connected!!!"); return; // Once you call acceptConnection or // rejectConnection within onConnect, return value is ignored. }
Tip: If you're using components, see Application.onConnectAccept and application.onConnectReject.
![]() ![]() ![]() |