xmpp.js

changeset 14
aaad945d10ba
parent 12
1fe880c7383e
child 15
b328899c936a
equal deleted inserted replaced
13:917f370a7631 14:aaad945d10ba
16 // Wraps a function so that its 'this' is always 'context' when called 16 // Wraps a function so that its 'this' is always 'context' when called
17 var recontext = function (context, f) { return function () { return f.apply(context, arguments); }; }; 17 var recontext = function (context, f) { return function () { return f.apply(context, arguments); }; };
18 18
19 xmpp.xmlns = { 19 xmpp.xmlns = {
20 streams: "http://etherx.jabber.org/streams", 20 streams: "http://etherx.jabber.org/streams",
21 component_accept: "jabber:component:accept" 21 component_accept: "jabber:component:accept",
22 chatstates: "http://jabber.org/protocol/chatstates"
22 }; 23 };
23 24
24 xmpp.Status = { 25 xmpp.Status = {
25 ERROR: 0, 26 ERROR: 0,
26 CONNECTING: 1, 27 CONNECTING: 1,
59 attr["xmlns:"+namespaces[i][0]] = namespaces[i][1]; 60 attr["xmlns:"+namespaces[i][0]] = namespaces[i][1];
60 if(!stanza) 61 if(!stanza)
61 { 62 {
62 if(stream.opened) 63 if(stream.opened)
63 stanza = xmpp.stanza(tagname, attr); 64 stanza = xmpp.stanza(tagname, attr);
65 /* else if(tagname == "stream") */
64 else if(tagname == "stream" && uri == xmpp.xmlns.streams) 66 else if(tagname == "stream" && uri == xmpp.xmlns.streams)
65 { 67 {
66 stream.opened = true; 68 stream.opened = true;
67 callbacks.opened(attr); 69 callbacks.opened(attr);
68 } 70 }
113 xmpp.Connection = function (host, port) 115 xmpp.Connection = function (host, port)
114 { 116 {
115 this.host = host || "localhost"; 117 this.host = host || "localhost";
116 this.port = port || 5347; 118 this.port = port || 5347;
117 119
118 this.socket = tcp.createConnection(); 120 /** this.socket = tcp.createConnection();
121 this.socket.close() **/
119 122
120 this.stream = new xmpp.Stream({ 123 this.stream = new xmpp.Stream({
121 opened: recontext(this, this._stream_opened), 124 opened: recontext(this, this._stream_opened),
122 stanza: recontext(this, this._handle_stanza), 125 stanza: recontext(this, this._handle_stanza),
123 closed: recontext(this, this._stream_closed) 126 closed: recontext(this, this._stream_closed)
134 this.jid = jid; 137 this.jid = jid;
135 this.password = pass; 138 this.password = pass;
136 this.connect_callback = callback; 139 this.connect_callback = callback;
137 140
138 var conn = this; 141 var conn = this;
142 this.socket = tcp.createConnection(this.port, this.host)
139 this.socket.addListener("connect", recontext(this, conn._socket_connected)); 143 this.socket.addListener("connect", recontext(this, conn._socket_connected));
140 this.socket.addListener("disconnect", recontext(this, conn._socket_disconnected)); 144 this.socket.addListener("disconnect", recontext(this, conn._socket_disconnected));
141 this.socket.addListener("receive", recontext(this, conn._socket_received)); 145 this.socket.addListener("data", recontext(this, conn._socket_received));
142 146
143 this.handlers = []; 147 this.handlers = [];
144 148
145 // Connect TCP socket 149 // Connect TCP socket
146 this.socket.connect(this.port, this.host); 150 // this.socket.connect(this.port, this.host);
147 151
148 this._setStatus(xmpp.Status.CONNECTING); 152 this._setStatus(xmpp.Status.CONNECTING);
149 }, 153 },
150 154
151 send: function (data) 155 send: function (data)
152 { 156 {
153 this.debug("SND: "+data); 157 this.debug("SND: "+data);
154 this.socket.send(data.toString()); 158 this.socket.write(data.toString());
155 }, 159 },
156 160
157 sendIQ: function (iq, on_result, on_error) 161 sendIQ: function (iq, on_result, on_error)
158 { 162 {
159 if(!iq.attr.id) 163 if(!iq.attr.id)
221 { 225 {
222 this.debug("STREAM: opened."); 226 this.debug("STREAM: opened.");
223 this._setStatus(xmpp.Status.AUTHENTICATING); 227 this._setStatus(xmpp.Status.AUTHENTICATING);
224 var handshake = sha1.hex(attr.id + this.password); 228 var handshake = sha1.hex(attr.id + this.password);
225 this.debug("Sending authentication token..."); 229 this.debug("Sending authentication token...");
230 this.debug("with id: '"+attr.id+"' and pass: '"+this.password+"'")
226 this.send("<handshake>"+handshake+"</handshake>"); 231 this.send("<handshake>"+handshake+"</handshake>");
227 }, 232 },
228 233
229 _handle_stanza: function (stanza) 234 _handle_stanza: function (stanza)
230 { 235 {
307 this.last_node = [this]; 312 this.last_node = [this];
308 return this; 313 return this;
309 }; 314 };
310 315
311 xmpp.StanzaBuilder.prototype = { 316 xmpp.StanzaBuilder.prototype = {
317 s: function (name, attr)
318 {
319 var s = new xmpp.StanzaBuilder(name, attr);
320 var parent = this;
321 parent.tags.push(s);
322 parent.children.push(s);
323 this.last_node.push(s);
324 return this
325 },
326
312 c: function (name, attr) 327 c: function (name, attr)
313 { 328 {
314 var s = new xmpp.StanzaBuilder(name, attr); 329 var s = new xmpp.StanzaBuilder(name, attr);
315 var parent = this.last_node[this.last_node.length-1]; 330 var parent = this.last_node[this.last_node.length-1];
316 parent.tags.push(s); 331 parent.tags.push(s);

mercurial