[jdev] Need help for developing a simple jabber client in php web application
Srinivasan.M, ANGLER - EIT
srinivasanm at angleritech.com
Mon Mar 17 22:31:05 CDT 2008
Dear team,
I need a urgent requirement of creating a simple jabber client in php, i
am using ejabberd jabber server running.
I using the class_jabber.php and related files. Though i am new to this
programming (socket) - could you please help me out in this regard.
Herewith, I am sending you the code which iam using right now for your
kind notice.
----------------------------------------------------------
<?php
// include the Jabber class
require_once("class_Jabber.php");
require_once('../settings/connect.php');
require_once('class_queryHandler.php');
require_once('class_ConnectionSocket.php');
require_once('classJabberMn.php');
$jABBER_LoginData = jabber_login_username($PHP_AUTH_USER);
//This function is used to get name/position of the person who sends
messages
$jABBER_LoginData1 = jabber_login_username_byId($sendPeId);
//message from person
$msgFrom = "test_".$jABBER_LoginData1[0]."@jabberServerName.net";
//Receiving person id
define("JABBER_USERNAME_FROM",$jABBER_LoginData1[0]);
define("JABBER_POSITION_FROM",$jABBER_LoginData1[1]);
$toPeId = $_GET['toPeId'];
$sendPeId = $_GET['sendPeId'];
$sendToId = $_POST['toPeId'];
//$sendById = $_POST['sendById'];
//Set your Jabber server hostname, username, and password here
define("JABBER_SERVER","jabberServerName.net");
define("JABBER_USERNAME",$jABBER_LoginData[0]);
define("JABBER_PASSWORD","scg");
define("JABBER_USER_ID",$jABBER_LoginData[2]);
$jabberUserData = $jABBER_LoginData[0].' online in the jabberServerName
Account';
//if($sendToId != '' || $sendById != '') {
//send message info
$jABBER_LoginData2 = jabber_login_username_sendToMsg($sendPeId);
$jABBER_Data_prsn = jabber_login_username_sendToMsg($sendById);
$jids = $jABBER_Data_prsn[0];
$messageToBeSent = $_POST['message'];
$messageSendBy = $jABBER_LoginData2[0];
//message sent by person/position
$messageSendBy_person = $jABBER_LoginData[3];
$messageSendBy_position = $jABBER_LoginData[4];
define("MESSAGESENDBY_PERSON",$messageSendBy_person);
define("MESSAGESENDBY_POSITION",$messageSendBy_position);
//}
define("RUN_TIME",300); // set a maximum run time of 5 Minutes
define("CBK_FREQ",5); // fire a callback event every 5 seconds
?>
<html>
<head>
<title>jabberServerName chat module</title>
<script language="JavaScript" type="text/javascript"
src="/forms/lib_form/prototype.js"></script>
<script language="JavaScript" type="text/javascript"
src="/forms/lib_form/effects.js"></script>
<LINK rel="stylesheet" type="text/css" href="/forms/css/styles_ie.css">
</head>
<?
class OurMessenger {
function OurMessenger() {
echo "Created!\n";
// create an instance of the Jabber class
$this->jab = new Jabber(true);
// set handlers for the events we wish to be notified about
$this->jab->set_handler("connected",$this,"handleConnected");
$this->jab->set_handler("authenticated",$this,"handleAuthenticated");
$this->jab->set_handler("authfailure",$this,"handleAuthFailure");
//$this->jab->set_handler("servicesdiscovered",$this,"handleServicesDisc
overed");
//$this->jab->set_handler("servicesupdated",$this,"handleServiceInfo");
//$this->jab->set_handler("browseresult",$this,"handleBrowseResult");
$this->jab->set_handler("heartbeat",$this,"handleHeartbeat");
$this->jab->set_handler("error",$this,"handleError");
$this->jab->set_handler("message_normal",$this,"handleMessage");
$this->jab->set_handler("message_chat",$this,"handleMessage");
$this->jab->set_handler("debug_log",$this,"handleDebug");
}
function executingMessenger(){
// connect to the Jabber server
if (!$this->jab->connect(JABBER_SERVER)) {
die("Could not connect to the Jabber server!\n");
}
// now, tell the Jabber class to begin its execution loop
$this->jab->execute(CBK_FREQ,RUN_TIME);
// Note that we will not reach this point (and the execute() method
will not
// return) until $this->jab->terminated is set to TRUE. The execute()
method simply
// loops, processing data from (and to) the Jabber server, and firing
events
// (which are handled by our TestMessenger class) until we tell it to
terminate.
//
// This event-based model will be familiar to programmers who have
worked on
// desktop applications, particularly in Win32 environments.
// disconnect from the Jabber server
$this->jab->disconnect();
}
// called when a connection to the Jabber server is established
function handleConnected() {
//echo "Connected!\n";
// now that we're connected, tell the Jabber class to login
$this->jab->login(JABBER_USERNAME,JABBER_PASSWORD);
}
// called after a login to indicate the the login was successful
function handleAuthenticated() {
//echo "Authenticated!\n";
// set this user's presence
global $messageToBeSent, $jids;
$today = date("Y-m-d H:i:s");
if($messageToBeSent != '') {
echo $messageToDisplay = "
<div id='message_comments_20671_20670'>
<table border='0' cellspacing='0' cellpadding='0' width='100%'>
<tr>
<td class='data' width='100%'>
<table width='100%' cellspacing='0' cellpadding='0'>
<tr bgcolor='#e6e6e6'>
<td class='data' width='50%'>
<strong>".ucfirst(MESSAGESENDBY_PERSON)."</strong> |
<strong>".MESSAGESENDBY_POSITION."</strong>
</td>
<td class='data' width='50%' align='right'>
".$today."
</td>
</tr>
<tr>
<td class='data' colspan='2'>
".$messageToBeSent."
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
";
$this->jab->message($jids, 'chat', NULL, $messageToBeSent);
//$this->jab->terminated = true;
$messageToDisplay='';
}
//sleep(1);
$this->jab->set_presence("", 'so what');
}
// called after a login to indicate that the login was NOT successful
function handleAuthFailure($code,$error) {
echo "Authentication failure: $error ($code)\n";
// set terminated to TRUE in the Jabber class to tell it to exit
$this->jab->terminated = true;
}
// called periodically by the Jabber class to allow us to do our own
// processing
function handleHeartbeat() {
//hardcoded !!!
if (onlineOfflineStatus(JABBER_USER_ID) <> 1) {
//echo "offline<BR>";
}
else {
//echo "online<BR>";
}
$statusCheck = $this->jab->connectStatus();
print_r($statusCheck);
//I am commenting this part of the code for test purpose
/*
$statusCheckExplore = explode('~',$statusCheck);
if($statusCheckExplore[0] == 'eof' && $statusCheckExplore[1] ==
'TRUE') {
//$this->_connection = &new $connector();
//$this->_connection->socket_close();
//sleep(1);
$this->jab->terminated = true;
$this->OurMessenger();
$this->executingMessenger();
}
*/
ob_flush();
flush();
}
// called when a message is received from a remote contact
function handleMessage($from,$to,$body,$subject,$thread,$id,$extended)
{
global $msgFrom;
$today = date("Y-m-d H:i:s");
//echo 'from-->'.$from.$msgFrom;
$fromMsg = explode('/', $from);
$toMsg = explode('/', $to);
if($fromMsg[0] == $msgFrom) {
echo $messageToShow = "
<div id='message_comments_20671_20670'>
<table border='0' cellspacing='0' cellpadding='0' width='100%'>
<tr>
<td class='data' width='100%'>
<table width='100%' cellspacing='0' cellpadding='0'>
<tr bgcolor='#D7EBFF'>
<td class='data' width='50%'>
<strong>".ucfirst(JABBER_USERNAME_FROM)."</strong> |
<strong>".JABBER_POSITION_FROM."</strong>
</td>
<td class='data' width='50%' align='right'>
".$today."
</td>
</tr>
<tr>
<td class='data' colspan='2'>
".$body."
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
";
$this->last_msg_id = $id;
$this->toMsgPrsn = $toMsg[0];
$this->last_msg_from = $from;
}
}
// called when an error is received from the Jabber server
function handleError($code,$error,$xmlns,$packet) {
echo "Error: $error ($code)".($xmlns?" in $xmlns":"noxmlns")." from
$from $node id ".$packet['iq']['@']['id']."\n";
}
function handleDebug($msg,$level) {
"DBG: $msg\n";
}
}
?>
</body>
</html>
----------------------------
please point me out where i am doing wrong here.
I need this asap.
Please help me in this regards, by adjusting the existing code or please
provide me a sample code for doing this.
Thanks and Regards,
Sriman.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.jabber.org/pipermail/jdev/attachments/20080318/a4ec2ee8/attachment-0002.htm>
More information about the JDev
mailing list