kinlane
2/28/2011 - 9:30 PM

Persisent XMPP Connection for Google Cloud Print XMPP Print Job Service Using XMPPHP Library

Persisent XMPP Connection for Google Cloud Print XMPP Print Job Service Using XMPPHP Library

// Begin XMPP
$conn = new XMPPHP_XMPP('talk.google.com', 5222, $G_Email, $G_Pass, 'xmpphp', 'gmail.com', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_VERBOSE);
$conn->autoSubscribe();

$vcard_request = array();

//var_dump($conn); 

try {

    $conn->connect(); 
    
    while(!$conn->isDisconnected()) 
    	{
    	
    	$payloads = $conn->processUntil(array('message', 'session_start'));
    	
    	foreach($payloads as $event) 
    		{
    		
    		$pl = $event[1];
    		
    		switch($event[0]) 
    			{
    			
    			case 'message': 
    			
    				$Full_JID =  $conn->fulljid;
    				$Bare_JID = $conn->jid;    			
    			
    				echo "<hr />";
    				
	    			//echo  "Message from: " . $pl['from'] . "<br />";   		

	    			if($pl['from'] == 'cloudprint.google.com')
		    			{
		    			// We have received a push for this Print Proxy ID + User.   
		    			
		    			// Now we can /fetch print jobs for this Proxy, User, Printer ID
		    			
		    			echo "Print Job Notification Received for " . $Printer_Proxy . " / " . $Bare_JID . "<br />";
		    			}   				
	    				
    			break;
    			   
    			case 'session_start': 			

    				$Full_JID =  $conn->fulljid;
    				$Bare_JID = $conn->jid;
    				
				    echo "FULL JID: " . $conn->fulljid . "<br />";
				    echo "JID: " . $conn->jid . "<br />";    

				    echo "<hr />";
    			
					$Body = "<iq from='" . $Full_JID . "' to='" . $Bare_JID . "' type='set' id='1'><subscribe xmlns='google:push'><item channel='cloudprint.google.com/proxy/" . $Printer_Proxy . "' from='cloudprint.google.com'/></subscribe></iq>";				
				    
    				$conn->subscription($Body);   			
    				
    			break;
				
				
    		}
    	}
    }
} catch(XMPPHP_Exception $e) {
    die($e->getMessage());
}