1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
<?php
/*
*
* Phomet: a php comet client
*
* Copyright (C) 2008 Morgan 'ARR!' Allen <morganrallen@gmail.com> http://morglog.alleycatracing.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
class Bayeux
{
private $oCurl = '';
private $nNextId = 0;
public $sUrl = '';
function __construct($sUrl)
{
$this->sUrl = $sUrl;
$this->oCurl = curl_init();
$aHeaders = array();
$aHeaders[] = 'Connection: Keep-Alive';
curl_setopt($this->oCurl, CURLOPT_URL, $sUrl);
curl_setopt($this->oCurl, CURLOPT_HTTPHEADER, $aHeaders);
curl_setopt($this->oCurl, CURLOPT_HEADER, 0);
curl_setopt($this->oCurl, CURLOPT_POST, 1);
curl_setopt($this->oCurl, CURLOPT_RETURNTRANSFER,1);
$this->handShake();
}
function __destruct()
{
$this->disconnect();
}
function handShake()
{
$msgHandshake = array();
$msgHandshake['channel'] = '/meta/handshake';
$msgHandshake['version'] = "1.0";
$msgHandshake['minimumVersion'] = "0.9";
$msgHandshake['supportedConnectionTypes'] = array('long-polling');
$msgHandshake['id'] = $this->nNextId++;
curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
$data = curl_exec($this->oCurl);
if(curl_errno($this->oCurl))
die("Error: " . curl_error($this->oCurl));
$oReturn = json_decode($data);
common_debug(print_r($oReturn, true));
if (is_array($oReturn)) {
$oReturn = $oReturn[0];
}
$bSuccessful = ($oReturn->successful) ? true : false;
if($bSuccessful)
{
$this->clientId = $oReturn->clientId;
$this->connect();
}
}
public function connect()
{
$aMsg['channel'] = '/meta/connect';
$aMsg['id'] = $this->nNextId++;
$aMsg['clientId'] = $this->clientId;
$aMsg['connectionType'] = 'long-polling';
curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
$data = curl_exec($this->oCurl);
}
function disconnect()
{
$msgHandshake = array();
$msgHandshake['channel'] = '/meta/disconnect';
$msgHandshake['id'] = $this->nNextId++;
$msgHandshake['clientId'] = $this->clientId;
curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
curl_exec($this->oCurl);
}
public function publish($sChannel, $oData)
{
if(!$sChannel || !$oData)
return;
$aMsg = array();
$aMsg['channel'] = $sChannel;
$aMsg['id'] = $this->nNextId++;
$aMsg['data'] = $oData;
$aMsg['clientId'] = $this->clientId;
curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
$data = curl_exec($this->oCurl);
// var_dump($data);
}
}
|