From f1b4f5b175ca4d561cfd599082b798d7ff6b5bd6 Mon Sep 17 00:00:00 2001 From: fritzy Date: Fri, 19 Sep 2008 12:03:29 +0000 Subject: [PATCH] * BOSH fixes git-svn-id: svn://netflint.net/xmpphp@56 ef36c318-a008-4979-b6e8-6b496270793b --- XMPPHP/BOSH.php | 21 ++++++++++++----- XMPPHP/Log.php | 1 + webclient_example.php | 54 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/XMPPHP/BOSH.php b/XMPPHP/BOSH.php index 1c8a3a9..113b957 100644 --- a/XMPPHP/BOSH.php +++ b/XMPPHP/BOSH.php @@ -55,11 +55,9 @@ class XMPPHP_BOSH extends XMPPHP_XMPP { $this->sid = null; if($session) { - print "loading session..."; $this->loadSession(); } if(!$this->sid) { - print "loading..."; $body = $this->__buildBody(); $body->addAttribute('hold','1'); $body->addAttribute('to', $this->host); @@ -74,6 +72,10 @@ class XMPPHP_BOSH extends XMPPHP_XMPP { $response = $this->__sendBody($body); $rxml = new SimpleXMLElement($response); $this->sid = $rxml['sid']; + + } else { + $buff = ""; + xml_parse($this->parser, $buff, false); } } @@ -162,15 +164,22 @@ class XMPPHP_BOSH extends XMPPHP_XMPP { } public function loadSession() { - foreach($_SESSION['XMPPHP_BOSH'] as $key => $value) { - $this[$key] = $value; + if(is_array($_SESSION['XMPPHP_BOSH'])) { + foreach($_SESSION['XMPPHP_BOSH'] as $key => $value) { + #print "Loading $key as $value
"; + $this->$key = $value; + } } } public function saveSession() { $_SESSION['XMPPHP_BOSH'] = Array(); - foreach ($this as $key => $value) { - $_SESSION['XMPPHP_BOSH'][$key] = $value; + #$variables = Array('server', 'user', 'password', 'resource', 'fulljid', 'basejid', 'authed', 'session_started', 'auto_subscribe', 'use_encryption', 'host', 'port', 'stream_start', 'stream_end', 'disconnected', 'sent_disconnected', 'ns_map', 'current_ns', 'lastid', 'default_ns', 'been_reset', 'last_send', 'use_ssl', 'rid', 'sid', 'http_server','http_buffer', 'xml_depth'); + $variables = Array('rid', 'sid'); + foreach ($variables as $key) { + #print "Saving $key as {$this->$key}
"; + flush(); + $_SESSION['XMPPHP_BOSH'][$key] = (string) $this->$key; } } } diff --git a/XMPPHP/Log.php b/XMPPHP/Log.php index 635e68d..f8a901a 100644 --- a/XMPPHP/Log.php +++ b/XMPPHP/Log.php @@ -112,5 +112,6 @@ class XMPPHP_Log { protected function writeLine($msg, $runlevel, $time) { //echo date('Y-m-d H:i:s', $time)." [".$this->names[$runlevel]."]: ".$msg."\n"; echo $time." [".$this->names[$runlevel]."]: ".$msg."\n"; + flush(); } } diff --git a/webclient_example.php b/webclient_example.php index 2a27b81..eb40e82 100644 --- a/webclient_example.php +++ b/webclient_example.php @@ -1,4 +1,56 @@ "; + +#Use XMPPHP_Log::LEVEL_VERBOSE to get more logging for error reports +#If this doesn't work, are you running 64-bit PHP with < 5.2.6? +$conn = new XMPPHP_BOSH('server.tld', 5280, 'user', 'password', 'xmpphp', 'server.tld', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_INFO); +$conn->autoSubscribe(); + +try { + if(isset($_SESSION['messages'])) { + foreach($_SESSION['messages'] as $msg) { + print $msg; + flush(); + } + } + $conn->connect('http://server.tld:5280/xmpp-httpbind', 1, true); + #while(true) { + $payloads = $conn->processUntil(array('message', 'presence', 'end_stream', 'session_start')); + foreach($payloads as $event) { + $pl = $event[1]; + switch($event[0]) { + case 'message': + if(!isset($_SESSION['messages'])) $_SESSION['message'] = Array(); + $msg = "---------------------------------------------------------------------------------\n{$pl['from']}: {$pl['body']}\n"; + print $msg; + $_SESSION['messages'][] = $msg; + flush(); + $conn->message($pl['from'], $body="Thanks for sending me \"{$pl['body']}\".", $type=$pl['type']); + if($pl['body'] == 'quit') $conn->disconnect(); + if($pl['body'] == 'break') $conn->send(""); + break; + case 'presence': + print "Presence: {$pl['from']} [{$pl['show']}] {$pl['status']}\n"; + break; + case 'session_start': + print "Session Start\n"; + $conn->getRoster(); + $conn->presence($status="Cheese!"); + break; + } + } + #} +} catch(XMPPHP_Exception $e) { + die($e->getMessage()); +} +$conn->saveSession(); + +print ""; +print ""; ?>