Created
June 30, 2012 00:13
-
-
Save robinsingh1/3021536 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Whats Up | |
(function(){ | |
var p = PUBNUB | |
, speech = p.$('text-to-speech') | |
, tts = p.$('tts') | |
, tts_tpl = p.attr( tts, 'template' ) | |
, speeches = p.$('speeches_template') | |
, speech_tpl = p.attr( speeches, 'template' ) | |
, loading = p.$('loading-speech') | |
, unavailable = p.$('unavailable') | |
, channel = 'text-to-speech-demo' | |
, user_data = {} | |
, safe_rx = /[<>\r\n]+/g | |
, safe = function(t) { return t.replace( safe_rx, '' ) } | |
, app_id = location.href.indexOf('localhost') != -1 ? | |
'373033892077' : '206485666051439' | |
, uuid = p.uuid(function(id) { | |
uuid = id; | |
p.css( loading, { display : 'none' } ); | |
} ); | |
/* ================================================================ */ | |
/* =============== RECEIVE VOICE TRANSMISSION =============== */ | |
/* ================================================================ */ | |
function receive( message, noaudio ) { | |
var text = safe(message.text) | |
, fbid = ''+(message.fbid || 128998223833309); | |
// Display Message | |
speeches.innerHTML = | |
p.supplant( speech_tpl, { | |
color : fbid.slice( 6, 9 ), | |
text : text, | |
fbid : fbid, | |
name : message.name || 'PubNub' | |
} ) + speeches.innerHTML; | |
if (noaudio === 1) return; | |
// Do Voice Audio | |
var voice = p.create('div'); | |
voice.innerHTML = p.supplant( | |
tts_tpl, | |
{ text : text.replace( /\s+/g, '+' ) } | |
); | |
tts.appendChild(voice); | |
console.log(voice.innerHTML); | |
} | |
p.subscribe({ | |
channel : channel, | |
callback : receive | |
}); | |
p.history({ | |
channel : channel, | |
limit : 20, | |
callback : function(messages) { | |
p.each( messages, function(msg) { receive( msg, 1 ) } ); | |
} | |
}); | |
if(!('webkitSpeech' in p.create('input'))) | |
return p.css( unavailable, { display : 'block' } ); | |
p.bind( 'webkitspeechchange', speech, function() { | |
p.publish({ | |
channel : channel, | |
message : { | |
uuid : uuid, | |
fbid : user_data.fbid, | |
name : user_data.name, | |
text : speech.value | |
} | |
}); | |
speech.value = ''; | |
} ); | |
/* ================================================================ */ | |
/* ==================== FACEBOOK CONNECT ==================== */ | |
/* ================================================================ */ | |
FB.init({ | |
appId : app_id, | |
status : true, | |
cookie : true, | |
xfbml : false | |
}); | |
p.bind( 'click', speech, function() { | |
if (user_data.fbid) return true; | |
FB.login(); | |
return true; | |
} ); | |
function fb_login(response) { | |
if (fb_login.ready) return; | |
if (!response.session) return; | |
// Save User ID | |
user_data.fbid = response.session.uid; | |
// Get Extended User Info | |
FB.api( '/' + user_data.fbid, function(info) { | |
user_data.name = info.name; | |
} ); | |
fb_login.ready = 1; | |
user_data.logged_in = true; | |
} | |
function fb_logout() { | |
fb_login.ready = 0; | |
user_data.logged_in = false; | |
user_data.friends = false; | |
user_data.name = 'Anonymous'; | |
FB.logout(); | |
} | |
FB.Event.subscribe( 'auth.sessionChange', fb_login ); | |
FB.getLoginStatus(fb_login); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment