Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

sample packaged chat client now supports two-way communication betwee…

…n mutually subscribed buddies. refactored packet ingestion to auto parse name, jid, client and domain
  • Loading branch information...
commit 4c3f2382334655b1309b940843e485976023764e 1 parent 0a210e0
Sean Soper authored November 05, 2008
79  example/chat-client/index.html
@@ -22,6 +22,7 @@
22 22
   session.callbacks.subscription.request = subscriptionRequest;
23 23
   session.callbacks.subscription.confirmation = subscriptionConfirmation;
24 24
   session.callbacks.roster.updated = updateRoster;
  25
+  session.callbacks.message.received = messageReceived;
25 26
   session.open();
26 27
 }
27 28
 
@@ -50,23 +51,23 @@
50 51
 }
51 52
 
52 53
 function subscriptionRequest( requests ) {
53  
-  $(requests).each(function(k, v) {
54  
-    var confirmed = confirm(v.name + ' wants to add you as a friend. Approve?');
  54
+  $(session.queues.subscription.requests).each(function(k, v) {
  55
+    var confirmed = confirm(v.from.name + ' wants to add you as a friend. Approve?');
55 56
     if (confirmed) {
56  
-      session.approveSubscription(v.jid)
57  
-      session.requestSubscription(v.jid)
  57
+      session.approveSubscription(v.from.jid)
  58
+      session.requestSubscription(v.from.jid)
58 59
     }
59 60
   });
60 61
 }
61 62
 
62  
-function subscriptionConfirmation( confirmations ) {
  63
+function subscriptionConfirmation() {
63 64
   var confirmed = '';
64  
-  $(confirmations).each(function(k, v) {
  65
+  $(session.queues.subscription.confirmations).each(function(k, v) {
65 66
     confirmed += v.name + '\n';
66 67
   });
67 68
 
68  
-  if (confirmations.length > 1)
69  
-    alert('Your friend requests to the following users was approved: ' + '\n');
  69
+  if (session.queues.subscription.confirmations.length > 1)
  70
+    alert('Your friend requests to the following users was approved: \n' + confirmed);
70 71
   else
71 72
     alert('Your friend request to ' + confirmations[0].name + ' was approved');
72 73
 }
@@ -74,20 +75,20 @@
74 75
 function updateRoster() {
75 76
   if (!session.connected) return;
76 77
 
77  
-  // Clear chat window
  78
+  // Clear roster
78 79
   $('#roster #list ul').empty();
79 80
 
80 81
   // Add buddies to roster
81 82
   $(session.roster.items).each(function(k, v) {
82 83
     var html;
83 84
     if (v.available) 
84  
-      html = $.bosh.tagBuilder('li', { jid: v.jid }, $.bosh.tagBuilder('a', { class: 'available', href: '#' }, v.name));
  85
+      html = $.bosh.tagBuilder('li', { jid: v.from.jid }, $.bosh.tagBuilder('a', { class: 'available', href: '#' }, v.from.name));
85 86
     else 
86  
-      html = $.bosh.tagBuilder('li', { jid: v.jid }, $.bosh.tagBuilder('a', { class: 'unavailable', href: '#' }, v.name));
  87
+      html = $.bosh.tagBuilder('li', { jid: v.from.jid }, $.bosh.tagBuilder('a', { class: 'unavailable', href: '#' }, v.from.name));
87 88
     $('#roster #list ul').append(html);
88 89
   });
89 90
 
90  
-  // Styles to control how a name looks when single-clicked on
  91
+  // Control how a name looks when single-clicked on
91 92
   $('#roster #list ul > li').click(function() {
92 93
     deHighlight();
93 94
     $(this).css({ backgroundColor: 'blue' });
@@ -111,7 +112,7 @@
111 112
 }
112 113
 
113 114
 function createNewChatWindow( jid ) {
114  
-  var chatWindowId = jid.split('@').join('-');
  115
+  var chatWindowId = domizeJid(jid)
115 116
   $('#template.chat-window').attr('id', chatWindowId).clone();
116 117
   $('#' + chatWindowId).show().children('textarea').focus();
117 118
   
@@ -119,52 +120,34 @@
119 120
   $('#' + chatWindowId + ' div.send input').bind('click', function() {
120 121
     var message = $.trim($(this).parents('.chat-window').children('textarea:first').val());
121 122
     var recipient = $(this).parents('.chat-window').attr('id').split('-')[0];
  123
+    $(this).parents('.chat-window').children('.outgoing:first')[0].value = "";
  124
+    addText($(this), { to: session.username, message: message });
122 125
     session.sendMessage(recipient, message);
123 126
   });
124 127
 }
125  
-/*
126  
-var users = [{ name: 'alice', password: 'secret', div: 'left' },
127  
-               { name: 'bob', password: 'secret', div: 'right' }]
128 128
 
129  
-var sessions = [];
  129
+function domizeJid( jid ) {
  130
+  return jid.split('@').join('-');
  131
+}
  132
+
  133
+function messageReceived() {
  134
+  jQuery(session.queues.messages).each(function(k, v) {
  135
+    var chatWindow = domizeJid(v.from.jid);
  136
+    if ($('#' + chatWindow).length == 0) createNewChatWindow(v.from.jid);
  137
+    console.log($('#' + chatWindow)[0]);
  138
+    addText($('#' + chatWindow), { from: v.from.name, message: v.message });
  139
+  });
  140
+}
130 141
 
131  
-function addText(element, settings) {
  142
+function addText(jqElement, settings) {
132 143
   if (settings['to'] == null)
133 144
     html = "<p class='message'><span class='from'>" + settings['from'] + ":</span> ";
134 145
   else
135 146
     html = "<p class='message'><span class='to'>" + settings['to'] + ":</span> ";
136 147
   html += settings['message'] + "</p>";
137  
-  element.children(".incoming").append(html);
  148
+  element = jqElement.hasClass('chat-window') ? jqElement.children(".incoming") : jqElement.parents('.chat-window').children(".incoming");
  149
+  element.append(html);
138 150
 }
139  
-
140  
-$(document).ready(function() {
141  
-  // Create sessions
142  
-  $(users).each(function(k, v) {
143  
-    sessions[k] = new $.bosh.Session(host, v.name, v.password);
144  
-    sessions[k].open();
145  
-    var this_div = $("#" + users[k].div);
146  
-
147  
-    // Set message received callback function
148  
-    sessions[k].cbMsgRecvd = function(messages) {
149  
-      $(messages).each(function(k, v) {
150  
-        addText(this_div, { to: v.from, message: v.message });
151  
-      });
152  
-    }
153  
-  });
154  
-
155  
-  // Attach event to button
156  
-  $('#container > div').each(function(k, v) {
157  
-    $(v).find('div.send > input[type="button"]').bind('click', function() {
158  
-      var recipient = $(v).children('input[type="hidden"]:first').val();
159  
-      var message = $.trim($(v).children('textarea:first').val());
160  
-      if (message == "") return;
161  
-      sessions[k].sendMessage(recipient, message);
162  
-      $(v).children('textarea:first')[0].value = "";
163  
-      addText($(v), { from: users[k].name, message: message });
164  
-    });
165  
-  });
166  
-});
167  
-*/
168 151
 </script>
169 152
 </head>
170 153
 <body>
3  example/chat-client/styles.css
@@ -96,6 +96,7 @@ div.header {
96 96
   height: 250px;
97 97
   float: left;
98 98
   text-align: left;
  99
+  font-size: 10pt;
99 100
   margin: 0 20px;
100 101
   background-color: #999;
101 102
   margin-top: 20px;
@@ -130,7 +131,7 @@ div.header {
130 131
   border: black solid thin;
131 132
   background-color: white;
132 133
   overflow: auto;
133  
-  font-size: 12pt;
  134
+  font-size: 10pt; /* textarea does not auto-inherit from chat-window class */
134 135
   font-family: Verdana,Helvetica,sans-serif;
135 136
 }
136 137
 
6  example/one-window/index.html
@@ -29,9 +29,9 @@
29 29
     var this_div = $("#" + users[k].div);
30 30
 
31 31
     // Set message received callback function
32  
-    sessions[k].callbacks.message.received = function(messages) {
33  
-      $(messages).each(function(k, v) {
34  
-        addText(this_div, { to: v.from, message: v.message });
  32
+    sessions[k].callbacks.message.received = function() {
  33
+      $(sessions[k].queues.messages).each(function(k, v) {
  34
+        addText(this_div, { to: v.from.name, message: v.message });
35 35
       });
36 36
     }
37 37
   });
56  jquery.bosh.js
@@ -77,16 +77,33 @@ jQuery.bosh = jQuery.extend({
77 77
     });
78 78
   },
79 79
 
  80
+  Sender: function ( packet ) {
  81
+    this.jid = null;
  82
+    this.domain = null;
  83
+    this.name = null;
  84
+    this.client = null;
  85
+    
  86
+    var sender = packet.getAttribute('from') || packet.getAttribute('jid');
  87
+    
  88
+    if (sender) {
  89
+      if (sender.split("@").length > 1) {      
  90
+        var from = sender.split("/")[0];
  91
+        this.jid = from;
  92
+        this.name = from.split('@')[0];
  93
+        this.domain = from.split('@')[1];
  94
+      }
  95
+      if (sender.split("/").length > 1)
  96
+        this.client = sender.split("/")[1];
  97
+    }
  98
+  },
  99
+
80 100
 	Message: function( packet ) {
81 101
 		this.from = null;
82 102
 		this.message = null;
83 103
 		this.timestamp = null;
84 104
     this.raw = packet;
85 105
 
86  
-		if (!packet) return;
87  
-
88  
-		if (packet.getAttribute('from') && packet.getAttribute('from').split("@").length > 1)
89  
-			this.from = packet.getAttribute('from').split("@")[0];
  106
+		this.from = new jQuery.bosh.Sender(packet);
90 107
 
91 108
 		if (jQuery('body', packet).length > 0)
92 109
       this.message = jQuery.trim(jQuery('body', packet).text());
@@ -104,9 +121,7 @@ jQuery.bosh = jQuery.extend({
104 121
 	},
105 122
 
106 123
   Presence: function( packet ) {
107  
-    this.jid = packet.getAttribute('jid') || packet.getAttribute('from');
108  
-    this.jid = this.jid.split('/')[0];  // JID sometimes contains Jabber client 'user@domain/client'
109  
-    this.name = this.jid.split('@')[0];
  124
+    this.from = new jQuery.bosh.Sender(packet);
110 125
     this.available = packet.getAttribute('type') == 'unavailable' ? false : true;
111 126
   },
112 127
 
@@ -116,7 +131,7 @@ jQuery.bosh = jQuery.extend({
116 131
     this.find = function( presence ) {
117 132
       var result = null;
118 133
       jQuery(this.items).each(function(k, v) {
119  
-        if (presence.jid == v.jid) result = k;
  134
+        if (presence.from.jid == v.from.jid) result = k;
120 135
       });
121 136
       return result;
122 137
     };
@@ -197,6 +212,7 @@ jQuery.bosh = jQuery.extend({
197 212
 		this.route = 'xmpp:' + this.domain + ':' + jQuery.bosh.settings.port;
198 213
 		this.username = username;
199 214
 		this.password = password;
  215
+		this.jid = this.username + '@' + this.domain;
200 216
 
201 217
 		this.rid = jQuery.bosh.generateRid();
202 218
 		this.lastResponse = null;
@@ -241,25 +257,16 @@ jQuery.bosh = jQuery.extend({
241 257
 				self.queues.messages.push(new jQuery.bosh.Message(v));
242 258
 			});
243 259
 
244  
-      if (self.callbacks.message.received) self.callbacks.message.received(self.queues.messages);
  260
+      if (self.callbacks.message.received) self.callbacks.message.received();
245 261
 		};
246 262
 
247  
-    this.ingestPresences = function( self, data ) {
248  
-      jQuery('presence', data).each(function(k, v) {
249  
-        var username = v.getAttribute('from').split('@')[0]
250  
-        if (username != self.username) self.roster.push(new jQuery.bosh.Presence(v));
251  
-      });
252  
-      
253  
-      if (self.callbacks.roster.updated) self.callbacks.roster.updated();
254  
-    };
255  
-
256 263
     this.ingestSubscriptionRequests = function( self, data ) {
257 264
       self.queues.subscription.requests = [];
258 265
       jQuery('presence[type="subscribe"]', data).each(function(k, v) {
259 266
         self.queues.subscription.requests.push(new jQuery.bosh.Presence(v));
260 267
       });
261 268
 
262  
-      if (self.callbacks.subscription.request) self.callbacks.subscription.request(self.queues.subscription.requests);
  269
+      if (self.callbacks.subscription.request) self.callbacks.subscription.request();
263 270
     };
264 271
 
265 272
     this.ingestSubscriptionConfirmations = function( self, data ) {
@@ -268,7 +275,16 @@ jQuery.bosh = jQuery.extend({
268 275
         self.queues.subscription.confirmations.push(new jQuery.bosh.Presence(v));
269 276
       });
270 277
 
271  
-      if (self.callbacks.subscription.confirmation) self.callbacks.subscription.confirmation(self.queues.subscription.confirmations);
  278
+      if (self.callbacks.subscription.confirmation) self.callbacks.subscription.confirmation();
  279
+    };
  280
+
  281
+    this.ingestPresences = function( self, data ) {
  282
+      jQuery('presence', data).each(function(k, v) {
  283
+        var username = v.getAttribute('from').split('@')[0]
  284
+        if (username != self.username) self.roster.push(new jQuery.bosh.Presence(v));
  285
+      });
  286
+      
  287
+      if (self.callbacks.roster.updated) self.callbacks.roster.updated();
272 288
     };
273 289
 
274 290
     this.listen = function() {

0 notes on commit 4c3f238

Please sign in to comment.
Something went wrong with that request. Please try again.