[Pkg-javascript-commits] [ltx] 122/469: component: switch to new code + echo_component example

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:01:12 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository ltx.

commit 5c83a31c513daa0e649675c3c3d1ccd72d57d03b
Author: Astro <astro at spaceboyz.net>
Date:   Thu Sep 9 03:46:14 2010 +0200

    component: switch to new code + echo_component example
---
 examples/echo_component.js | 42 ++++++++++++++++++++++++++++++++++++++++++
 lib/xmpp/component.js      | 28 ++++++++++++++--------------
 2 files changed, 56 insertions(+), 14 deletions(-)

diff --git a/examples/echo_component.js b/examples/echo_component.js
new file mode 100644
index 0000000..b40f288
--- /dev/null
+++ b/examples/echo_component.js
@@ -0,0 +1,42 @@
+/**
+ * Echo Component - the XMPP Hello World
+ **/
+var sys = require('sys');
+var xmpp = require('../lib/xmpp');
+var argv = process.argv;
+
+if (argv.length != 6) {
+    sys.puts('Usage: node echo_bot.js <my-jid> <my-password> <host> <port>');
+    process.exit(1);
+}
+
+var cl = new xmpp.Component({ jid: argv[2],
+			      password: argv[3],
+			      host: argv[4],
+			      port: argv[5] });
+cl.on('online',
+      function() {
+	  cl.send(new xmpp.Element('presence',
+				   { type: 'chat'}).
+		  c('show').t('chat').up().
+		  c('status').t('Happily echoing your <message/> stanzas')
+		 );
+      });
+cl.on('stanza',
+      function(stanza) {
+	  if (stanza.is('message') &&
+	      // Important: never reply to errors!
+	      stanza.attrs.type !== 'error') {
+
+	      // Swap addresses...
+	      var me = stanza.attrs.to;
+	      stanza.attrs.to = stanza.attrs.from;
+	      stanza.attrs.from = me;
+	      // and send back.
+	      cl.send(stanza);
+	  }
+      });
+cl.on('error',
+      function(e) {
+	  sys.puts(e);
+      });
diff --git a/lib/xmpp/component.js b/lib/xmpp/component.js
index 7e3b1f4..771b596 100644
--- a/lib/xmpp/component.js
+++ b/lib/xmpp/component.js
@@ -3,6 +3,7 @@ var JID = require('./jid').JID;
 var xml = require('./xml');
 var sys = require('sys');
 var crypto = require('crypto');
+var SRV = require('./srv');
 
 var NS_COMPONENT = 'jabber:component:accept';
 
@@ -14,6 +15,7 @@ var NS_COMPONENT = 'jabber:component:accept';
  *   port: Number (required)
  */
 function Component(params) {
+    var self = this;
     Connection.Connection.call(this);
 
     if (typeof params.jid == 'string')
@@ -24,21 +26,21 @@ function Component(params) {
     this.xmlns = NS_COMPONENT;
     this.streamTo = this.jid.domain;
     // Immediately start stream
-    this.addListener('connect', this.startStream);
-    this.addListener('streamStart', this.onStreamStart);
-    this.addListener('rawStanza', this.onRawStanza);
-    this.addListener('end', this.onEnd);
-
-    this.connect(params.port, params.host);
+    this.socket.addListener('connect', function() {
+	self.startStream();
+    });
+    this.addListener('streamStart', function(streamAttrs) {
+	self.onStreamStart(streamAttrs);
+    });
+    this.addListener('rawStanza', function(stanza) {
+	self.onRawStanza(stanza);
+    });
+    SRV.connect(this.socket, [], params.host, params.port);
 }
 
 sys.inherits(Component, Connection.Connection);
 exports.Component = Component;
 
-Component.prototype.onEnd = function() {
-    this.authenticated = false;
-};
-
 Component.prototype.onStreamStart = function(streamAttrs) {
     var digest = sha1_hex(streamAttrs.id + this.password);
     this.send(new xml.Element('handshake').t(digest));
@@ -57,11 +59,9 @@ Component.prototype.startStream = function() {
 };
 
 Component.prototype.onRawStanza = function(stanza) {
-    if (!this.authenticated &&
-	stanza.is('handshake', NS_COMPONENT)) {
-	this.authenticated = true;
+    if (stanza.is('handshake', NS_COMPONENT)) {
 	this.emit('online');
-    } else if (this.authenticated) {
+    } else {
 	this.emit('stanza', stanza);
     }
 };

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git



More information about the Pkg-javascript-commits mailing list