[Pkg-javascript-commits] [ltx] 62/469: connection: move stream starting to client & component

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:01:01 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 5437b43f7f1d680198919f7efa693ea03bd48454
Author: Astro <astro at spaceboyz.net>
Date:   Sat Sep 4 19:47:06 2010 +0200

    connection: move stream starting to client & component
---
 lib/xmpp/client.js     | 14 ++++++++++++++
 lib/xmpp/component.js  | 14 ++++++++++++++
 lib/xmpp/connection.js | 51 +++++++++++++++++++++++++++++++++-----------------
 3 files changed, 62 insertions(+), 17 deletions(-)

diff --git a/lib/xmpp/client.js b/lib/xmpp/client.js
index bf2cf69..525eb25 100644
--- a/lib/xmpp/client.js
+++ b/lib/xmpp/client.js
@@ -39,6 +39,8 @@ function Client(params) {
     this.xmppVersion = "1.0";
     this.streamTo = this.jid.domain;
     this.state = STATE_PREAUTH;
+    // Immediately start stream
+    this.addListener('connect', this.startStream);
     this.addListener('rawStanza', this.onRawStanza);
 
     if (params.host) {
@@ -74,6 +76,18 @@ function Client(params) {
 sys.inherits(Client, Connection.Connection);
 exports.Client = Client;
 
+Client.prototype.startStream = function() {
+    Connection.prototype.startStream.call(this);
+
+    var tag = "<stream:stream xmlns='" + this.xmlns +
+	"' xmlns:stream='" + Connection.NS_STREAM + "'" +
+	" to='" + this.streamTo + "'";
+    if (this.xmppVersion)
+	tag += " version='" + this.xmppVersion + "'";
+    tag += ">";
+    this.send(tag);
+};
+
 Client.prototype.onRawStanza = function(stanza) {
     /* Actually, we shouldn't wait for <stream:features/> if
        this.streamAttrs.version is missing, but who uses pre-XMPP-1.0
diff --git a/lib/xmpp/component.js b/lib/xmpp/component.js
index 76baf53..a422606 100644
--- a/lib/xmpp/component.js
+++ b/lib/xmpp/component.js
@@ -23,6 +23,8 @@ function Component(params) {
     this.password = params.password;
     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);
@@ -42,6 +44,18 @@ Component.prototype.onStreamStart = function(streamAttrs) {
     this.send(new xml.Element('handshake').t(digest));
 };
 
+Component.prototype.startStream = function() {
+    Connection.prototype.startStream.call(this);
+
+    var tag = "<stream:stream xmlns='" + this.xmlns +
+	"' xmlns:stream='" + Connection.NS_STREAM + "'" +
+	" to='" + this.streamTo + "'";
+    if (this.xmppVersion)
+	tag += " version='" + this.xmppVersion + "'";
+    tag += ">";
+    this.send(tag);
+};
+
 Component.prototype.onRawStanza = function(stanza) {
     if (!this.authenticated &&
 	stanza.is('handshake', NS_COMPONENT)) {
diff --git a/lib/xmpp/connection.js b/lib/xmpp/connection.js
index 74ca92b..aed4ac8 100644
--- a/lib/xmpp/connection.js
+++ b/lib/xmpp/connection.js
@@ -13,17 +13,40 @@ var NS_STREAM = exports.NS_STREAM = 'http://etherx.jabber.org/streams';
 function Connection() {
     net.Stream.call(this);
 
-    this.charset = 'UTF-8';
-    this.allowTLS = true;  /* can be set by user */
-    this.addListener('connect', this.startStream);
-    this.addListener('data', this.onData);
-//    this.addListener('end', this.onEnd);
-    this.addListener('error', this.onError);
+    initConnection(this);
 }
 
 sys.inherits(Connection, net.Stream);
 exports.Connection = Connection;
 
+// Defaults
+Connection.prototype.charset = 'UTF-8';
+Connection.prototype.allowTLS = true;
+
+
+/** Constructor code, usable for existing streams
+ */
+function makeConnection(conn) {
+    for(var k in Connection.prototype)
+	if (Connection.prototype.hasOwnProperty(k))
+	    conn[k] = Connection.prototype[k];
+
+    initConnection(conn);
+}
+exports.makeConnection = makeConnection;
+
+/** Actual constructor code
+ */
+function initConnection(conn) {
+    conn.charset = 'UTF-8';
+
+    conn.addListener('data', conn.onData);
+    conn.addListener('close', conn.onClose);
+}
+
+/** Climbs the stanza up if a child was passed,
+    but you can send strings and buffers too.
+*/
 Connection.prototype.send = function(stanza) {
     if (!this.writable) {
 	this.end();
@@ -44,6 +67,7 @@ Connection.prototype.send = function(stanza) {
 
 Connection.prototype.startParser = function() {
     var self = this;
+    self.setEncoding('utf8');
     self.element = null;
     self.parser = new expat.Parser(self.charset);
 
@@ -96,14 +120,6 @@ Connection.prototype.startParser = function() {
 
 Connection.prototype.startStream = function() {
     this.startParser();
-
-    var tag = "<stream:stream xmlns='" + this.xmlns +
-	"' xmlns:stream='" + NS_STREAM + "'" +
-	" to='" + this.streamTo + "'";
-    if (this.xmppVersion)
-	tag += " version='" + this.xmppVersion + "'";
-    tag += ">";
-    this.send(tag);
 };
 
 Connection.prototype.onData = function(data) {
@@ -164,8 +180,9 @@ Connection.prototype.rmStreamNs = function(stanza) {
 
 
 /**
- * All errors are terminal for the connection.
+ * 
  */
-Connection.prototype.onError = function(error) {
-    this.end();
+Connection.prototype.onClose = function() {
+    delete this.element;
+    delete this.parser;
 };

-- 
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