Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

Events list

ptejada edited this page Aug 9, 2012 · 21 revisions

There are a handful of built-in events in the framework. Some more useful and advance then others. This built-in commands should not be manually sent as it might brake the framework workflow. Below is a list of the events.

client: connect | ready | restored | error* | dead | quit

channel: joined | join | left | message | data


client

  • connect:

    This event is triggered right before the client sends the command to connect to the APE Server along with the user information/object client.user. This event is probably the must powerful and useful in the whole framework, it makes use of the smart trigger by holding the connect process when returned false.

    Take a look at this example where the connect event is used to get the user's name prior to connecting to the server without breaking your application workflow:

     //Create new client object
     var client = new APS("localhost:6969");
     
     //Adds the `connect` event to the client
     client.on("connect", function(){
     	//Check if the user's name is known
     	if(!!client.user.name){
     		//Simply ask for the user name
     		client.user.name = prompt("Which nick name would you like?", "Pablo");
     	}
     	//the framework continues connecting
     })
     
     //Subscribes to the channel `chat`
     client.sub("chat");

    Below is more advance and useful example. It uses jQuery to make an ajax request to get the user's info without breaking the workflow of your application still:

     //Create new client object
     var client = new APS("localhost:6969");
     
     //Adds the `connect` event to the client
     client.on("connect", function(){
     	//Check if the user's name is known
     	if(!!client.user.name){
     		//Ajax call to get the user info
     		$.get("script_to_get_user_info.php", function(data){
     			client.user = data.user;
     			//Resumes the connecting proccess
     			client.connect();
     		});
     		
     		//Holds the connecting process
     		return false;
     	}
     	//We have a name, continue connecting to the APE server
     })
     
     //Subscribes to the channel `chat`
     client.sub("chat");

    Please note that the client.connect() will re-trigger the connect events so if your ajax callback function does not sets a value to client.user.name you might create a infinity loop in this specific example.

  • ready:

    When this event is triggered the client object has been completely initiated and connected to the APE Server. No parameter are passed to this event.

  • restored:

    this event is triggered when the client has been restored from a session. When the client is restored from a session the connect event is never triggered. No parameters are passed to this event.

  • error:

    this event is triggered when an error is explicitly sent from the server. The error code and name are passed as parameter to this event. You can also add an error event for an specific error code, for example to add event for the error code 007 you would add the event error007. For a full list of error codes check Error codes.

  • dead:

    This event is triggered when the client can no longer communicate with the APE Server. No parameters are passed to this events.

  • quit:

    This event is triggered when the client is programically and intentionally disconnected using the client.quit() function.

channel:

There are two ways you could add events to a channel. Directly from an existing channel object channel.on() or at any time from the client object using client.onChannel(). Remember that channel's events can be added to the client object to act as global events. For example if you add a joined to client it will be triggered every time the user joins a channel.

  • joined: triggered when the current user successfully joins a channel. It can been seen as the sub() method's callback function.

Still developing...


Clone this wiki locally