From 04d403b90c00714265e6c76388d1dbda38cc30eb Mon Sep 17 00:00:00 2001 From: Nikos M Date: Fri, 13 May 2016 05:13:49 +0300 Subject: [PATCH] v.1.0.0 * node test chat client * minor fixes and typos --- src/RT.js | 10 ++++- src/RT.min..js | 2 +- test/chat/chat.js | 111 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 test/chat/chat.js diff --git a/src/RT.js b/src/RT.js index 541bd14..1729180 100644 --- a/src/RT.js +++ b/src/RT.js @@ -72,9 +72,10 @@ RT.XHR = function XHR( send, abort ){ if ( XHR.DONE !== xhr.readyState ) return null; return true===decoded ? xhr._headers : xhr._rawHeaders; }; - xhr.getResponseHeader = function( key ) { + xhr.getResponseHeader = function( key, lowercased ) { if ( (null == key) || (XHR.DONE !== xhr.readyState) ) return null; var headers = xhr._headers || {}; + if ( false !== lowercased ) key = key.toLowerCase( ); return headers[HAS](key) ? headers[key] : null; }; xhr.dispose = function( ) { @@ -209,9 +210,14 @@ RT.XHR.create = isXPCOM if ( null != payload ) { payload = String( payload ); - $hr$.setHeader( 'Content-Length', payload.length.toString() ); + $hr$.setHeader( 'Content-Length', String(payload.length) ); $hr$.write( payload ); } + /*else + { + $hr$.setHeader( 'Content-Length', '0' ); + $hr$.write( '' ); + }*/ $hr$.end( ); }, function( ) { diff --git a/src/RT.min..js b/src/RT.min..js index cfc6d21..2c2ad0a 100644 --- a/src/RT.min..js +++ b/src/RT.min..js @@ -6,4 +6,4 @@ * https://github.com/foo123/RT * **/ -!function(e,t,n){"use strict";"undefined"!=typeof Components&&"object"==typeof Components.classes&&"object"==typeof Components.classesByID&&Components.utils&&"function"==typeof Components.utils["import"]?(e.EXPORTED_SYMBOLS=[t])&&(e[t]=n()):"object"==typeof exports?module.exports=n():(e[t]=n())&&"function"==typeof define&&define.amd&&define(function(){return e[t]})}(this,"RT",function(){"use strict";function e(t){t=t||{};var r=(t.use||t.rt_type||"default").toLowerCase();return e.Client.Impl[n](r)?new e.Client.Impl[r](t):new e.Client(t)}var t="prototype",n="hasOwnProperty",r=Object.keys,o=Object[t].toString,s="undefined"!=typeof Components&&"object"==typeof Components.classes&&"object"==typeof Components.classesByID&&Components.utils&&"function"==typeof Components.utils["import"],i="undefined"!=typeof global&&"[object global]"===o.call(global),a=String.fromCharCode,u=/^\s+|\s+$/g,l=String[t].trim?function(e){return e.trim()}:function(e){return e.replace(u,"")},c=/[^A-Za-z0-9\+\/\=]/g,d=/\x0d\x0a/g,p=0;return e.VERSION="1.0.0",e.Platform={XPCOM:s,Node:i},e.XHR=function f(e,t){var r=this,o=!1;r.readyState=f.UNSENT,r.status=null,r.statusText=null,r.responseType="text",r.responseURL=null,r.response=null,r.responseText=null,r.responseXml=null,r._rawHeaders=null,r._headers=null,r.send=function(t){return o||f.UNSENT!==r.readyState?r:(e&&e(t),r.readyState=f.OPENED,r)},r.abort=function(){return o?r:(o=!0,t&&t(),r)},r.getAllResponseHeaders=function(e){return f.DONE!==r.readyState?null:!0===e?r._headers:r._rawHeaders},r.getResponseHeader=function(e){if(null==e||f.DONE!==r.readyState)return null;var t=r._headers||{};return t[n](e)?t[e]:null},r.dispose=function(){return r.readyState=null,r.status=null,r.statusText=null,r.responseType=null,r.responseURL=null,r.response=null,r.responseText=null,r.responseXml=null,r._rawHeaders=null,r._headers=null,r.getAllResponseHeaders=null,r.getResponseHeader=null,r.send=null,r.abort=null,r}},e.XHR.UNSENT=0,e.XHR.OPENED=1,e.XHR.HEADERS_RECEIVED=2,e.XHR.LOADING=3,e.XHR.DONE=4,e.XHR.create=s?function(t,n){if(t=t||{},!t.url)return null;var r=Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(),o=new e.XHR(function(e){r.send(e)},function(){r.abort()}),s=function(e,t){return e.readyState=t.readyState,e.responseType=t.responseType,e.responseURL=t.responseURL,e.response=t.response,e.responseText=t.responseText,e.responseXml=t.responseXml,e.status=t.status,e.statusText=t.statusText,e};return o.getAllResponseHeaders=function(t){var n=r.getAllResponseHeaders();return!0===t?e.Util.Header.decode(n):n},o.getResponseHeader=function(e){return r.getResponseHeader(e)},r.open(t.method||"GET",t.url,!t.sync),o.responseType=r.responseType=t.responseType||"text",r.timeout=t.timeout||3e4,t.onProgress&&r.addEventListener("progress",function(){t.onProgress(s(o,r))}),t.onLoadStart&&r.addEventListener("loadstart",function(){t.onLoadStart(s(o,r))}),!t.sync&&t.onStateChange&&r.addEventListener("readystatechange",function(){t.onStateChange(s(o,r))}),r.addEventListener("load",function(){s(o,r),e.XHR.DONE===r.readyState&&(200===r.status?t.onComplete&&t.onComplete(o):t.onRequestError?t.onRequestError(o):t.onError&&t.onError(o))}),r.addEventListener("abort",function(){t.onAbort&&t.onAbort(s(o,r))}),r.addEventListener("error",function(){t.onError&&t.onError(s(o,r))}),r.addEventListener("timeout",function(){t.onTimeout&&t.onTimeout(s(o,r))}),t.headers&&e.Util.Header.encode(t.headers,r),t.mimeType&&r.overrideMimeType(t.mimeType),arguments.length>1&&o.send(n),o}:i?function(t,n){if(t=t||{},!t.url)return null;var r,s,i="[object Object]"===o.call(t.url)?t.url:require("url").parse(t.url),a={method:t.method||"GET",agent:!1,protocol:i.protocol,host:i.hostname,hostname:i.hostname,port:i.port||80,path:(i.pathname||"/")+(i.query?"?"+i.query:"")};return s=new e.XHR(function(e){null!=e&&(e=String(e),r.setHeader("Content-Length",e.length.toString()),r.write(e)),r.end()},function(){r.abort()}),r=("https:"===a.protocol?require("https").request:require("http").request)(a,function(n){var r="",o=0;s.readyState=e.XHR.OPENED,t.onStateChange&&t.onStateChange(s),s.readyState=e.XHR.HEADERS_RECEIVED,s._rawHeaders=n.rawHeaders.join("\r\n"),s._headers=n.headers,s.responseURL=n.url||null,s.status=n.statusCode||null,s.statusText=n.statusMessage||null,t.onStateChange&&t.onStateChange(s),n.on("data",function(n){r+=n.toString(),o||(o=1,s.readyState=e.XHR.LOADING,t.onStateChange&&t.onStateChange(s),t.onLoadStart&&t.onLoadStart(s)),t.onProgress&&t.onProgress(s)}),n.on("end",function(){s.readyState=e.XHR.DONE,s.responseType="text",s.response=s.responseText=r,t.onStateChange&&t.onStateChange(s),t.onLoadEnd&&t.onLoadEnd(s),e.XHR.DONE===s.readyState&&(200===s.status?t.onComplete&&t.onComplete(s):t.onRequestError?t.onRequestError(s):t.onError&&t.onError(s))}),n.on("error",function(e){s.statusText=e.toString(),t.onError&&t.onError(s)})}),r.setTimeout(t.timeout||3e4,function(e){t.onTimeout&&t.onTimeout(s)}),r.on("abort",function(e){t.onAbort&&t.onAbort(s)}),r.on("error",function(e){s.statusText=e.toString(),t.onError&&t.onError(s)}),t.headers&&e.Util.Header.encode(t.headers,null,r),arguments.length>1&&s.send(n),s}:function(t,n){if(t=t||{},!t.url)return null;var r=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP"),o=new e.XHR(function(e){r.send(e)},function(){r.abort()}),s=function(e,t){return e.readyState=t.readyState,e.responseType=t.responseType,e.responseURL=t.responseURL,e.response=t.response,e.responseText=t.responseText,e.responseXml=t.responseXml,e.status=t.status,e.statusText=t.statusText,e};return o.getAllResponseHeaders=function(t){var n=r.getAllResponseHeaders();return!0===t?e.Util.Header.decode(n):n},o.getResponseHeader=function(e){return r.getResponseHeader(e)},r.open(t.method||"GET",t.url,!t.sync),o.responseType=r.responseType=t.responseType||"text",r.timeout=t.timeout||3e4,t.onProgress&&(r.onprogress=function(){t.onProgress(s(o,r))}),t.onLoadStart&&(r.onloadstart=function(){t.onLoadStart(s(o,r))}),t.onLoadEnd&&(r.onloadend=function(){t.onLoadEnd(s(o,r))}),!t.sync&&t.onStateChange&&(r.onreadystatechange=function(){t.onStateChange(s(o,r))}),r.onload=function(){s(o,r),e.XHR.DONE===r.readyState&&(200===r.status?t.onComplete&&t.onComplete(o):t.onRequestError?t.onRequestError(o):t.onError&&t.onError(o))},r.onabort=function(){t.onAbort&&t.onAbort(s(o,r))},r.onerror=function(){t.onError&&t.onError(s(o,r))},r.ontimeout=function(){t.onTimeout&&t.onTimeout(s(o,r))},t.headers&&e.Util.Header.encode(t.headers,r),t.mimeType&&r.overrideMimeType(t.mimeType),arguments.length>1&&o.send(n),o},e.UUID=function(e,t){return(e||"")+ ++p+"_"+Date.now()+"_"+Math.floor(1e3*Math.random())+(t||"")},e.Const={BASE64:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",CRLF:"\r\n",CRLF_RE:/(\r\n)|\r|\n/g,COOKIE_RE:/([^=]+)(?:=(.*))?/},e.Util={String:{trim:l},Utf8:{encode:function(e){e=e.replace(d,"\n");var t,n,r,o="";for(t=0,n=e.length;n>t;t++)r=e.charCodeAt(t),o+=128>r?a(r):r>127&&2048>r?a(r>>6|192)+a(63&r|128):a(r>>12|224)+a(r>>6&63|128)+a(63&r|128);return o},decode:function(e){for(var t="",n=0,r=c1=c2=0,o=e.length;o>n;)r=e.charCodeAt(n),128>r?(t+=a(r),n++):r>191&&224>r?(c2=e.charCodeAt(n+1),t+=a((31&r)<<6|63&c2),n+=2):(c2=e.charCodeAt(n+1),c3=e.charCodeAt(n+2),t+=a((15&r)<<12|(63&c2)<<6|63&c3),n+=3);return t}},Base64:{encode:function(t){t=e.Util.Utf8.encode(t);for(var n,r,o,s,i,a,u,l="",c=0,d=t.length,p=e.Const.BASE64;d>c;)n=t.charCodeAt(c++),r=t.charCodeAt(c++),o=t.charCodeAt(c++),s=n>>2,i=(3&n)<<4|r>>4,a=(15&r)<<2|o>>6,u=63&o,isNaN(r)?a=u=64:isNaN(o)&&(u=64),l=l+p.charAt(s)+p.charAt(i)+p.charAt(a)+p.charAt(u);return l},decode:function(t){t=t.replace(c,"");for(var n,r,o,s,i,u,l,d="",p=0,f=t.length;f>p;)s=keyString.indexOf(t.charAt(p++)),i=keyString.indexOf(t.charAt(p++)),u=keyString.indexOf(t.charAt(p++)),l=keyString.indexOf(t.charAt(p++)),n=s<<2|i>>4,r=(15&i)<<4|u>>2,o=(3&u)<<6|l,d+=a(n),64!=u&&(d+=a(r)),64!=l&&(d+=a(o));return d=e.Util.Utf8.decode(d)}},Json:{encode:JSON.stringify,decode:JSON.parse},Url:{create:function(t){if(!t)return"";var n,s,i,a,u,l,c,d,p,f=[],h=r(t),g=h.length,C=e.Util.Url.encode;for(a=0,n=g>a?[[s=h[a++],t[s]]]:[];n.length;){if(u=n.shift(),s=u[0],i=u[1],p=o.call(i),"[object Array]"===p)for(s+="[]",l=0,c=i.length;c>l;l++)n.unshift([s,i[l]]);else if("[object Object]"===p)for(d=r(i),l=0,c=d.length;c>l;l++)n.unshift([s+"["+d[l]+"]",i[d[l]]]);else f.push(C(s)+"="+C(i));!n.length&&g>a&&n.unshift([s=h[a++],t[s]])}return f.join("&")},rawencode:function(e){return encodeURIComponent(""+e).split("!").join("%21").split("'").join("%27").split("(").join("%28").split(")").join("%29").split("*").join("%2A")},rawdecode:function(e){return decodeURIComponent(""+e)},encode:function(t){return e.Util.Url.rawencode(t).split("%20").join("+")},decode:function(t){return e.Util.Url.rawdecode((""+t).split("+").join("%20"))}},Cookie:{create:function(e,t,n,r,o,s,i){var a=arguments.length;return{name:a>0?e:"",value:a>1?t:"",domain:a>2?n:"",path:a>3?r:"/",expires:a>4?o:new Date(Date.now()+31536e6),secure:a>5?!!s:!1,httponly:a>6?!!i:!1}},encode:function(e){if(e&&e.name){var t=String(e.name)+"="+String(e.value);return t+="; Domain="+String(e.domain),t+="; Path="+String(e.path),t+="; Expires="+String(e.expires),e.secure&&(t+="; Secure"),e.httponly&&(t+="; HttpOnly"),t}},decode:function(t){var r,o,s,i,a=e.Util.Cookie.create(),u=e.Const.COOKIE_RE,l=String(t).split("; ");if(null!=(r=l.shift().match(u))){for(a.name=r[1],a.value=r[2],s=0,i=l.length;i>s;s++)r=l[s].match(u),null!=r&&r.length&&(o=r[1].toLowerCase(),a[n](o)&&(a[o]="string"==typeof r[2]?r[2]:!0));return"string"==typeof a.expires&&(a.expires=new Date(a.expires)),a}}},Header:{encode:function(t,n,s){var i="";if(!t)return xhr?xhr:i;var a,u,l,c,d,p=r(t),f=e.Const.CRLF;if(s){for(u=0,l=p.length;l>u;u++)a=p[u],s.setHeader(a,t[a]);return s}if(n){for(u=0,l=p.length;l>u;u++)if(a=p[u],"[object Array]"===o.call(t[a]))for(c=0,d=t[a].length;d>c;c++)n.setRequestHeader(a,String(t[a][c]));else n.setRequestHeader(a,String(t[a]));return n}for(u=0,l=p.length;l>u;u++)if(a=p[u],"[object Array]"===o.call(t[a]))for(i.length&&(i+=f),i+=a+": "+String(t[a][0]),c=1,d=t[a].length;d>c;c++)i+=f+String(t[a][c]);else i.length&&(i+=f),i+=a+": "+String(t[a]);return i},decode:function(t,r){var o,s,i,a,u={},c=null,d=e.Const.CRLF;if(t)for(r=!0===r,t=t.split(d),s=0,i=t.length;i>s;s++)a=t[s],o=a.split(":"),o.length>1?(c=l(o.shift()),r&&(c=c.toLowerCase()),u[n](c)?("string"==typeof u[c]&&(u[c]=[u[c]]),u[c].push(l(o.join(":")))):u[c]=l(o.join(":"))):o[0].length&&c&&(u[c]=d+o[0]);return u}}},e.Client=function h(t){var n=this;return n instanceof h?(n.$cfg$=t||{},n.$event$={},void(n.status=e.Client.CREATED)):new h(t)},e.Client.Impl={},e.Client.CREATED=1,e.Client.DESTROYED=0,e.Client.OPENED=2,e.Client.CLOSED=4,e.Client.PENDING=8,e.Client.ABORTED=16,e.Client[t]={constructor:e.Client,status:e.Client.CREATED,$cfg$:null,$event$:null,dispose:function(){var t=this;return t.status=e.Client.DESTROYED,t.$cfg$=null,t.$event$=null,t},config:function(e,t){var n=this,r=n.$cfg$;return e?arguments.length>1?(r[e]=t,n):r[e]:void 0},on:function(e,t,r){var o=this;return e&&t?(o.$event$[n](e)?o.$event$[e].push([t,!0===r,0]):o.$event$[e]=[[t,!0===r,0]],o):o},one:function(e,t){return this.on(e,t,!0)},off:function(e,t){var r=this;if(!e||!r.$event$[n](e))return r;if(null==t)delete r.$event$[e];else{for(var o=r.$event$[e],s=o.length-1;s>=0;s--)o[s][0]===t&&t.splice(s,1);o.length||delete r.$event$[e]}return r},emit:function(e,t){var r=this;if(!e||!r.$event$[n](e))return r;var o,s,i=r.$event$[e].slice(),a=i.length,u=[],l={event:e,data:t,target:r};for(o=0;a>o;o++)s=i[o],s[1]&&u.push(o),s[1]&&s[2]||(s[2]=1,s[0](l));for(i=r.$event$[e],o=u.length-1;o>=0;o--)i.splice(u[o],1);return i.length||delete r.$event$[e],r},abort:function(t,n){return this.status=e.Client.ABORTED,!0===t?this.emit("abort",n):this},open:function(t){return this.status=e.Client.OPENED,this.emit("open",t)},close:function(t){return this.status=e.Client.CLOSED,this.emit("close",t)},send:function(e){return this},listen:function(){return this},init:function(){var e=this;return setTimeout(function(){e.listen()},40),e}},e.Client[t].addEventListener=e.Client[t].on,e.Client[t].removeEventListener=e.Client[t].off,e.Client[t].trigger=e.Client[t].dispatchEvent=e.Client[t].emit,e}); \ No newline at end of file +!function(e,t,n){"use strict";"undefined"!=typeof Components&&"object"==typeof Components.classes&&"object"==typeof Components.classesByID&&Components.utils&&"function"==typeof Components.utils["import"]?(e.EXPORTED_SYMBOLS=[t])&&(e[t]=n()):"object"==typeof exports?module.exports=n():(e[t]=n())&&"function"==typeof define&&define.amd&&define(function(){return e[t]})}(this,"RT",function(){"use strict";function e(t){t=t||{};var r=(t.use||t.rt_type||"default").toLowerCase();return e.Client.Impl[n](r)?new e.Client.Impl[r](t):new e.Client(t)}var t="prototype",n="hasOwnProperty",r=Object.keys,o=Object[t].toString,s="undefined"!=typeof Components&&"object"==typeof Components.classes&&"object"==typeof Components.classesByID&&Components.utils&&"function"==typeof Components.utils["import"],a="undefined"!=typeof global&&"[object global]"===o.call(global),i=String.fromCharCode,u=/^\s+|\s+$/g,l=String[t].trim?function(e){return e.trim()}:function(e){return e.replace(u,"")},c=/[^A-Za-z0-9\+\/\=]/g,d=/\x0d\x0a/g,p=0;return e.VERSION="1.0.0",e.Platform={XPCOM:s,Node:a},e.XHR=function f(e,t){var r=this,o=!1;r.readyState=f.UNSENT,r.status=null,r.statusText=null,r.responseType="text",r.responseURL=null,r.response=null,r.responseText=null,r.responseXml=null,r._rawHeaders=null,r._headers=null,r.send=function(t){return o||f.UNSENT!==r.readyState?r:(e&&e(t),r.readyState=f.OPENED,r)},r.abort=function(){return o?r:(o=!0,t&&t(),r)},r.getAllResponseHeaders=function(e){return f.DONE!==r.readyState?null:!0===e?r._headers:r._rawHeaders},r.getResponseHeader=function(e,t){if(null==e||f.DONE!==r.readyState)return null;var o=r._headers||{};return!1!==t&&(e=e.toLowerCase()),o[n](e)?o[e]:null},r.dispose=function(){return r.readyState=null,r.status=null,r.statusText=null,r.responseType=null,r.responseURL=null,r.response=null,r.responseText=null,r.responseXml=null,r._rawHeaders=null,r._headers=null,r.getAllResponseHeaders=null,r.getResponseHeader=null,r.send=null,r.abort=null,r}},e.XHR.UNSENT=0,e.XHR.OPENED=1,e.XHR.HEADERS_RECEIVED=2,e.XHR.LOADING=3,e.XHR.DONE=4,e.XHR.create=s?function(t,n){if(t=t||{},!t.url)return null;var r=Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(),o=new e.XHR(function(e){r.send(e)},function(){r.abort()}),s=function(e,t){return e.readyState=t.readyState,e.responseType=t.responseType,e.responseURL=t.responseURL,e.response=t.response,e.responseText=t.responseText,e.responseXml=t.responseXml,e.status=t.status,e.statusText=t.statusText,e};return o.getAllResponseHeaders=function(t){var n=r.getAllResponseHeaders();return!0===t?e.Util.Header.decode(n):n},o.getResponseHeader=function(e){return r.getResponseHeader(e)},r.open(t.method||"GET",t.url,!t.sync),o.responseType=r.responseType=t.responseType||"text",r.timeout=t.timeout||3e4,t.onProgress&&r.addEventListener("progress",function(){t.onProgress(s(o,r))}),t.onLoadStart&&r.addEventListener("loadstart",function(){t.onLoadStart(s(o,r))}),!t.sync&&t.onStateChange&&r.addEventListener("readystatechange",function(){t.onStateChange(s(o,r))}),r.addEventListener("load",function(){s(o,r),e.XHR.DONE===r.readyState&&(200===r.status?t.onComplete&&t.onComplete(o):t.onRequestError?t.onRequestError(o):t.onError&&t.onError(o))}),r.addEventListener("abort",function(){t.onAbort&&t.onAbort(s(o,r))}),r.addEventListener("error",function(){t.onError&&t.onError(s(o,r))}),r.addEventListener("timeout",function(){t.onTimeout&&t.onTimeout(s(o,r))}),t.headers&&e.Util.Header.encode(t.headers,r),t.mimeType&&r.overrideMimeType(t.mimeType),arguments.length>1&&o.send(n),o}:a?function(t,n){if(t=t||{},!t.url)return null;var r,s,a="[object Object]"===o.call(t.url)?t.url:require("url").parse(t.url),i={method:t.method||"GET",agent:!1,protocol:a.protocol,host:a.hostname,hostname:a.hostname,port:a.port||80,path:(a.pathname||"/")+(a.query?"?"+a.query:"")};return s=new e.XHR(function(e){null!=e&&(e=String(e),r.setHeader("Content-Length",String(e.length)),r.write(e)),r.end()},function(){r.abort()}),r=("https:"===i.protocol?require("https").request:require("http").request)(i,function(n){var r="",o=0;s.readyState=e.XHR.OPENED,t.onStateChange&&t.onStateChange(s),s.readyState=e.XHR.HEADERS_RECEIVED,s._rawHeaders=n.rawHeaders.join("\r\n"),s._headers=n.headers,s.responseURL=n.url||null,s.status=n.statusCode||null,s.statusText=n.statusMessage||null,t.onStateChange&&t.onStateChange(s),n.on("data",function(n){r+=n.toString(),o||(o=1,s.readyState=e.XHR.LOADING,t.onStateChange&&t.onStateChange(s),t.onLoadStart&&t.onLoadStart(s)),t.onProgress&&t.onProgress(s)}),n.on("end",function(){s.readyState=e.XHR.DONE,s.responseType="text",s.response=s.responseText=r,t.onStateChange&&t.onStateChange(s),t.onLoadEnd&&t.onLoadEnd(s),e.XHR.DONE===s.readyState&&(200===s.status?t.onComplete&&t.onComplete(s):t.onRequestError?t.onRequestError(s):t.onError&&t.onError(s))}),n.on("error",function(e){s.statusText=e.toString(),t.onError&&t.onError(s)})}),r.setTimeout(t.timeout||3e4,function(e){t.onTimeout&&t.onTimeout(s)}),r.on("abort",function(e){t.onAbort&&t.onAbort(s)}),r.on("error",function(e){s.statusText=e.toString(),t.onError&&t.onError(s)}),t.headers&&e.Util.Header.encode(t.headers,null,r),arguments.length>1&&s.send(n),s}:function(t,n){if(t=t||{},!t.url)return null;var r=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP"),o=new e.XHR(function(e){r.send(e)},function(){r.abort()}),s=function(e,t){return e.readyState=t.readyState,e.responseType=t.responseType,e.responseURL=t.responseURL,e.response=t.response,e.responseText=t.responseText,e.responseXml=t.responseXml,e.status=t.status,e.statusText=t.statusText,e};return o.getAllResponseHeaders=function(t){var n=r.getAllResponseHeaders();return!0===t?e.Util.Header.decode(n):n},o.getResponseHeader=function(e){return r.getResponseHeader(e)},r.open(t.method||"GET",t.url,!t.sync),o.responseType=r.responseType=t.responseType||"text",r.timeout=t.timeout||3e4,t.onProgress&&(r.onprogress=function(){t.onProgress(s(o,r))}),t.onLoadStart&&(r.onloadstart=function(){t.onLoadStart(s(o,r))}),t.onLoadEnd&&(r.onloadend=function(){t.onLoadEnd(s(o,r))}),!t.sync&&t.onStateChange&&(r.onreadystatechange=function(){t.onStateChange(s(o,r))}),r.onload=function(){s(o,r),e.XHR.DONE===r.readyState&&(200===r.status?t.onComplete&&t.onComplete(o):t.onRequestError?t.onRequestError(o):t.onError&&t.onError(o))},r.onabort=function(){t.onAbort&&t.onAbort(s(o,r))},r.onerror=function(){t.onError&&t.onError(s(o,r))},r.ontimeout=function(){t.onTimeout&&t.onTimeout(s(o,r))},t.headers&&e.Util.Header.encode(t.headers,r),t.mimeType&&r.overrideMimeType(t.mimeType),arguments.length>1&&o.send(n),o},e.UUID=function(e,t){return(e||"")+ ++p+"_"+Date.now()+"_"+Math.floor(1e3*Math.random())+(t||"")},e.Const={BASE64:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",CRLF:"\r\n",CRLF_RE:/(\r\n)|\r|\n/g,COOKIE_RE:/([^=]+)(?:=(.*))?/},e.Util={String:{trim:l},Utf8:{encode:function(e){e=e.replace(d,"\n");var t,n,r,o="";for(t=0,n=e.length;n>t;t++)r=e.charCodeAt(t),o+=128>r?i(r):r>127&&2048>r?i(r>>6|192)+i(63&r|128):i(r>>12|224)+i(r>>6&63|128)+i(63&r|128);return o},decode:function(e){for(var t="",n=0,r=c1=c2=0,o=e.length;o>n;)r=e.charCodeAt(n),128>r?(t+=i(r),n++):r>191&&224>r?(c2=e.charCodeAt(n+1),t+=i((31&r)<<6|63&c2),n+=2):(c2=e.charCodeAt(n+1),c3=e.charCodeAt(n+2),t+=i((15&r)<<12|(63&c2)<<6|63&c3),n+=3);return t}},Base64:{encode:function(t){t=e.Util.Utf8.encode(t);for(var n,r,o,s,a,i,u,l="",c=0,d=t.length,p=e.Const.BASE64;d>c;)n=t.charCodeAt(c++),r=t.charCodeAt(c++),o=t.charCodeAt(c++),s=n>>2,a=(3&n)<<4|r>>4,i=(15&r)<<2|o>>6,u=63&o,isNaN(r)?i=u=64:isNaN(o)&&(u=64),l=l+p.charAt(s)+p.charAt(a)+p.charAt(i)+p.charAt(u);return l},decode:function(t){t=t.replace(c,"");for(var n,r,o,s,a,u,l,d="",p=0,f=t.length;f>p;)s=keyString.indexOf(t.charAt(p++)),a=keyString.indexOf(t.charAt(p++)),u=keyString.indexOf(t.charAt(p++)),l=keyString.indexOf(t.charAt(p++)),n=s<<2|a>>4,r=(15&a)<<4|u>>2,o=(3&u)<<6|l,d+=i(n),64!=u&&(d+=i(r)),64!=l&&(d+=i(o));return d=e.Util.Utf8.decode(d)}},Json:{encode:JSON.stringify,decode:JSON.parse},Url:{create:function(t){if(!t)return"";var n,s,a,i,u,l,c,d,p,f=[],h=r(t),g=h.length,C=e.Util.Url.encode;for(i=0,n=g>i?[[s=h[i++],t[s]]]:[];n.length;){if(u=n.shift(),s=u[0],a=u[1],p=o.call(a),"[object Array]"===p)for(s+="[]",l=0,c=a.length;c>l;l++)n.unshift([s,a[l]]);else if("[object Object]"===p)for(d=r(a),l=0,c=d.length;c>l;l++)n.unshift([s+"["+d[l]+"]",a[d[l]]]);else f.push(C(s)+"="+C(a));!n.length&&g>i&&n.unshift([s=h[i++],t[s]])}return f.join("&")},rawencode:function(e){return encodeURIComponent(""+e).split("!").join("%21").split("'").join("%27").split("(").join("%28").split(")").join("%29").split("*").join("%2A")},rawdecode:function(e){return decodeURIComponent(""+e)},encode:function(t){return e.Util.Url.rawencode(t).split("%20").join("+")},decode:function(t){return e.Util.Url.rawdecode((""+t).split("+").join("%20"))}},Cookie:{create:function(e,t,n,r,o,s,a){var i=arguments.length;return{name:i>0?e:"",value:i>1?t:"",domain:i>2?n:"",path:i>3?r:"/",expires:i>4?o:new Date(Date.now()+31536e6),secure:i>5?!!s:!1,httponly:i>6?!!a:!1}},encode:function(e){if(e&&e.name){var t=String(e.name)+"="+String(e.value);return t+="; Domain="+String(e.domain),t+="; Path="+String(e.path),t+="; Expires="+String(e.expires),e.secure&&(t+="; Secure"),e.httponly&&(t+="; HttpOnly"),t}},decode:function(t){var r,o,s,a,i=e.Util.Cookie.create(),u=e.Const.COOKIE_RE,l=String(t).split("; ");if(null!=(r=l.shift().match(u))){for(i.name=r[1],i.value=r[2],s=0,a=l.length;a>s;s++)r=l[s].match(u),null!=r&&r.length&&(o=r[1].toLowerCase(),i[n](o)&&(i[o]="string"==typeof r[2]?r[2]:!0));return"string"==typeof i.expires&&(i.expires=new Date(i.expires)),i}}},Header:{encode:function(t,n,s){var a="";if(!t)return xhr?xhr:a;var i,u,l,c,d,p=r(t),f=e.Const.CRLF;if(s){for(u=0,l=p.length;l>u;u++)i=p[u],s.setHeader(i,t[i]);return s}if(n){for(u=0,l=p.length;l>u;u++)if(i=p[u],"[object Array]"===o.call(t[i]))for(c=0,d=t[i].length;d>c;c++)n.setRequestHeader(i,String(t[i][c]));else n.setRequestHeader(i,String(t[i]));return n}for(u=0,l=p.length;l>u;u++)if(i=p[u],"[object Array]"===o.call(t[i]))for(a.length&&(a+=f),a+=i+": "+String(t[i][0]),c=1,d=t[i].length;d>c;c++)a+=f+String(t[i][c]);else a.length&&(a+=f),a+=i+": "+String(t[i]);return a},decode:function(t,r){var o,s,a,i,u={},c=null,d=e.Const.CRLF;if(t)for(r=!0===r,t=t.split(d),s=0,a=t.length;a>s;s++)i=t[s],o=i.split(":"),o.length>1?(c=l(o.shift()),r&&(c=c.toLowerCase()),u[n](c)?("string"==typeof u[c]&&(u[c]=[u[c]]),u[c].push(l(o.join(":")))):u[c]=l(o.join(":"))):o[0].length&&c&&(u[c]=d+o[0]);return u}}},e.Client=function h(t){var n=this;return n instanceof h?(n.$cfg$=t||{},n.$event$={},void(n.status=e.Client.CREATED)):new h(t)},e.Client.Impl={},e.Client.CREATED=1,e.Client.DESTROYED=0,e.Client.OPENED=2,e.Client.CLOSED=4,e.Client.PENDING=8,e.Client.ABORTED=16,e.Client[t]={constructor:e.Client,status:e.Client.CREATED,$cfg$:null,$event$:null,dispose:function(){var t=this;return t.status=e.Client.DESTROYED,t.$cfg$=null,t.$event$=null,t},config:function(e,t){var n=this,r=n.$cfg$;return e?arguments.length>1?(r[e]=t,n):r[e]:void 0},on:function(e,t,r){var o=this;return e&&t?(o.$event$[n](e)?o.$event$[e].push([t,!0===r,0]):o.$event$[e]=[[t,!0===r,0]],o):o},one:function(e,t){return this.on(e,t,!0)},off:function(e,t){var r=this;if(!e||!r.$event$[n](e))return r;if(null==t)delete r.$event$[e];else{for(var o=r.$event$[e],s=o.length-1;s>=0;s--)o[s][0]===t&&t.splice(s,1);o.length||delete r.$event$[e]}return r},emit:function(e,t){var r=this;if(!e||!r.$event$[n](e))return r;var o,s,a=r.$event$[e].slice(),i=a.length,u=[],l={event:e,data:t,target:r};for(o=0;i>o;o++)s=a[o],s[1]&&u.push(o),s[1]&&s[2]||(s[2]=1,s[0](l));for(a=r.$event$[e],o=u.length-1;o>=0;o--)a.splice(u[o],1);return a.length||delete r.$event$[e],r},abort:function(t,n){return this.status=e.Client.ABORTED,!0===t?this.emit("abort",n):this},open:function(t){return this.status=e.Client.OPENED,this.emit("open",t)},close:function(t){return this.status=e.Client.CLOSED,this.emit("close",t)},send:function(e){return this},listen:function(){return this},init:function(){var e=this;return setTimeout(function(){e.listen()},40),e}},e.Client[t].addEventListener=e.Client[t].on,e.Client[t].removeEventListener=e.Client[t].off,e.Client[t].trigger=e.Client[t].dispatchEvent=e.Client[t].emit,e}); \ No newline at end of file diff --git a/test/chat/chat.js b/test/chat/chat.js new file mode 100644 index 0000000..48a5f74 --- /dev/null +++ b/test/chat/chat.js @@ -0,0 +1,111 @@ +"use strict"; + +var RT = require("../../src/RT.js"); +require("../../src/RT.Poll.js"); +require("../../src/RT.BOSH.js"); +require("../../src/RT.WebSocket.js"); + +function parse_args( args ) +{ + var + Flags = {}, Options = {}, Params = [], + optionname = '', argumentforoption = false, + arg, index, i, len, i0 + ; + + if ( args ) + { + i0 = 0; + } + else + { + args = process.argv; + // remove firt 2 args ('node' and 'this filename') + //args = args.slice(2); + i0 = 2; + } + + for (i=i0,len=args.length; i 1 && '-' == arg[0] && '-' != arg[1]) + { + arg.slice(1).split('').forEach(function(c){ + Flags[c] = true; + }); + argumentforoption = false; + } + /*/^--/.test(arg)*/ + else if (/^--/.test(arg)) + { + index = arg.indexOf('='); + if (~index) + { + optionname = arg.slice(2, index); + Options[optionname] = arg.slice(index + 1); + argumentforoption = false; + } + else + { + optionname = arg.slice(2); + Options[optionname] = true; + argumentforoption = true; + } + } + else + { + if (argumentforoption) + { + Options[optionname] = arg; + } + else + { + Params.push(arg); + } + argumentforoption = false; + } + } + + return {flags: Flags, options: Options, params: Params}; +} + +var args = parse_args(); +var user = args.options['user'] || 'user'; +var rt_impl = (args.options['use'] || 'poll').toLowerCase(); +if ( 'poll' !== rt_impl && 'bosh' !== rt_impl && 'ws' !== rt_impl ) rt_impl = 'poll'; + +console.log('user = ' + user); +console.log('use = ' + ('ws' === rt_impl ? 'WebSocket' : ('bosh' === rt_impl ? 'BOSH' : 'Poll'))); +console.log('---------------------------------'); + +var rt_chat = RT({ + use: rt_impl, + endpoint: 'ws' === rt_impl ? 'ws://127.0.0.1:1111' : ('bosh' === rt_impl ? 'http://_mygit/RT/test/chat/relay.php?bosh=1' : 'http://_mygit/RT/test/chat/relay.php?poll=1') + }) + .on('receive', function( evt ){ + if ( !evt.data ) return; + var m = RT.Util.Json.decode( evt.data ); + console.log('user : ' + m.user); + console.log('message : ' + m.message); + }) + /*.on('open', function( ){ + console.log('OPENED'); + })*/ + .on('close', function( ){ + console.log('CLOSED!'); + }) + .on('error', function( evt ){ + console.log('ERROR: '+evt.data); + }) + .init( ) +; + +function send( msg ) +{ + if ( RT.Client.OPENED !== rt_chat.status ) return; + if ( !msg.length ) return; + rt_chat.send(RT.Util.Json.encode({ + 'user': user, + 'message': msg + })); +}