4 * Phomet: a php comet client
6 * Copyright (C) 2008 Morgan 'ARR!' Allen <morganrallen@gmail.com> http://morglog.alleycatracing.com
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 private $sPassword = '';
34 function __construct($sUrl, $sUser='', $sPassword='')
38 $this->oCurl = curl_init();
41 $aHeaders[] = 'Connection: Keep-Alive';
43 curl_setopt($this->oCurl, CURLOPT_URL, $sUrl);
44 curl_setopt($this->oCurl, CURLOPT_HTTPHEADER, $aHeaders);
45 curl_setopt($this->oCurl, CURLOPT_HEADER, 0);
46 curl_setopt($this->oCurl, CURLOPT_POST, 1);
47 curl_setopt($this->oCurl, CURLOPT_RETURNTRANSFER,1);
49 if (!is_null($sUser) && mb_strlen($sUser) > 0) {
50 curl_setopt($this->oCurl, CURLOPT_USERPWD,"$sUser:$sPassword");
63 $msgHandshake = array();
64 $msgHandshake['channel'] = '/meta/handshake';
65 $msgHandshake['version'] = "1.0";
66 $msgHandshake['minimumVersion'] = "0.9";
67 $msgHandshake['supportedConnectionTypes'] = array('long-polling');
68 $msgHandshake['id'] = $this->nNextId++;
70 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
72 $data = curl_exec($this->oCurl);
74 if(curl_errno($this->oCurl))
75 die("Error: " . curl_error($this->oCurl));
77 $oReturn = json_decode($data);
79 if (is_array($oReturn)) {
80 $oReturn = $oReturn[0];
83 $bSuccessful = ($oReturn->successful) ? true : false;
87 $this->clientId = $oReturn->clientId;
93 public function connect()
95 $aMsg['channel'] = '/meta/connect';
96 $aMsg['id'] = $this->nNextId++;
97 $aMsg['clientId'] = $this->clientId;
98 $aMsg['connectionType'] = 'long-polling';
100 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
102 $data = curl_exec($this->oCurl);
105 function disconnect()
107 $msgHandshake = array();
108 $msgHandshake['channel'] = '/meta/disconnect';
109 $msgHandshake['id'] = $this->nNextId++;
110 $msgHandshake['clientId'] = $this->clientId;
112 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
114 curl_exec($this->oCurl);
117 public function publish($sChannel, $oData)
119 if(!$sChannel || !$oData)
124 $aMsg['channel'] = $sChannel;
125 $aMsg['id'] = $this->nNextId++;
126 $aMsg['data'] = $oData;
127 $aMsg['clientId'] = $this->clientId;
129 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
131 $data = curl_exec($this->oCurl);