Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var message = $msg({to: 'whoever@whatever', from: 'whoever@whatever', type: "chat"}).c("body").t('message');
- conn.send(message);
- var conn = null;
- var BOSH_SERVICE = 'MyBoshService';
- function register(){
- if(document.getElementById("username").value == "" || document.getElementById("password").value == "")
- {
- alert("Please enter both username and password");
- return;
- }
- alert("Registro pulsado");
- conn = new Strophe.Connection(BOSH_SERVICE);
- conn.register.connect("myhost", onConnectionStatus, 60, 1);
- }
- function onConnectionStatus(nStatus){
- if(nStatus == Strophe.Status.ERROR)
- {
- alert("An error occured");
- }
- else if(nStatus == Strophe.Status.CONNECTED)
- {
- localStorage.setItem("username", "MYJID");
- localStorage.setItem("password", 'mypass');
- connected();
- conn.send($pres());
- }
- else if(nStatus == Strophe.Status.AUTHFAIL)
- {
- alert("Username and password is wrong");
- }
- else if(nStatus == Strophe.Status.REGISTER)
- {
- conn.register.fields.username = document.getElementById("username").value;
- conn.register.fields.password = document.getElementById("password").value;
- conn.register.submit();
- connected();
- }
- else if(nStatus == Strophe.Status.REGISTERED)
- {
- alert("Registration is successful");
- }
- else if(nStatus == Strophe.Status.REGIFAIL)
- {
- alert("Registration failed. Please try again later");
- }
- else if(nStatus == Strophe.Status.CONFLICT)
- {
- alert("Try some other username and password");
- }
- else if(nStatus == Strophe.Status.NOTACCEPTABLE)
- {
- alert("Try some other username and password");
- }
- else if (nStatus == Strophe.Status.DISCONNECTED)
- {
- var timer = setInterval(function(){
- if(conn.connected == false)
- {
- conn.connect(conn.jid, conn.pass, function(nStatus){
- if (nStatus == Strophe.Status.CONNECTED)
- {
- conn.send($pres());
- }
- });
- }
- else
- {
- clearInterval(timer);
- }
- }, 3000);
- }}
- function login(){
- conn = new Strophe.Connection("MyBoshService");
- conn.connect('user','pass', onConnectionStatus);
- }
- window.addEventListener("load",function(){
- login();
- }, false);
- function logout(){
- localStorage.removeItem("username");
- localStorage.removeItem("password");
- window.location = "login/index.html";
- }
- //register handlers
- function connected(){
- //get all the friends from friends list
- conn.roster.get(get_friends, 0);
- //handle when someone sends us a friend requests
- conn.addHandler(subscribeStanza, null, "presence", "subscribe");
- //when someone accepts our friend request
- conn.addHandler(subscribedStanza, null, "presence", "subscribed");
- //someone who had earlier accepted your friend request now removed you from their contact list
- conn.addHandler(unsubscribeStanza, null, "presence", "unsubscribe");
- //handle when user comes online
- conn.addHandler(status_changed_available, null, "presence");
- //handle when user comes online
- conn.addHandler(status_changed_unavailable, null, "presence", "unavailable");
- //handle when someone sends us a message
- conn.addHandler(recibir, null, "message",null,null,null);
- //send your status to all other users.
- conn.send($pres());
- }
- var nombre;
- var html_chat_name = "";
- var idchat = "";
- function get_friends(items){
- if(items != undefined)
- {
- if (items.length != 0)
- {
- var html_friends_list = "";
- for(var count = 0; count < items.length; count++)
- {
- if(items[count].subscription == "both")
- {
- var display_name = Strophe.getNodeFromJid(items[count].jid);
- html_friends_list = html_friends_list + "<li style='font-size:19px' id='open_chat-" + items[count].jid + "'>" + "<a href='chat-js/index.html'>" + display_name + "<span class='block-list-label' id='" + items[count].jid + "_unread_messages" + "'>0</span><span class='block-list-label' id='" + items[count].jid + "_change_status" + "'></span></a></li>";
- }
- }
- document.getElementById("friends-list").innerHTML = html_friends_list;
- var list = document.querySelectorAll('#friends-list li');
- Array.prototype.slice.call(list).forEach(function(listItem){
- listItem.addEventListener('click', function(e){
- e.preventDefault();
- nombre = this.textContent.slice(0,-1);
- html_chat_name = html_chat_name + nombre;
- alert(this.id + ": " + nombre);
- var valuesId = (this.id).split("-");
- idchat = valuesId[1];
- //GUARDAMOS EL VALOR DEL NOMBRE EN LOCAL PARA EXTRAERLO
- var local_nombre = html_chat_name.capitalize();
- localStorage.setItem("nombre", local_nombre);
- alert(local_nombre);
- //GUARDAMOS EL VALOR DEL JID QUE SERA CONCATENADO AL ID DEL INPUT DEL CHAT
- var jidpropio = idchat;
- localStorage.setItem("jid", idchat);
- });
- });
- }
- }
- }
- Messenger.prototype.send = function send() {
- var text = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0];
- text = this.filter(text);
- if (this.validate(text)) {
- var message = {
- user: this.me,
- text: text,
- time: new Date().getTime()
- };
- this.messageList.push(message);
- }
- };
- */
- function recibir(stanza) {
- console.log('recibir');
- if(stanza.getAttribute('from')!==undefined){
- var from = stanza.getAttribute('from');
- }
- if(stanza.getAttribute('type')!==undefined){
- var type = stanza.getAttribute('type');
- }
- var jid = Strophe.getBareJidFromJid(from);
- var body = stanza.getElementsByTagName('body')[0].innerHTML;
- console.log(from);
- console.log(type);
- console.log(jid);
- console.log(body);
- var message = $msg({to: 'myjid', from: jid, type: "chat"}).c("body").t(body);
- console.log('recibir');
- console.log(body);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement