Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created December 17, 2010 19:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mheadd/745515 to your computer and use it in GitHub Desktop.
Save mheadd/745515 to your computer and use it in GitHub Desktop.
Tropo / Bayeux Example for real time web connections
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bayeux / Comet Test for Tropo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript" src="http://path/to/your/bayeaux/server/tropo.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var client = new Faye.Client('http://path/to/your/bayeaux/server/tropo');
var subscription = client.subscribe('/dtmf/relay', function(message) {
console.log(message.text);
$("#keys").append("<p>User entered DTMF: " + message.text + "</p>");
});
});
</script>
</head>
<body>
<h2>User selections</h2>
<div id="keys"></div>
</body>
</html>
var faye = require('faye'), sys = require('sys'),
http = require('http'), port = process.ARGV[2] || '8000';
var bayeux = new faye.NodeAdapter({ mount: '/tropo' });
var server = http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Move along. Nothing to see here.');
response.end();
});
bayeux.attach(server);
sys.puts('Server listening on port ' + port + "...");
server.listen(port);
<?php
answer();
say("Welcome to the Bayeaux my friend.");
$bayeau = new Bayeux("http://path/to/your/bayeaux/server/tropo");
do {
$result = ask("Press any key to send a message. Press 0 to end this demo.", array("choices" => "[1 DIGITS]"));
_log("*** User entered: ".$result->value." ***");
$bayeau->publish("/dtmf/relay", array("text" => $result->value));
} while ($result->value != 0);
hangup();
// A class to send messages using the Bayeau protocol.
// Adapted from http://morglog.org/?p=22
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['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);
$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);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment