|
$(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": "[email protected]", |
|
"_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(); |
|
}); |
Could you please send working code with html page , where i can form .