Skip to content

Instantly share code, notes, and snippets.

@whoahbot
Created April 21, 2009 06:05
Show Gist options
  • Save whoahbot/98973 to your computer and use it in GitHub Desktop.
Save whoahbot/98973 to your computer and use it in GitHub Desktop.
$(function() {
$('.login_button').click(function() {
doLogin();
});
$('#send_chat_form').submit(function() {
var message = $(this).find('.message').val();
sendMsg(message);
$('#chat').append('<div class="msg">me: ' + message + '</div>');
$(this).find(".message").val('');
return false;
});
});
function handleMessage(aJSJaCPacket) {
$('#chat').append('<div class="msg">' + aJSJaCPacket.getFromJID() + ': ' + aJSJaCPacket.getBody().htmlEnc() + '</div>');
}
function handleError(e) {
$('#log').append("An error occured:<br />"+ ("Code: "+ e.getAttribute('code')+"\nType: " + e.getAttribute('type')+ "\nCondition: "+ e.firstChild.nodeName).htmlEnc());
if (con.connected()) {
con.disconnect();
}
}
function handleConnected() {
$('#login').hide();
$('#log').html('');
$('#send_chat').show();
var packet = new JSJaCPresence();
packet.setTo("#[email protected]/whoahbot");
packet.appendNode('x', {xmlns: "http://jabber.org/protocol/muc"});
con.send(packet);
}
function doLogin(form) {
$('#log').html('');
try {
// setup args for contructor
oArgs = new Object();
oArgs.httpbase = 'http://whoahbot.example.com/http-bind/';
oArgs.timerval = 2000;
if (typeof(oDbg) != 'undefined') {
oArgs.oDbg = oDbg;
}
con = new JSJaCHttpBindingConnection(oArgs);
setupCon(con);
// setup args for connect method
oArgs = new Object();
oArgs.domain = 'example.com';
oArgs.username = $('#username').val();
oArgs.resource = 'jsjac_simpleclient';
oArgs.pass = $('#password').val();
oArgs.register = false;
con.connect(oArgs);
} catch (e) {
$('#log').append(e.toString());
} finally {
return false;
}
}
function setupCon(con) {
con.registerHandler('message',handleMessage);
con.registerHandler('onconnect',handleConnected);
con.registerHandler('onerror',handleError);
}
function sendMsg(message) {
try {
var aMsg = new JSJaCMessage();
aMsg.setTo(new JSJaCJID("#[email protected]/whoahbot"));
aMsg.setType('groupchat');
aMsg.setBody(message);
con.send(aMsg);
} catch (e) {
$('#chat').html("<div class='msg error''>Error: "+ e.message +"</div>");
}
}
onerror = function(e) {
$('#log').append(e);
$('#door').show();
if (con && con.connected()) {
con.disconnect();
}
return false;
};
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang='en-us' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js' type='text/javascript'></script>
<script src='/javascript/jsjac.js' type='text/javascript'></script>
<script src='/javascript/chat.js' type='text/javascript'></script>
</head>
<body>
<div id='doc'>
<div id="login">
<ul>
<li class="errors"></li>
<li><label for="username">Username</label></li>
<li><input type="text" tabindex="1" name="username" id="username"/></li>
<li><label for="password">Password</label></li>
<li><input type="password" tabindex="2" name="password" id="password" /></li>
<li>&nbsp;</li>
<li><input type="submit" tabindex="3" class="login_button" value="Login"></li>
</ul>
</div>
<div id='chat'></div>
<div id='send_chat'>
<form method="POST" action="#" id="send_chat_form">
<input type="text" size="50" class="message" />
</form>
</div>
</div>
</body>
</html>
@KIOSENSEY
Copy link

sdsd

@ZezinJoestar
Copy link

aeee

@ZezinJoestar
Copy link

kkk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment