Comparison

mod_tcpproxy/web/xmpp.io.js @ 148:f2f9b965d1ad

mod_tcpproxy: Add web/ folder containing demo JS client
author Matthew Wild <mwild1@gmail.com>
date Tue, 13 Apr 2010 04:52:15 +0100
child 1343:7dbde05b48a9
comparison
equal deleted inserted replaced
147:4db80a46b064 148:f2f9b965d1ad
1 var xmlns_ibb = "http://jabber.org/protocol/ibb";
2 var xmlns_tcp = "http://prosody.im/protocol/tcpproxy";
3
4 function XMPPIO(xmppconn, xmpptcp_host)
5 {
6 this.xmppconn = xmppconn;
7 this.xmpphost = xmpptcp_host;
8 this.sid = "FIXME";
9
10 this.listeners = [];
11 return this;
12 }
13
14 XMPPIO.prototype = {
15 connect: function (host, port)
16 {
17 var conn = this;
18 console.log("Connecting...");
19
20 function onConnect()
21 {
22 this.xmppconn.addHandler(function (stanza)
23 {
24 var data = stanza.getElementsByTagName("data")[0];
25 if(data)
26 conn.emit("data", Strophe.Base64.decode(Strophe.getText(data)));
27 }, null, "message", null, null, this.xmpphost, {});
28 this.xmppconn.addHandler(function (stanza)
29 {
30 var data = stanza.getElementsByTagName("close")[0];
31 if(close)
32 {
33 conn.write = function () { throw "Connection closed"; };
34 conn.emit("end");
35 }
36 }, xmlns_ibb, "iq", "set", null, this.xmpphost, {});
37 conn.emit("connect");
38 }
39
40 this.xmppconn.sendIQ($iq({to:this.xmpphost,type:"set"})
41 .c("open", {
42 "xmlns": xmlns_ibb,
43 "xmlns:tcp": xmlns_tcp,
44 "tcp:host": host,
45 "tcp:port": port.toString(),
46 "block-size": "4096",
47 "sid": this.sid.toString(),
48 "stanza": "message"
49 }), onConnect,
50 function () { conn.emit("error"); });
51 },
52 emit: function ()
53 {
54 console.log("xmpp.io: Emitting "+arguments[0]);
55 var args = Array.prototype.slice.call(arguments, 1);
56 var listeners = this.listeners[arguments[0]];
57 if(listeners)
58 {
59 for(var i=0;i<listeners.length;i++)
60 {
61 listeners[i][1].apply(listeners[i][0], args);
62 }
63 }
64 },
65 addListener: function (event, method, obj)
66 {
67 if(typeof(obj)=="undefined")
68 obj = this;
69 if(!(event in this.listeners))
70 this.listeners[event] = [];
71 this.listeners[event].push([obj, method]);
72 },
73 write: function (data)
74 {
75 return this.xmppconn.send($msg({to:this.xmpphost})
76 .c("data", {xmlns:xmlns_ibb, sid:this.sid.toString()})
77 .t(Strophe.Base64.encode(data)));
78 },
79 end: function ()
80 {
81 return this.xmppconn.send($iq({to:this.xmpphost})
82 .c("close", {xmlns:xmlns_ibb, sid:this.sid.toString()}));
83 }
84 };