Skip to content

Instantly share code, notes, and snippets.

@frne
Created February 16, 2012 13:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save frne/1844785 to your computer and use it in GitHub Desktop.
Save frne/1844785 to your computer and use it in GitHub Desktop.
ejabberd, BOSH und Strophe.js

#Strophe.js Strophe.js is a JavaScript library for speaking XMPP via BOSH (XEP 124 and 206). It is licensed under the MIT license, except for the files sha1.js, base64.js and md5.js, which are licensed as public domain and BSD (see these files for details).

It has been tested on Firefox 1.5, 2.x, and 3.x, IE 6, 7, and 8, Safari, Mobile Safari, Chrome, and it should also work on the mobile Opera browser as well as the desktop Opera browser.

The homepage for Strophe is http://strophe.im/strophejs

The book Professional XMPP Programming with JavaScript and jQuery is also available, which covers Strophe in detail in the context of web applications. You can find more information at http://professionalxmpp.com.

#More links XMPP over BOSH


http://xmpp.org/extensions/xep-0206.html

<body xmlns="http://jabber.org/protocol/httpbind"
sid="e0408bf8d86f0a7f44afda59aa8f81b75d029bb6"
wait="60"
requests="2"
inactivity="30"
maxpause="120"
polling="2"
ver="1.6"
from="ymc-pg-ejabberd"
secure="true"
authid="3533709236"
xmpp="urn:xmpp:xbosh"
stream="http://etherx.jabber.org/streams"
version="1.0">
<body xmlns="http://jabber.org/protocol/httpbind">
<message xmlns="jabber:client"
from="zambo@conference.ymc-pg-ejabberd/frank"
to="frank@ymc-pg-ejabberd/17046541991329220505919269"
type="groupchat"
id="2719">
<body xmlns="jabber:client">Hi Jörg. Hast du das gekriegt?</body>
<x xmlns="jabber:x:event">
<composing />
</x>
<x xmlns="jabber:x:delay"
stamp="20120214T11:20:48" />
</message>
</body>
<body xmlns="http://jabber.org/protocol/httpbind">
<presence xmlns="jabber:client"
from="zambo@conference.ymc-pg-ejabberd/admin"
to="frank@ymc-pg-ejabberd/17046541991329220505919269">
<priority>1</priority>
<c xmlns="http://jabber.org/protocol/caps"
node="http://pidgin.im/"
hash="sha-1"
ver="lV6i//bt2U8Rm0REcX8h4F3Nk3M="
ext="voice-v1 camera-v1 video-v1" />
<x xmlns="vcard-temp:x:update">
<photo />
</x>
<x xmlns="http://jabber.org/protocol/muc#user">
<item affiliation="owner"
role="moderator" />
</x>
</presence>
</body>
$(document).ready(function () {
var Chat = function () {
var self = this;
this.connection = false;
this.jid = false;
this.BOSH_SERVICE = 'http://ymc-pg-ejabberd:5280/http-bind';
//this.BOSH_SERVICE = 'http://k1-s52:5280/http-bind';
this.init = function () {
$(".loginButton").button();
$(".logoutButton").button();
$(".msgButton").button();
self.connection = new Strophe.Connection(this.BOSH_SERVICE);
self.connection.xmlInput = this.log;
self.connection.xmlOutput = this.log;
$('#disconnect').bind('click', function (event) {
event.preventDefault();
self.connection.disconnect();
});
$('#connect').bind('click', function (event) {
event.preventDefault();
var button = $('#connect').get(0);
if (button.value == 'connect') {
button.value = 'disconnect';
self.jid = $('#jid').get(0).value;
self.connection.connect(
$('#jid').get(0).value,
$('#pass').get(0).value,
self.events.onConnect
);
} else {
button.value = 'connect';
self.connection.disconnect();
}
});
$("#messageForm").bind("submit", function (event) {
event.preventDefault();
console.log("Result: " + $("#message").val() );
window.chat.groupchat.message($("#message").val());
$("#message").val("");
return false;
});
};
this.out = function (message) {
$('#log').append('<br />').append(
message
);
};
this.log = function( message ) {
console.log( message );
};
this.events = {
"onConnect": function (status) {
if (status == Strophe.Status.CONNECTING) {
$('#log').html('');
self.out('Connecting chat server...');
} else if (status == Strophe.Status.CONNFAIL) {
self.out('Failed to connect.');
$('#connect').get(0).value = 'connect';
} else if (status == Strophe.Status.DISCONNECTING) {
self.out('Disconnecting from chat server...');
} else if (status == Strophe.Status.DISCONNECTED) {
self.out('Disconnected.');
$('#connect').get(0).value = 'connect';
$('#messageForm').hide();
$('#loginForm').show();
} else if (status == Strophe.Status.CONNECTED) {
self.out('Connected.');
self.out('ECHOBOT: Send a message to ' + self.connection.jid +
' to talk to me.');
self.connection.addHandler(function (msg) {
return self.events.onMessage( msg );
}, null, 'message', null, null, null);
self.connection.send($pres().tree());
$('#loginForm').hide();
$('#messageForm').show();
self.groupchat.join();
}
},
"onMessage": function( msg ) {
if(jQuery(msg).attr("type") == "chat") {
self.out( "<strong>" + jQuery(msg).attr("from") + ": </strong>" + jQuery(msg).find("body:first").text() );
}
return true;
}
};
this.sendMessage = function( recipient, message ) {
var reply = $msg({to: recipient, type: "chat"})
.c("body")
.t(message);
console.log("SENDING MESSAGE: " + message);
self.connection.send(reply.tree());
self.out( "<strong>" + self.connection.jid + ": </strong>" + message );
};
this.groupchat = {
"_chatRoomId": "Zambo@conference.ymc-pg-ejabberd",
"_chatRoomNick": function() {
var ret = self.jid.split("@");
return ret [0];
},
"join":function () {
try {
self.connection.muc.join(
this._chatRoomId,
this._chatRoomNick(),
this.incomingMessageHandler,
this.groupPresenceHandler,
null
);
self.out("<strong>" + this._chatRoomNick() + "</strong> ist dem Raum <strong><italic>" + this._chatRoomId + "</italic></strong> beigetreten!");
} catch (e) {
self.out("<span style='color: red;'>Beitritt zum Raum <strong>" + this._chatRoomId + "</strong> momentan leider nicht möglich!</span>");
console.error(e);
}
},
"message":function (msg) {
try {
self.connection.muc.groupchat(
this._chatRoomId,
msg,
null
);
} catch (e) {
self.out("<span style='color: red;'>Nachricht konnte nicht gesenedet werden!</span>");
$.error(e);
}
},
"incomingMessageHandler": function ( msg ) {
console.log("MESSAGE HANDLE");
console.log( msg );
if(jQuery(msg).attr("type") == "chat") {
self.out( "<strong>" + jQuery(msg).attr("from") + ": </strong>" + jQuery(msg).find("body:first").text() );
}
},
"groupPresenceHandler": function ( presence ) {
console.log("PRESENCE HANDLE");
console.log(presence);
}
};
// initialisation
this.init();
};
window.chat = new Chat();
});
@ravindrarjpt9
Copy link

Could you please send working code with html page , where i can form .

@harshpandyaec
Copy link

could you please provide the group chat working code with HTML and JS , thanks.

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