Advertisement
Guest User

Untitled

a guest
Apr 25th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. var message = $msg({to: 'whoever@whatever', from: 'whoever@whatever', type: "chat"}).c("body").t('message');
  2.  
  3. conn.send(message);
  4.  
  5. var conn = null;
  6.  
  7. var BOSH_SERVICE = 'MyBoshService';
  8.  
  9.  
  10. function register(){
  11. if(document.getElementById("username").value == "" || document.getElementById("password").value == "")
  12. {
  13. alert("Please enter both username and password");
  14. return;
  15. }
  16.  
  17. alert("Registro pulsado");
  18. conn = new Strophe.Connection(BOSH_SERVICE);
  19. conn.register.connect("myhost", onConnectionStatus, 60, 1);
  20. }
  21.  
  22.  
  23. function onConnectionStatus(nStatus){
  24. if(nStatus == Strophe.Status.ERROR)
  25. {
  26. alert("An error occured");
  27. }
  28. else if(nStatus == Strophe.Status.CONNECTED)
  29. {
  30. localStorage.setItem("username", "MYJID");
  31.  
  32. localStorage.setItem("password", 'mypass');
  33. connected();
  34. conn.send($pres());
  35. }
  36. else if(nStatus == Strophe.Status.AUTHFAIL)
  37. {
  38. alert("Username and password is wrong");
  39. }
  40.  
  41. else if(nStatus == Strophe.Status.REGISTER)
  42. {
  43. conn.register.fields.username = document.getElementById("username").value;
  44. conn.register.fields.password = document.getElementById("password").value;
  45. conn.register.submit();
  46. connected();
  47. }
  48. else if(nStatus == Strophe.Status.REGISTERED)
  49. {
  50. alert("Registration is successful");
  51. }
  52. else if(nStatus == Strophe.Status.REGIFAIL)
  53. {
  54. alert("Registration failed. Please try again later");
  55. }
  56. else if(nStatus == Strophe.Status.CONFLICT)
  57. {
  58. alert("Try some other username and password");
  59. }
  60. else if(nStatus == Strophe.Status.NOTACCEPTABLE)
  61. {
  62. alert("Try some other username and password");
  63. }
  64. else if (nStatus == Strophe.Status.DISCONNECTED)
  65. {
  66. var timer = setInterval(function(){
  67. if(conn.connected == false)
  68. {
  69. conn.connect(conn.jid, conn.pass, function(nStatus){
  70. if (nStatus == Strophe.Status.CONNECTED)
  71. {
  72. conn.send($pres());
  73. }
  74. });
  75. }
  76. else
  77. {
  78. clearInterval(timer);
  79. }
  80. }, 3000);
  81. }}
  82.  
  83.  
  84. function login(){
  85.  
  86. conn = new Strophe.Connection("MyBoshService");
  87. conn.connect('user','pass', onConnectionStatus);
  88.  
  89.  
  90. }
  91.  
  92.  
  93.  
  94. window.addEventListener("load",function(){
  95.  
  96. login();
  97.  
  98. }, false);
  99.  
  100. function logout(){
  101.  
  102. localStorage.removeItem("username");
  103. localStorage.removeItem("password");
  104. window.location = "login/index.html";
  105. }
  106.  
  107. //register handlers
  108. function connected(){
  109.  
  110. //get all the friends from friends list
  111. conn.roster.get(get_friends, 0);
  112.  
  113. //handle when someone sends us a friend requests
  114. conn.addHandler(subscribeStanza, null, "presence", "subscribe");
  115.  
  116. //when someone accepts our friend request
  117. conn.addHandler(subscribedStanza, null, "presence", "subscribed");
  118.  
  119. //someone who had earlier accepted your friend request now removed you from their contact list
  120. conn.addHandler(unsubscribeStanza, null, "presence", "unsubscribe");
  121.  
  122. //handle when user comes online
  123. conn.addHandler(status_changed_available, null, "presence");
  124.  
  125. //handle when user comes online
  126. conn.addHandler(status_changed_unavailable, null, "presence", "unavailable");
  127.  
  128. //handle when someone sends us a message
  129. conn.addHandler(recibir, null, "message",null,null,null);
  130.  
  131. //send your status to all other users.
  132. conn.send($pres());
  133.  
  134. }
  135.  
  136.  
  137. var nombre;
  138. var html_chat_name = "";
  139. var idchat = "";
  140.  
  141. function get_friends(items){
  142.  
  143.  
  144. if(items != undefined)
  145. {
  146. if (items.length != 0)
  147. {
  148. var html_friends_list = "";
  149.  
  150. for(var count = 0; count < items.length; count++)
  151. {
  152. if(items[count].subscription == "both")
  153. {
  154. var display_name = Strophe.getNodeFromJid(items[count].jid);
  155.  
  156.  
  157. 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>";
  158.  
  159.  
  160. }
  161. }
  162.  
  163. document.getElementById("friends-list").innerHTML = html_friends_list;
  164.  
  165.  
  166. var list = document.querySelectorAll('#friends-list li');
  167.  
  168. Array.prototype.slice.call(list).forEach(function(listItem){
  169. listItem.addEventListener('click', function(e){
  170.  
  171. e.preventDefault();
  172. nombre = this.textContent.slice(0,-1);
  173. html_chat_name = html_chat_name + nombre;
  174.  
  175. alert(this.id + ": " + nombre);
  176.  
  177.  
  178.  
  179. var valuesId = (this.id).split("-");
  180. idchat = valuesId[1];
  181.  
  182.  
  183. //GUARDAMOS EL VALOR DEL NOMBRE EN LOCAL PARA EXTRAERLO
  184. var local_nombre = html_chat_name.capitalize();
  185. localStorage.setItem("nombre", local_nombre);
  186. alert(local_nombre);
  187.  
  188.  
  189. //GUARDAMOS EL VALOR DEL JID QUE SERA CONCATENADO AL ID DEL INPUT DEL CHAT
  190. var jidpropio = idchat;
  191. localStorage.setItem("jid", idchat);
  192.  
  193.  
  194. });
  195. });
  196.  
  197. }
  198. }
  199. }
  200.  
  201.  
  202. Messenger.prototype.send = function send() {
  203. var text = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0];
  204.  
  205. text = this.filter(text);
  206.  
  207. if (this.validate(text)) {
  208. var message = {
  209. user: this.me,
  210. text: text,
  211. time: new Date().getTime()
  212. };
  213.  
  214. this.messageList.push(message);
  215.  
  216.  
  217.  
  218. }
  219. };
  220. */
  221.  
  222. function recibir(stanza) {
  223.  
  224. console.log('recibir');
  225. if(stanza.getAttribute('from')!==undefined){
  226.  
  227. var from = stanza.getAttribute('from');
  228. }
  229. if(stanza.getAttribute('type')!==undefined){
  230.  
  231. var type = stanza.getAttribute('type');
  232. }
  233.  
  234.  
  235.  
  236. var jid = Strophe.getBareJidFromJid(from);
  237. var body = stanza.getElementsByTagName('body')[0].innerHTML;
  238.  
  239. console.log(from);
  240. console.log(type);
  241. console.log(jid);
  242. console.log(body);
  243.  
  244. var message = $msg({to: 'myjid', from: jid, type: "chat"}).c("body").t(body);
  245.  
  246. console.log('recibir');
  247. console.log(body);
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement