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
31 function __construct($sUrl)
35 $this->oCurl = curl_init();
38 $aHeaders[] = 'Connection: Keep-Alive';
40 curl_setopt($this->oCurl, CURLOPT_URL, $sUrl);
41 curl_setopt($this->oCurl, CURLOPT_HTTPHEADER, $aHeaders);
42 curl_setopt($this->oCurl, CURLOPT_HEADER, 0);
43 curl_setopt($this->oCurl, CURLOPT_POST, 1);
44 curl_setopt($this->oCurl, CURLOPT_RETURNTRANSFER,1);
56 $msgHandshake = array();
57 $msgHandshake['channel'] = '/meta/handshake';
58 $msgHandshake['version'] = "1.0";
59 $msgHandshake['minimumVersion'] = "0.9";
60 $msgHandshake['supportedConnectionTypes'] = array('long-polling');
61 $msgHandshake['id'] = $this->nNextId++;
63 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
65 $data = curl_exec($this->oCurl);
67 if(curl_errno($this->oCurl))
68 die("Error: " . curl_error($this->oCurl));
70 $oReturn = json_decode($data);
72 common_debug(print_r($oReturn, true));
74 if (is_array($oReturn)) {
75 $oReturn = $oReturn[0];
78 $bSuccessful = ($oReturn->successful) ? true : false;
82 $this->clientId = $oReturn->clientId;
88 public function connect()
90 $aMsg['channel'] = '/meta/connect';
91 $aMsg['id'] = $this->nNextId++;
92 $aMsg['clientId'] = $this->clientId;
93 $aMsg['connectionType'] = 'long-polling';
95 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
97 $data = curl_exec($this->oCurl);
100 function disconnect()
102 $msgHandshake = array();
103 $msgHandshake['channel'] = '/meta/disconnect';
104 $msgHandshake['id'] = $this->nNextId++;
105 $msgHandshake['clientId'] = $this->clientId;
107 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
109 curl_exec($this->oCurl);
112 public function publish($sChannel, $oData)
114 if(!$sChannel || !$oData)
119 $aMsg['channel'] = $sChannel;
120 $aMsg['id'] = $this->nNextId++;
121 $aMsg['data'] = $oData;
122 $aMsg['clientId'] = $this->clientId;
124 curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
126 $data = curl_exec($this->oCurl);