native-lite.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /**
  2. * desc: js和客户端的交互模块「仅客户端」
  3. * author: wangyang
  4. * modify: ll
  5. * date: 2015-04-11
  6. */
  7. //本地回调对象
  8. var NativeCallback;
  9. var NativeDataAdapter;
  10. define(['require', 'helper', 'config'], function (require, helper, config) {
  11. //客户端接口类
  12. function NativeApi() {
  13. if (typeof NativeApi.instance === 'object') {
  14. return NativeApi.instance;
  15. }
  16. NativeApi.instance = this;
  17. this.getUserInfoCallback = null;
  18. this.shareCallback = null;
  19. this.getSignCallback = null;
  20. this.loginCallback = null;
  21. this.isWxAppInstalledCallback = null;
  22. this.payCallback = null;
  23. this.selectAddressCallback = null;
  24. this.chooseImageCallback = null;
  25. this.uploadImageCallbacks = {};
  26. this.selectPetTypeCallback = null;
  27. this.charge = null;
  28. this.keyboardEventTimer = 0;
  29. this.originalHeight = document.body.scrollHeight;
  30. this.keyboardIsOpen = false;
  31. this.keyboardIsClose = false;
  32. }
  33. NativeApi.prototype = {
  34. //获取用户信息
  35. getUserInfo: function (callback) {
  36. var that = this;
  37. var _callback = 'NativeCallback.getUserInfo';
  38. helper.osProxy({
  39. android: function () {
  40. var param = {
  41. callback: _callback
  42. };
  43. var param_str = JSON.stringify(param);
  44. window.jsapi.getUserInfo(param_str);
  45. },
  46. ios: function () {
  47. window.location.href = 'http://callclient?method=getUserInfo&callback=' + _callback;
  48. }
  49. });
  50. var callback = typeof (arguments[0]) == 'function' ? arguments[0] : null;
  51. this.getUserInfoCallback = callback;
  52. },
  53. //从web返回APP
  54. back: function () {
  55. helper.osProxy({
  56. android: function () {
  57. window.jsapi.exitWebView();
  58. },
  59. ios: function () {
  60. window.location.href = 'http://callclient?method=exitWebView';
  61. }
  62. });
  63. },
  64. //登录
  65. login: function (callback) {
  66. var that = this;
  67. var _callback = 'NativeCallback.login';
  68. helper.osProxy({
  69. android: function () {
  70. var param = {
  71. callback: _callback
  72. };
  73. var param_str = JSON.stringify(param);
  74. window.jsapi.goLogin(param_str);
  75. },
  76. ios: function () {
  77. window.location.href = 'http://callclient?method=goLogin&callback=' + _callback;
  78. }
  79. });
  80. var callback = typeof (arguments[0]) == 'function' ? arguments[0] : null;
  81. this.loginCallback = callback;
  82. },
  83. //分享
  84. share: function (title, content, img_url, url, app_id, callback) {
  85. var param = {
  86. "share_title": title,
  87. "share_string": content,
  88. "share_img_url": img_url ? encodeURI(img_url) : 'http://common.yiguanjia.club/images/logo.png',
  89. "share_url": url ? encodeURI(url) : 'http://common.yiguanjia.club',
  90. "share_app_id": app_id
  91. };
  92. var _callback = 'NativeCallback.share';
  93. helper.osProxy({
  94. android: function () {
  95. param['callback'] = _callback;
  96. var param_str = JSON.stringify(param);
  97. window.jsapi.doShare(param_str);
  98. },
  99. ios: function () {
  100. var param_str = JSON.stringify(param);
  101. window.location.href = 'http://callclient?method=doShare&param=' + param_str + '&callback=' + _callback;
  102. }
  103. });
  104. var callback = typeof (arguments[5]) == 'function' ? arguments[5] : null;
  105. this.shareCallback = callback;
  106. },
  107. //参数加密验证
  108. getSign: function (param, callback) {
  109. var that = this;
  110. for (var x in param) {
  111. param[x] = param[x].toString();
  112. }
  113. var param = {
  114. sign_json_string: param
  115. };
  116. var _callback = 'NativeCallback.getSign';
  117. helper.osProxy({
  118. android: function () {
  119. param['callback'] = _callback;
  120. var param_str = JSON.stringify(param);
  121. window.jsapi.sign(param_str);
  122. },
  123. ios: function () {
  124. var param_str = JSON.stringify(param);
  125. window.location.href = 'http://callclient?method=sign&param=' + param_str + '&callback=' + _callback;
  126. },
  127. wx: function () {
  128. //目前加上参数,绕过验证,后面再加上相关的验证
  129. param['user_id'] = localStorage.getItem('wxUserID');
  130. param['request_from'] = 'weixin';
  131. if (that.getSignCallback) {
  132. that.getSignCallback(param);
  133. }
  134. }
  135. });
  136. var callback = typeof (arguments[1]) == 'function' ? arguments[1] : null;
  137. this.getSignCallback = callback;
  138. },
  139. //支付
  140. pay: function (params, callback) {
  141. var that = this;
  142. var param = {};
  143. var _callback = 'NativeCallback.pay';
  144. helper.osProxy({
  145. android: function () {
  146. param['charge'] = params.charge;
  147. param['callback'] = _callback;
  148. var param_str = JSON.stringify(param);
  149. window.jsapi.pay(param_str);
  150. },
  151. ios: function () {
  152. NativeDataAdapter.charge = JSON.stringify(params.charge);
  153. param['charge'] = 'NativeDataAdapter.charge';
  154. var param_str = JSON.stringify(param);
  155. window.location.href = 'http://callclient?method=pay&param=' + encodeURIComponent(param_str) + '&callback=' + _callback;
  156. }
  157. });
  158. var callback = typeof (arguments[1]) == 'function' ? arguments[1] : null;
  159. this.payCallback = callback;
  160. },
  161. //检测是否安装了微信
  162. isWxAppInstalled: function (callback) {
  163. var _callback = 'NativeCallback.isWxInstalled';
  164. var param = {};
  165. helper.osProxy({
  166. android: function () {
  167. param['callback'] = _callback;
  168. var param_str = JSON.stringify(param);
  169. window.jsapi.isWxAppInstalled(param_str);
  170. },
  171. ios: function () {
  172. var param_str = JSON.stringify(param);
  173. window.location.href = 'http://callclient?method=isWxAppInstalled&param=' + param_str + '&callback=' + _callback;
  174. }
  175. });
  176. var callback = typeof (arguments[0]) == 'function' ? arguments[0] : null;
  177. this.isWxAppInstalledCallback = callback;
  178. },
  179. //选择地址
  180. selectAddress: function (callback) {
  181. var _callback = 'NativeCallback.selectAddress';
  182. var param = {};
  183. helper.osProxy({
  184. android: function () {
  185. param['callback'] = _callback;
  186. var param_str = JSON.stringify(param);
  187. window.jsapi.selectAddress(param_str);
  188. },
  189. ios: function () {
  190. var param_str = JSON.stringify(param);
  191. window.location.href = 'http://callclient?method=selectAddress&param=' + param_str + '&callback=' + _callback;
  192. }
  193. });
  194. var callback = typeof (arguments[0]) == 'function' ? arguments[0] : null;
  195. this.selectAddressCallback = callback;
  196. },
  197. //打开一个链接
  198. openURL: function (new_param) {
  199. if (config.appVersion < 2.6) {
  200. return false;
  201. }
  202. var param = {
  203. url: 'http://common.yiguanjia.club'
  204. };
  205. if (typeof new_param === 'undefined') {
  206. new_param = {};
  207. }
  208. for (var x in new_param) {
  209. if (param.hasOwnProperty(x)) {
  210. param[x] = new_param[x];
  211. }
  212. }
  213. helper.osProxy({
  214. android: function () {
  215. var param_str = JSON.stringify(param);
  216. window.jsapi.openURL(param_str);
  217. },
  218. ios: function () {
  219. var param_str = JSON.stringify(param);
  220. window.location.href = 'http://callclient?method=openURL&param=' + param_str;
  221. }
  222. });
  223. },
  224. //打开多个页面时页面之间通信
  225. postMessage: function (data) {
  226. if (config.appVersion < 2.6) {
  227. return false;
  228. }
  229. var _callback = 'NativeCallback.postMessage';
  230. helper.osProxy({
  231. android: function () {
  232. var param = {
  233. data: JSON.stringify(data),
  234. callback: _callback
  235. };
  236. var param_str = JSON.stringify(param);
  237. window.jsapi.postMessage(param_str);
  238. },
  239. ios: function () {
  240. var param = {
  241. data: data,
  242. callback: _callback
  243. };
  244. var param_str = JSON.stringify(param);
  245. window.location.href = 'http://callclient?method=postMessage&param=' + param_str;
  246. }
  247. });
  248. },
  249. //选择图片
  250. chooseImage: function (new_param, callback) {
  251. if (config.appVersion < 2.6) {
  252. return false;
  253. }
  254. var param = {
  255. sourceType: ['album', 'camera'],
  256. count: 9,
  257. clip: false,
  258. clipSize: 320
  259. };
  260. if (typeof new_param === 'undefined') {
  261. new_param = {};
  262. }
  263. for (var x in new_param) {
  264. if (param.hasOwnProperty(x)) {
  265. param[x] = new_param[x];
  266. }
  267. }
  268. var _callback = 'NativeCallback.chooseImage';
  269. helper.osProxy({
  270. android: function () {
  271. param['callback'] = _callback;
  272. var param_str = JSON.stringify(param);
  273. window.jsapi.chooseImage(param_str);
  274. },
  275. ios: function () {
  276. var param_str = JSON.stringify(param);
  277. window.location.href = 'http://callclient?method=chooseImage&param=' + param_str + '&callback=' + _callback;
  278. }
  279. });
  280. var callback = typeof (arguments[1]) == 'function' ? arguments[1] : null;
  281. this.chooseImageCallback = callback;
  282. },
  283. //预览图片
  284. previewImage: function (urls, current) {
  285. if (!config.isWX && config.appVersion < 2.6) {
  286. return false;
  287. }
  288. var param = {
  289. urls: urls,
  290. current: current,
  291. };
  292. var param_str = JSON.stringify(param);
  293. helper.osProxy({
  294. android: function () {
  295. window.jsapi.previewImage(param_str);
  296. },
  297. ios: function () {
  298. window.location.href = 'http://callclient?method=previewImage&param=' + param_str;
  299. }
  300. });
  301. },
  302. //上传图片
  303. uploadImage: function (new_param, callback) {
  304. if (config.appVersion < 2.6) {
  305. return false;
  306. }
  307. var param = {
  308. localId: '',
  309. isShowProgressTips: true,
  310. };
  311. if (typeof new_param === 'undefined') {
  312. new_param = {};
  313. }
  314. for (var x in new_param) {
  315. if (param.hasOwnProperty(x)) {
  316. param[x] = new_param[x];
  317. }
  318. }
  319. var _callback = 'NativeCallback.uploadImage';
  320. helper.osProxy({
  321. android: function () {
  322. param['callback'] = _callback;
  323. var param_str = JSON.stringify(param);
  324. window.jsapi.uploadImage(param_str);
  325. },
  326. ios: function () {
  327. var param_str = JSON.stringify(param);
  328. window.location.href = 'http://callclient?method=uploadImage&param=' + param_str + '&callback=' + _callback;
  329. }
  330. });
  331. var callback = typeof (arguments[1]) == 'function' ? arguments[1] : null;
  332. this.uploadImageCallbacks[param.localId] = callback;
  333. },
  334. // 选择宠物
  335. selectPetType: function (callback){
  336. var _callback = 'NativeCallback.selectPetType';
  337. var param = {};
  338. helper.osProxy({
  339. android: function () {
  340. param['callback'] = _callback;
  341. var param_str = JSON.stringify(param);
  342. window.jsapi.selectPetType(param_str);
  343. },
  344. ios: function () {
  345. var param_str = JSON.stringify(param);
  346. window.location.href = 'http://callclient?method=selectPetType&param=' + param_str + '&callback=' + _callback;
  347. }
  348. });
  349. var callback = typeof (arguments[0]) == 'function' ? arguments[0] : null;
  350. this.selectPetTypeCallback = callback;
  351. },
  352. // 拨打电话
  353. call: function (telephone) {
  354. var that = this;
  355. helper.osProxy({
  356. android: function () {
  357. if (config.appVersion < '2.2') {
  358. //无法调用拨号,弹出
  359. $(document).trigger('spa:openpanel', ['simpleAlert', {
  360. message: '客服热线:' + telephone
  361. }]);
  362. } else {
  363. var param = {
  364. telephone: telephone
  365. };
  366. var param_str = JSON.stringify(param);
  367. window.jsapi.call(param_str);
  368. }
  369. },
  370. ios: function () {
  371. window.location.href = 'tel:' + telephone;
  372. }
  373. });
  374. },
  375. //ios
  376. //开关滑动返回
  377. switchPopGesture: function (flag) {
  378. var param = {
  379. enable: flag
  380. };
  381. helper.osProxy({
  382. ios: function () {
  383. var param_str = JSON.stringify(param);
  384. window.location.href = 'http://callclient?method=switchPopGesture&param=' + param_str;
  385. }
  386. });
  387. },
  388. //安卓
  389. //代理物理返回按钮
  390. delegateBackButton: function (flag, callback) {
  391. var param = {
  392. enable: flag,
  393. callback: 'NativeCallback.back'
  394. };
  395. helper.osProxy({
  396. android: function () {
  397. var param_str = JSON.stringify(param);
  398. window.jsapi.delegateBackButton(param_str);
  399. }
  400. });
  401. },
  402. //注册键盘事件,由于安卓回调有问题,所以改为js setInterval自定义检测
  403. registerEvent: function (enable) {
  404. if (!enable) {
  405. if (this.keyboardEventTimer != 0) {
  406. clearInterval(this.keyboardEventTimer);
  407. }
  408. return;
  409. }
  410. var that = this;
  411. this.keyboardIsOpen = false;
  412. this.keyboardIsClose = false;
  413. helper.osProxy({
  414. android: function () {
  415. that.keyboardEventTimer = setInterval(function () {
  416. if (that.originalHeight > document.body.scrollHeight) {
  417. if (!that.keyboardIsOpen) {
  418. that.keyboardIsOpen = true;
  419. that.keyboardIsClose = false;
  420. $('input, textarea').trigger('openKeyboard');
  421. }
  422. } else {
  423. if (!that.keyboardIsClose) {
  424. that.keyboardIsClose = true;
  425. that.keyboardIsOpen = false;
  426. $('input, textarea').trigger('closeKeyboard');
  427. }
  428. }
  429. }, 50);
  430. }
  431. });
  432. }
  433. };
  434. /*
  435. * 回调类
  436. */
  437. function CallbackApi() {
  438. if (typeof CallbackApi.instance === 'object') {
  439. return CallbackApi.instance;
  440. }
  441. CallbackApi.instance = this;
  442. }
  443. CallbackApi.prototype = {
  444. formatNativeParam: function (res) {
  445. var f_res = helper.osProxy({
  446. android: function () {
  447. var reg = new RegExp('(\r\n|\r|\n)', 'g');
  448. res = JSON.stringify(res).replace(reg, '');
  449. return JSON.parse(res);
  450. },
  451. ios: function () {
  452. if (res && res.hasOwnProperty('success')) {
  453. if (res.success == '1') {
  454. res.success = true;
  455. } else {
  456. res.success = false;
  457. }
  458. }
  459. return res;
  460. }
  461. });
  462. return f_res;
  463. },
  464. getUserInfo: function (res) {
  465. var f_res = this.formatNativeParam(res);
  466. var native_api = new NativeApi();
  467. if (typeof (native_api.getUserInfoCallback) == 'function') {
  468. native_api.getUserInfoCallback(f_res);
  469. }
  470. },
  471. login: function (res) {
  472. //fix by wangyang
  473. //ios客户端登陆回调的通知在2.0有点早,改为延时0.1s再重新获取一次用户信息
  474. var f_res = this.formatNativeParam(res);
  475. var nativeApi = new NativeApi();
  476. if (nativeApi.loginCallback != null) {
  477. setTimeout(function () {
  478. nativeApi.getUserInfo(nativeApi.loginCallback);
  479. nativeApi.loginCallback(f_res);
  480. }, 100);
  481. }
  482. },
  483. share: function (res) {
  484. var f_res = this.formatNativeParam(res);
  485. var native_api = new NativeApi();
  486. if (typeof (native_api.shareCallback) == 'function') {
  487. native_api.shareCallback(f_res);
  488. }
  489. },
  490. getSign: function (res) {
  491. var f_res = this.formatNativeParam(res);
  492. var native_api = new NativeApi();
  493. if (typeof (native_api.getSignCallback) == 'function') {
  494. native_api.getSignCallback(f_res);
  495. }
  496. },
  497. pay: function (res) {
  498. var f_res = this.formatNativeParam(res);
  499. if (f_res.success) {
  500. f_res.message = '支付成功';
  501. } else {
  502. helper.osProxy({
  503. android: function () {
  504. if (config.appVersion < '2.2') {
  505. if (f_res.message == 'user_cancelled') {
  506. f_res.message = '取消支付';
  507. } else {
  508. f_res.message = '支付异常,请稍后再试';
  509. }
  510. } else {
  511. switch(f_res.error_code){
  512. case 'fail':
  513. f_res.message = '支付失败';
  514. break;
  515. case 'cancel':
  516. f_res.message = '取消支付';
  517. break;
  518. default:
  519. f_res.message = '支付异常,请稍后再试';
  520. break;
  521. }
  522. }
  523. },
  524. ios: function () {
  525. if (f_res.error_code == 5) {
  526. f_res.message = '取消支付';
  527. } else {
  528. f_res.message = '支付异常,请稍后再试';
  529. }
  530. }
  531. });
  532. }
  533. var native_api = new NativeApi();
  534. if (typeof (native_api.payCallback) == 'function') {
  535. native_api.payCallback(f_res);
  536. }
  537. },
  538. selectAddress: function (res) {
  539. var f_res = this.formatNativeParam(res);
  540. var native_api = new NativeApi();
  541. helper.osProxy({
  542. android: function () {
  543. //2.1返回的data是json字符串,特殊处理一下
  544. if (f_res.success && typeof (f_res.data) == 'string') {
  545. f_res.data = JSON.parse(f_res.data);
  546. }
  547. }
  548. });
  549. if (typeof (native_api.selectAddressCallback) == 'function') {
  550. native_api.selectAddressCallback(f_res);
  551. }
  552. },
  553. selectPetType: function (res) {
  554. var f_res = this.formatNativeParam(res);
  555. var native_api = new NativeApi();
  556. if (typeof (native_api.selectPetTypeCallback) == 'function') {
  557. native_api.selectPetTypeCallback(f_res);
  558. }
  559. },
  560. chooseImage: function (res) {
  561. var f_res = this.formatNativeParam(res);
  562. var native_api = new NativeApi();
  563. if (typeof (native_api.chooseImageCallback) == 'function') {
  564. native_api.chooseImageCallback(f_res);
  565. }
  566. },
  567. uploadImage: function (res) {
  568. var f_res = this.formatNativeParam(res);
  569. var native_api = new NativeApi();
  570. if (res.data.localId && typeof (native_api.uploadImageCallbacks[res.data.localId]) == 'function') {
  571. native_api.uploadImageCallbacks[res.data.localId](f_res);
  572. }
  573. },
  574. isWxInstalled: function (res) {
  575. var f_res = this.formatNativeParam(res);
  576. var native_api = new NativeApi();
  577. if (typeof (native_api.isWxAppInstalledCallback) == 'function') {
  578. native_api.isWxAppInstalledCallback(f_res);
  579. }
  580. },
  581. //以下回调为js自定义事件,注意,依赖jquery的trigger
  582. postMessage: function (res) {
  583. var f_res = this.formatNativeParam(res);
  584. f_res = helper.osProxy({
  585. android: function () {
  586. f_res.data = JSON.parse(f_res.data);
  587. return f_res;
  588. },
  589. ios: function () {
  590. return f_res;
  591. }
  592. });
  593. require(['$'], function ($) {
  594. $(document).trigger('postMessage', f_res);
  595. });
  596. },
  597. back: function () {
  598. require(['$'], function ($) {
  599. $(document).trigger('tapBackButton');
  600. });
  601. }
  602. };
  603. /*
  604. * 传递数据类,用于ios,某些复杂的json数据不适合通过url传递
  605. * 换一种思路,通过提供一个JS类给OC,然后OC调用js方法反查数据
  606. */
  607. function NativeDataCache() {
  608. if (typeof NativeDataCache.instance === 'object') {
  609. return NativeDataCache.instance;
  610. }
  611. NativeDataCache.instance = this;
  612. NativeDataCache.charge = '';
  613. }
  614. /**
  615. * android websocket支持
  616. */
  617. helper.osProxy({
  618. android: function () {
  619. if (typeof(WebSocketFactory) == 'undefined') {
  620. return false;
  621. }
  622. // window object
  623. var global = window;
  624. // WebSocket Object. All listener methods are cleaned up!
  625. var WebSocket = global.WebSocket = function (url) {
  626. // get a new websocket object from factory (check com.strumsoft.websocket.WebSocketFactory.java)
  627. this.socket = WebSocketFactory.getInstance(url);
  628. // store in registry
  629. if (this.socket) {
  630. WebSocket.store[this.socket.getId()] = this;
  631. } else {
  632. throw new Error('Websocket instantiation failed! Address might be wrong.');
  633. }
  634. };
  635. // private property
  636. WebSocket._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  637. // private method for decoding base64
  638. WebSocket._decode = function (input) {
  639. var output = "";
  640. var chr1, chr2, chr3;
  641. var enc1, enc2, enc3, enc4;
  642. var i = 0;
  643. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  644. while (i < input.length) {
  645. enc1 = this._keyStr.indexOf(input.charAt(i++));
  646. enc2 = this._keyStr.indexOf(input.charAt(i++));
  647. enc3 = this._keyStr.indexOf(input.charAt(i++));
  648. enc4 = this._keyStr.indexOf(input.charAt(i++));
  649. chr1 = (enc1 << 2) | (enc2 >> 4);
  650. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  651. chr3 = ((enc3 & 3) << 6) | enc4;
  652. output = output + String.fromCharCode(chr1);
  653. if (enc3 != 64) {
  654. output = output + String.fromCharCode(chr2);
  655. }
  656. if (enc4 != 64) {
  657. output = output + String.fromCharCode(chr3);
  658. }
  659. }
  660. output = this._utf8_decode(output);
  661. return output;
  662. }
  663. // private method for UTF-8 decoding
  664. WebSocket._utf8_decode = function (utftext) {
  665. var string = "";
  666. var i = 0;
  667. var c = c1 = c2 = 0;
  668. while (i < utftext.length) {
  669. c = utftext.charCodeAt(i);
  670. if (c < 128) {
  671. string += String.fromCharCode(c);
  672. i++;
  673. } else if ((c > 191) && (c < 224)) {
  674. c2 = utftext.charCodeAt(i + 1);
  675. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  676. i += 2;
  677. } else {
  678. c2 = utftext.charCodeAt(i + 1);
  679. c3 = utftext.charCodeAt(i + 2);
  680. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  681. i += 3;
  682. }
  683. }
  684. return string;
  685. }
  686. // storage to hold websocket object for later invokation of event methods
  687. WebSocket.store = {};
  688. // static event methods to call event methods on target websocket objects
  689. WebSocket.onmessage = function (evt) {
  690. WebSocket.store[evt._target]['onmessage'].call(global, this._decode(evt._data));
  691. }
  692. WebSocket.onopen = function (evt) {
  693. WebSocket.store[evt._target]['onopen'].call(global, evt);
  694. }
  695. WebSocket.onclose = function (evt) {
  696. WebSocket.store[evt._target]['onclose'].call(global, evt);
  697. }
  698. WebSocket.onerror = function (evt) {
  699. WebSocket.store[evt._target]['onerror'].call(global, evt);
  700. }
  701. // instance event methods
  702. WebSocket.prototype.send = function (data) {
  703. this.socket.send(data);
  704. }
  705. WebSocket.prototype.close = function () {
  706. this.socket.close();
  707. }
  708. WebSocket.prototype.getReadyState = function () {
  709. this.socket.getReadyState();
  710. }
  711. ///////////// Must be overloaded
  712. WebSocket.prototype.onopen = function () {
  713. throw new Error('onopen not implemented.');
  714. };
  715. // alerts message pushed from server
  716. WebSocket.prototype.onmessage = function (msg) {
  717. throw new Error('onmessage not implemented.');
  718. };
  719. // alerts message pushed from server
  720. WebSocket.prototype.onerror = function (msg) {
  721. throw new Error('onerror not implemented.');
  722. };
  723. // alert close event
  724. WebSocket.prototype.onclose = function () {
  725. throw new Error('onclose not implemented.');
  726. };
  727. }
  728. })
  729. NativeDataAdapter = new NativeDataCache();
  730. NativeCallback = new CallbackApi();
  731. return new NativeApi();
  732. })