Created
January 12, 2010 20:16
-
-
Save santiago/275560 to your computer and use it in GitHub Desktop.
This file contains 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
var BOSH_SERVICE = '/bosh/chat' | |
var connection = null; | |
var friend= ""; | |
var me= ""; | |
function removeNL(s){ | |
return s.replace(/[\n\r\t]/g," "); | |
} | |
function xmlInput(elem) { | |
$(elem).find("bind").find("jid").each(function() { | |
me= $(this).text(); | |
}); | |
$(elem).find("message").each(function() { | |
var msg= $(this).text(); | |
var _from= $(this).attr("from"); | |
var from= _from.split("/"); | |
var user= from[0]; | |
friend= _from; | |
var resource= from[1]; | |
if(msg!="") { | |
$("#chat-messages").append("<div class='message' id='msg-"+(++recvd_msg_id)+"'><p>"+user+" says...</p><p>"+msg+"</p></div>"); | |
} | |
}); | |
} | |
function onConnect(status) | |
{ | |
if (status == Strophe.Status.CONNECTING) { | |
} else if (status == Strophe.Status.CONNFAIL) { | |
$('#connect').get(0).value = 'connect'; | |
} else if (status == Strophe.Status.DISCONNECTING) { | |
} else if (status == Strophe.Status.DISCONNECTED) { | |
$("#chat-status-icon").attr("src","/images/icono-offline.png") | |
$("#chat-status-label").text("Desconectado"); | |
$('#connect').get(0).value = 'connect'; | |
} else if (status == Strophe.Status.CONNECTED) { | |
connection.send($pres().tree()); | |
connection.addHandler(onMessage, null, 'message', null, null, null); | |
connection.addHandler(onPresence, null, 'presence', null, null, null); | |
$("#chat-status-icon").attr("src","/images/icono-on-line.png") | |
$("#chat-status-label").text("Conectado"); | |
var msg = "quiubo pues!"; | |
var body= $build("body"); | |
body.t(removeNL(msg)); | |
var message= $msg({to:"santiago@junior-2.local", | |
from:"admin@junior-2.local", | |
type:"chat", | |
"xml:lang":"es"}).cnode(body.tree()); | |
// this message is sent immediately | |
connection.send(message.tree()); | |
} | |
} | |
function onPresence(msg) { | |
// do something | |
} | |
// for echobot | |
function onMessage(msg) { | |
var to = msg.getAttribute('to'); | |
var from = msg.getAttribute('from'); | |
var type = msg.getAttribute('type'); | |
var elems = msg.getElementsByTagName('body'); | |
if (type == "chat" && elems.length > 0) { | |
var body = elems[0]; | |
log('ECHOBOT: I got a message from ' + from + ': ' + | |
Strophe.getText(body)); | |
var reply = $msg({to: from, from: to, type: 'chat'}) | |
.cnode(Strophe.copyElement(body)); | |
// the message is sent immediately | |
connection.send(reply.tree()); | |
log('ECHOBOT: I sent ' + from + ': ' + Strophe.getText(body)); | |
} | |
// we must return true to keep the handler alive. | |
// returning false would remove it after it finishes. | |
return true; | |
} | |
function send_msg() { | |
var msg = $("#chat-input").val(); | |
var message= $msg({to:friend, | |
from:me, | |
type:"chat", | |
"xml:lang":"es"}); | |
var body= $build("body"); | |
body.t(removeNL(msg)); | |
var reply = $msg({to: friend, from: me, type: 'chat'}) | |
.cnode(body.tree()); | |
// message deliver is delayed until | |
// the 60 sec request is finished | |
connection.send(reply.tree()); | |
} | |
$(document).ready(function () { | |
connection= new Strophe.Connection(BOSH_SERVICE); | |
connection.xmlInput= xmlInput; | |
connection.xmlOutput= xmlOutput; | |
$("#chat-window button").click(send_msg); | |
$('#connect').bind('click', function () { | |
var button = $('#connect').get(0); | |
if (button.value == 'connect') { | |
button.value = 'disconnect'; | |
connection.connect($('#jid').get(0).value, | |
$('#pass').get(0).value, | |
onConnect); | |
} else { | |
button.value = 'connect'; | |
connection.disconnect(); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment