]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Comet/bayeux.class.inc.php
602a7b6446cb6b034f0e3d2640df15f72a8b8f4e
[quix0rs-gnu-social.git] / plugins / Comet / bayeux.class.inc.php
1 <?php
2 /*
3  *
4  * Phomet: a php comet client
5  *
6  * Copyright (C) 2008 Morgan 'ARR!' Allen <morganrallen@gmail.com> http://morglog.alleycatracing.com
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 class Bayeux
25 {
26     private $oCurl = '';
27     private $nNextId = 0;
28
29     public $sUrl = '';
30
31     function __construct($sUrl)
32     {
33         $this->sUrl = $sUrl;
34
35         $this->oCurl = curl_init();
36
37         $aHeaders = array();
38         $aHeaders[] = 'Connection: Keep-Alive';
39
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);
45
46         $this->handShake();
47     }
48
49     function __destruct()
50     {
51         $this->disconnect();
52     }
53
54     function handShake()
55     {
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++;
62
63         curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
64
65         $data = curl_exec($this->oCurl);
66
67         if(curl_errno($this->oCurl))
68           die("Error: " . curl_error($this->oCurl));
69
70         $oReturn = json_decode($data);
71
72         common_debug(print_r($oReturn, true));
73
74         if (is_array($oReturn)) {
75             $oReturn = $oReturn[0];
76         }
77
78         $bSuccessful = ($oReturn->successful) ? true : false;
79
80         if($bSuccessful)
81         {
82             $this->clientId = $oReturn->clientId;
83
84             $this->connect();
85         }
86     }
87
88     public function connect()
89     {
90         $aMsg['channel'] = '/meta/connect';
91         $aMsg['id'] = $this->nNextId++;
92         $aMsg['clientId'] = $this->clientId;
93         $aMsg['connectionType'] = 'long-polling';
94
95         curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
96
97         $data = curl_exec($this->oCurl);
98     }
99
100     function disconnect()
101     {
102         $msgHandshake = array();
103         $msgHandshake['channel'] = '/meta/disconnect';
104         $msgHandshake['id'] = $this->nNextId++;
105         $msgHandshake['clientId'] = $this->clientId;
106
107         curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
108
109         curl_exec($this->oCurl);
110     }
111
112     public function publish($sChannel, $oData)
113     {
114         if(!$sChannel || !$oData)
115           return;
116
117         $aMsg = array();
118
119         $aMsg['channel'] = $sChannel;
120         $aMsg['id'] = $this->nNextId++;
121         $aMsg['data'] = $oData;
122         $aMsg['clientId'] = $this->clientId;
123
124         curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
125
126         $data = curl_exec($this->oCurl);
127 //        var_dump($data);
128     }
129 }