3 * Phomet: a php comet client
5 * Copyright (C) 2008 Morgan 'ARR!' Allen <morganrallen@gmail.com> http://morglog.alleycatracing.com
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 private $sPassword = '';
33 function __construct($sUrl, $sUser='', $sPassword='')
37 $this->oCurl = curl_init();
40 $aHeaders[] = 'Connection: Keep-Alive';
42 curl_setopt($this->oCurl, CURLOPT_URL, $sUrl);
43 curl_setopt($this->oCurl, CURLOPT_HTTPHEADER, $aHeaders);
44 curl_setopt($this->oCurl, CURLOPT_HEADER, 0);
45 curl_setopt($this->oCurl, CURLOPT_POST, 1);
46 curl_setopt($this->oCurl, CURLOPT_RETURNTRANSFER,1);
48 if (!is_null($sUser) && mb_strlen($sUser) > 0) {
49 curl_setopt($this->oCurl, CURLOPT_USERPWD,"$sUser:$sPassword");
62 $msgHandshake = array();
63 $msgHandshake['channel'] = '/meta/handshake';
64 $msgHandshake['version'] = "1.0";
65 $msgHandshake['minimumVersion'] = "0.9";
66 $msgHandshake['supportedConnectionTypes'] = array('long-polling');
67 $msgHandshake['id'] = $this->nNextId++;
69 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
71 $data = curl_exec($this->oCurl);
73 if(curl_errno($this->oCurl))
74 die("Error: " . curl_error($this->oCurl));
76 $oReturn = json_decode($data);
78 if (is_array($oReturn)) {
79 $oReturn = $oReturn[0];
82 $bSuccessful = ($oReturn->successful) ? true : false;
86 $this->clientId = $oReturn->clientId;
92 public function connect()
94 $aMsg['channel'] = '/meta/connect';
95 $aMsg['id'] = $this->nNextId++;
96 $aMsg['clientId'] = $this->clientId;
97 $aMsg['connectionType'] = 'long-polling';
99 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
101 $data = curl_exec($this->oCurl);
104 function disconnect()
106 $msgHandshake = array();
107 $msgHandshake['channel'] = '/meta/disconnect';
108 $msgHandshake['id'] = $this->nNextId++;
109 $msgHandshake['clientId'] = $this->clientId;
111 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
113 curl_exec($this->oCurl);
116 public function publish($sChannel, $oData)
118 if(!$sChannel || !$oData)
123 $aMsg['channel'] = $sChannel;
124 $aMsg['id'] = $this->nNextId++;
125 $aMsg['data'] = $oData;
126 $aMsg['clientId'] = $this->clientId;
128 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
130 $data = curl_exec($this->oCurl);