Bayeux.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * Phomet: a php comet client
  4. *
  5. * Copyright (C) 2008 Morgan 'ARR!' Allen <morganrallen@gmail.com> http://morglog.alleycatracing.com
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. *
  21. */
  22. class Bayeux
  23. {
  24. private $oCurl = '';
  25. private $nNextId = 0;
  26. private $sUser = '';
  27. private $sPassword = '';
  28. public $sUrl = '';
  29. public function __construct($sUrl, $sUser='', $sPassword='')
  30. {
  31. $this->sUrl = $sUrl;
  32. $this->oCurl = curl_init();
  33. $aHeaders = [];
  34. $aHeaders[] = 'Connection: Keep-Alive';
  35. curl_setopt($this->oCurl, CURLOPT_URL, $sUrl);
  36. curl_setopt($this->oCurl, CURLOPT_HTTPHEADER, $aHeaders);
  37. curl_setopt($this->oCurl, CURLOPT_HEADER, 0);
  38. curl_setopt($this->oCurl, CURLOPT_POST, 1);
  39. curl_setopt($this->oCurl, CURLOPT_RETURNTRANSFER, 1);
  40. if (!is_null($sUser) && mb_strlen($sUser) > 0) {
  41. curl_setopt($this->oCurl, CURLOPT_USERPWD, "$sUser:$sPassword");
  42. }
  43. $this->handShake();
  44. }
  45. public function __destruct()
  46. {
  47. $this->disconnect();
  48. }
  49. public function handShake()
  50. {
  51. $msgHandshake = [];
  52. $msgHandshake['channel'] = '/meta/handshake';
  53. $msgHandshake['version'] = "1.0";
  54. $msgHandshake['minimumVersion'] = "0.9";
  55. $msgHandshake['supportedConnectionTypes'] = array('long-polling');
  56. $msgHandshake['id'] = $this->nNextId++;
  57. curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
  58. $data = curl_exec($this->oCurl);
  59. if (curl_errno($this->oCurl)) {
  60. die("Error: " . curl_error($this->oCurl));
  61. }
  62. $oReturn = json_decode($data);
  63. if (is_array($oReturn)) {
  64. $oReturn = $oReturn[0];
  65. }
  66. $bSuccessful = ($oReturn->successful) ? true : false;
  67. if ($bSuccessful) {
  68. $this->clientId = $oReturn->clientId;
  69. $this->connect();
  70. }
  71. }
  72. public function connect()
  73. {
  74. $aMsg['channel'] = '/meta/connect';
  75. $aMsg['id'] = $this->nNextId++;
  76. $aMsg['clientId'] = $this->clientId;
  77. $aMsg['connectionType'] = 'long-polling';
  78. curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
  79. $data = curl_exec($this->oCurl);
  80. }
  81. public function disconnect()
  82. {
  83. $msgHandshake = [];
  84. $msgHandshake['channel'] = '/meta/disconnect';
  85. $msgHandshake['id'] = $this->nNextId++;
  86. $msgHandshake['clientId'] = $this->clientId;
  87. curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
  88. curl_exec($this->oCurl);
  89. }
  90. public function publish($sChannel, $oData)
  91. {
  92. if (!$sChannel || !$oData) {
  93. return;
  94. }
  95. $aMsg = [];
  96. $aMsg['channel'] = $sChannel;
  97. $aMsg['id'] = $this->nNextId++;
  98. $aMsg['data'] = $oData;
  99. $aMsg['clientId'] = $this->clientId;
  100. curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
  101. $data = curl_exec($this->oCurl);
  102. // var_dump($data);
  103. }
  104. }