Hi, all.
I've got a question. How to force destroying pipe when user leaves the
APE chat?
I've found the method called ape.fireEvent() I'll write here a piece
of code that initializes our APE chat and makes it functionning
-----------------------------------------------------------------
$(document).ready(function() {
//global variable
client = new APE.Client;
// load the APE client
client.load({
identifier: 'chat',
channel: ape_channel,
complete: function(ape){
new Chat(ape).initialize();
}
});
});
currentUserPipe = 0;
function setCurrentPipe(pubid){
currentUserPipe = pubid;
$('.tab').removeClass('active_tab');
$('#'+pubid).addClass('active_tab');
}
function Chat(ape){
var userPipes = new Array();
// we call this function once APE has finished loading
this.initialize = function(){
// once a new user joins (pipe created), call setup()
ape.addEvent('multiPipeCreate', this.setup);
// when a user joins, update the user list
ape.addEvent('userJoin', this.createUser);
// when a user leaves, destroy them with mighty thunder!
ape.addEvent('userLeft', this.deleteUser);
//user exist
ape.onError('007', this.userName);
// when we want to send data
ape.onCmd('send', this.cmdSend);
// and when we recieve data
ape.onRaw('data', this.rawData);
//alert(muser);
// start the session with a random name!
ape.start({"name":muser});
}
this.setup = function(pipe){
$("#chat_text").keypress(function(event){
if(event.keyCode == '13'){
msg = $("#chat_text").val();
msg = msg.trim();
if(msg!=''){
//alert('The currentUserPipe = '+
currentUserPipe);
//send message to one user (uniPipe)
if (currentUserPipe &&
userPipes[currentUserPipe]){
userPipes[currentUserPipe].send(msg);
//TODO: Don't send to pipe just save
and display on SENDER screen
} else if(currentUserPipe &&
currentUserPipe != 0 && !
userPipes[currentUserPipe]){
/*
* Refactoring
*/
//Showing own message on the
screen
$('#wnd').append('<span>'+userNicks[ape.user.properties.name]+'</
span>: '+ msg+'<br>');
//Sending to DB
var user_to =
$('.active_tab:first').attr('id');
var online_data = '';
for (var key in userPipes) {
if(userPipes[key])
online_data =
online_data + key + ',';
}
$.ajax({
type: 'POST',
data:
'msg='+encodeURIComponent(msg)+'&to='+user_to
+'&online='+online_data,
url:
"message_send.php",
success:
function(data){
//alert(data);
}
});
} //send message to all users
(multiPipe)
else {
pipe.send(msg);
}
}
$("#chat_text").val('');
}
});
/*/ add an event listener on our button
$("#chat_button").click(function(){
msg = $("#chat_text").val();
msg = msg.trim();
if(msg!=''){
//send message to one user (uniPipe)
if (currentUserPipe &&
userPipes[currentUserPipe]){
userPipes[currentUserPipe].send(msg);
}
//send message to all users (multiPipe)
else{
pipe.send(msg);
}
}
$("#chat_text").val('');
});*/
}
this.cmdSend = function(param,pipe){
/*
* Refactoring
*/
//Showing own message on the screen
$('#wnd').append('<span>'+userNicks[ape.user.properties.name]+'</
span>: '+ param.msg+'<br>');
//Sending to DB
var user_to = $('.active_tab:first').attr('id');
var online_data = '';
for (var key in userPipes) {
if(userPipes[key])
online_data =
online_data + key + ',';
}
$.ajax({
type: 'POST',
data:
'msg='+encodeURIComponent(param.msg)+'&to='+user_to
+'&online='+online_data,
url: "message_send.php",
success: function(data){
//alert(data);
}
});
}
this.rawData = function(raw, pipe){
//add message to previouse message (from the same user)
//if (pipe.lastMsg && pipe.lastMsg.from.pubid ==
raw.data.from.pubid) {
// $('#wnd').append(decodeURIComponent(raw.data.msg) +
'<br>');
//}
//add message with new user name (from new user)
//else {
$('#wnd').append('<span>' +
userNicks[raw.data.from.properties.name] + '</span>: ' +
decodeURIComponent(raw.data.msg) + '<br>');
//}
pipe.lastMsg = {from:raw.data.from};
}
this.createUser = function(user, pipe){
// a user has joined so prepend them to the debug window
//alert('new user: '+user.properties.name);
//Transform the user into a pipe
//userPipes[user.pubid] = ape.getPipe(user.pubid);
userPipes[user.properties.name] = ape.getPipe(user.pubid);
//get user nick
$.ajax({
type:'post',
url: 'NickName.php',
data: 'address_id='+user.properties.name,
success: function(nick) {
userNicks[user.properties.name] = nick;
$('#'+user.properties.name).addClass('online');
//add new user to user tab
/*if (ape.user.pubid != user.pubid) {
$('#und').append('<div id="' +
user.pubid + '" class="tab"
onClick="setCurrentPipe(this.id);">' + userNicks[user.properties.name]
+ '</div>');
}*/
}
});
}
this.deleteUser = function(user, pipe){
// a user has left so update the debug window
//alert('deleted user: '+user.properties.name);
//delete user from user tab
//$('#'+user.pubid).remove();
$('#'+user.properties.name).removeClass('online');
//delete user nick
userNicks[user.properties.name] = null;
//unset user uniPipe
userPipes[user.properties.name] = null;
}
this.userName = function(user, pipe){
// user already exist
alert('user exist');
//close window
window.close();
}
/*this.getOnlineUsers = function() {
var online_data;
for (var key in userPipes) {
online_data = online_data + key + ',';
}
}*/
}
------------------------------------------------------------------------------------------
So I tried to place in the place where the events of ape are binding
window.onbeforeunload = function() {
ape.fireEvent('userLeft');
}
But nothing happens. Maybe this is because I'm not passing arguments
to fireEvent.
Any help will be appreciated
--
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/