$(document).ready(function(){
$("#frm_login").submit(function(e){
e.preventDefault();
try{
connection.connect($('#jid').get(0).value,
$('#pass').get(0).value,
onConnect);
//connection.addHandler(onMessage, null, "message", "chat");
}
catch(e)
{
log("can't stablish connection, Check Ejabberd Server is running")
}
})
$(document).bind('connected', function () {
$("#frm_messagebox").submit(function(e){
e.preventDefault();
try{
var msg=$msg({"to":$('#tobox').get(0).value,"type":"chat"}).c("body").t($('#messagebox').get(0).value)
connection.send(msg)
$("#chatmessage").append("<div class='fromuser msg'><div>"+$('#messagebox').get(0).value+"</div></div>")
$('#messagebox').get(0).value=""
//connection.addHandler(onMessage, null, "message", "chat");
}
catch(e)
{
log("can't stablish connection, Check Ejabberd Server is running")
}
})
// inform the user
var domain = Strophe.getDomainFromJid(connection.jid);
send_ping(domain);
});
})
var BOSH_SERVICE = 'ws://server.com:5280/websocket';
var connection = null;
function log(msg)
{
$('#log').append('<div></div>').append(document.createTextNode(msg));
}
function rawInput(data)
{
log('RECV: ' + data);
}
function rawOutput(data)
{
log('SENT: ' + data);
}
function onConnect(status)
{
if (status == Strophe.Status.CONNECTING) {
log('Strophe is connecting.');
} else if (status == Strophe.Status.CONNFAIL) {
log('Strophe failed to connect.');
$('#connect').get(0).value = 'connect';
} else if (status == Strophe.Status.DISCONNECTING) {
log('Strophe is disconnecting.');
} else if (status == Strophe.Status.DISCONNECTED) {
log('Strophe is disconnected.');
$('#connect').get(0).value = 'connect';
} else if (status == Strophe.Status.CONNECTED) {
$(document).trigger('connected');
log('Strophe is connected.');
var pres=$pres({type: "avalible"})
connection.send(pres);
//<presence type=’unavailable’/>
//var cur_msg=$msg({to: "ali@server.com", type: "chat"}).c("body").t("How do you do?");
//connection.send(cur_msg)
//vcard_iq= $iq({to: "admin@server.com", type: "set", id: "91"}).c("vCard",{"xmlns":"vcard-temp"}).c("NICKNAME").t("rrr").up().c("FN").t("hhh")
//connection.send(vcard_iq)
//connection.disconnect();
}
}
function onMessage(msgXML)
{
var to = msgXML.getAttribute('to');
var from = msgXML.getAttribute('from');
var fromBareJid = Strophe.getBareJidFromJid(from);
var type = msgXML.getAttribute('type');
var elems = msgXML.getElementsByTagName('body');
var body = elems[0]
var text = Strophe.getText(body);
$("#chatmessage").append("<div class='fromother msg'><div><span class='otherid'> "+Strophe.getNodeFromJid(from)+"  </span>  "+ text+" </div></div>")
return true;
}
function send_ping(to){
var ping = $iq({
to: to,
type: 'get',
id: 'ping1'}).c('ping', {xmlns: 'urn:xmpp:ping'});
connection.send(ping);
}
connection = new Strophe.Connection(BOSH_SERVICE, {protocol: "wss"});
connection.rawInput = rawInput;
connection.rawOutput = rawOutput;
connection.addHandler(onMessage, null, 'message', "chat", null, null,
{matchBare: true} );
//connection.addHandler(onMessage, null, "message", null, null, null);
//connection.addHandler(onMessage, null, 'message', 'chat')//or 'normal'