app-fecaa264f9.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * Make sure the charset of the page using this script is
  3. * set to utf-8 or you will not get the correct results.
  4. */
  5. var utf8 = (function () {
  6. var highSurrogateMin = 0xd800,
  7. highSurrogateMax = 0xdbff,
  8. lowSurrogateMin = 0xdc00,
  9. lowSurrogateMax = 0xdfff,
  10. surrogateBase = 0x10000;
  11. function isHighSurrogate(charCode) {
  12. return highSurrogateMin <= charCode && charCode <= highSurrogateMax;
  13. }
  14. function isLowSurrogate(charCode) {
  15. return lowSurrogateMin <= charCode && charCode <= lowSurrogateMax;
  16. }
  17. function combineSurrogate(high, low) {
  18. return ((high - highSurrogateMin) << 10) + (low - lowSurrogateMin) + surrogateBase;
  19. }
  20. /**
  21. * Convert charCode to JavaScript String
  22. * handling UTF16 surrogate pair
  23. */
  24. function chr(charCode) {
  25. var high, low;
  26. if (charCode < surrogateBase) {
  27. return String.fromCharCode(charCode);
  28. }
  29. // convert to UTF16 surrogate pair
  30. high = ((charCode - surrogateBase) >> 10) + highSurrogateMin,
  31. low = (charCode & 0x3ff) + lowSurrogateMin;
  32. return String.fromCharCode(high, low);
  33. }
  34. /**
  35. * Convert JavaScript String to an Array of
  36. * UTF8 bytes
  37. * @export
  38. */
  39. function stringToBytes(str) {
  40. var bytes = [],
  41. strLength = str.length,
  42. strIndex = 0,
  43. charCode, charCode2;
  44. while (strIndex < strLength) {
  45. charCode = str.charCodeAt(strIndex++);
  46. // handle surrogate pair
  47. if (isHighSurrogate(charCode)) {
  48. if (strIndex === strLength) {
  49. throw new Error('Invalid format');
  50. }
  51. charCode2 = str.charCodeAt(strIndex++);
  52. if (!isLowSurrogate(charCode2)) {
  53. throw new Error('Invalid format');
  54. }
  55. charCode = combineSurrogate(charCode, charCode2);
  56. }
  57. // convert charCode to UTF8 bytes
  58. if (charCode < 0x80) {
  59. // one byte
  60. bytes.push(charCode);
  61. }
  62. else if (charCode < 0x800) {
  63. // two bytes
  64. bytes.push(0xc0 | (charCode >> 6));
  65. bytes.push(0x80 | (charCode & 0x3f));
  66. }
  67. else if (charCode < 0x10000) {
  68. // three bytes
  69. bytes.push(0xe0 | (charCode >> 12));
  70. bytes.push(0x80 | ((charCode >> 6) & 0x3f));
  71. bytes.push(0x80 | (charCode & 0x3f));
  72. }
  73. else {
  74. // four bytes
  75. bytes.push(0xf0 | (charCode >> 18));
  76. bytes.push(0x80 | ((charCode >> 12) & 0x3f));
  77. bytes.push(0x80 | ((charCode >> 6) & 0x3f));
  78. bytes.push(0x80 | (charCode & 0x3f));
  79. }
  80. }
  81. return bytes;
  82. }
  83. /**
  84. * Convert an Array of UTF8 bytes to
  85. * a JavaScript String
  86. * @export
  87. */
  88. function bytesToString(bytes) {
  89. var str = '',
  90. length = bytes.length,
  91. index = 0,
  92. byte,
  93. charCode;
  94. while (index < length) {
  95. // first byte
  96. byte = bytes[index++];
  97. if (byte < 0x80) {
  98. // one byte
  99. charCode = byte;
  100. }
  101. else if ((byte >> 5) === 0x06) {
  102. // two bytes
  103. charCode = ((byte & 0x1f) << 6) | (bytes[index++] & 0x3f);
  104. }
  105. else if ((byte >> 4) === 0x0e) {
  106. // three bytes
  107. charCode = ((byte & 0x0f) << 12) | ((bytes[index++] & 0x3f) << 6) | (bytes[index++] & 0x3f);
  108. }
  109. else {
  110. // four bytes
  111. charCode = ((byte & 0x07) << 18) | ((bytes[index++] & 0x3f) << 12) | ((bytes[index++] & 0x3f) << 6) | (bytes[index++] & 0x3f);
  112. }
  113. str += chr(charCode);
  114. }
  115. return str;
  116. }
  117. return {
  118. stringToBytes: stringToBytes,
  119. bytesToString: bytesToString
  120. };
  121. }());
  122. 'use strict';
  123. var app = angular.module('app', [
  124. 'ui.router',
  125. 'templatescache',
  126. 'ngAnimate'
  127. ]);
  128. angular.module('app').run(['$rootScope', '$state', '$stateParams',
  129. function($rootScope, $state, $stateParams) {
  130. $rootScope.$state = $state;
  131. $rootScope.$stateParams = $stateParams;
  132. }
  133. ]).config(['$stateProvider', '$urlRouterProvider',
  134. function($stateProvider, $urlRouterProvider) {
  135. $urlRouterProvider.otherwise('/peer'); //
  136. $stateProvider.state('peer', {
  137. url: '/peer',
  138. templateUrl: 'templates/peer.html',
  139. controller: 'PeerController'
  140. });
  141. }
  142. ])
  143. angular.module('templatescache', []).run(['$templateCache', function($templateCache) {$templateCache.put('templates/peer.html','<div style="margin:5px">\n\n</div>');}]);
  144. 'use strict';
  145. angular.module('app').controller('PeerController', ['$scope', '$state', function($scope, $state) {
  146. lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', '_Register', '', $scope);
  147. var onConnected = function(c){
  148. c.on('data', function(data) {
  149. //to do
  150. });
  151. c.on('close', function() {
  152. delete $scope.connectedPeers[c.peer];
  153. });
  154. $scope.connectedPeers[c.peer] = 1;
  155. };
  156. $scope.init = function(jsonStr){
  157. var data = JSON.parse(jsonStr);
  158. // $scope.peer = new Peer(
  159. // {
  160. // config:{
  161. // 'iceServers': [
  162. // {
  163. // 'url': 'stun:lqq@59.110.63.164',
  164. // "credential":"123456"
  165. // },
  166. // {
  167. // "url":"turn:lqq@59.110.63.164",
  168. // "credential":"123456"
  169. // }
  170. // ]},
  171. // key: data.loginName
  172. // }
  173. // );
  174. // $scope.connectedPeers = {};
  175. // $scope.peer.on('open', function(id){console.log(id);});
  176. // $scope.peer.on('connection', onConnected);
  177. // $scope.peer.on('error', function(err){console.log(err);});
  178. };
  179. $scope.connect = function(jsonStr){
  180. var data = JSON.parse(jsonStr);
  181. if (!$scope.connectedPeers[data.loginName]) {
  182. var c = $scope.peer.connect(data.loginName, data);
  183. c.on('open', function(){onConnected(c);});
  184. c.on('error', function(err){console.log(err);});
  185. $scope.connectedPeers[data.loginName] = 1;
  186. }
  187. };
  188. $scope.send = function(jsonStr, data){
  189. // var param = JSON.parse(jsonStr);
  190. var buf = new Uint8Array(data);
  191. var str = utf8.bytesToString(buf);//new TextDecoder().decode(uint8array); data.toString('utf8')//String.fromCharCode.apply(null, buf);
  192. console.info(str);
  193. var blob = new Blob(buf)
  194. // var c = $scope.peer.connections[param.loginName];
  195. // if (c){
  196. // c.send(new Blob(data));
  197. // }
  198. var reader = new FileReader();
  199. reader.readAsText(blob, 'utf-8');
  200. reader.onload = function (e) {
  201. console.info(reader.result);
  202. }
  203. };
  204. $scope.close = function(jsonStr){
  205. var c = $scope.peer.connections[param.loginName];
  206. if (c){
  207. c.close();
  208. }
  209. };
  210. $scope.closeAll = function(jsonStr){
  211. _.forEach($scope.peer.connections, function(c) {
  212. c.close();
  213. });
  214. };
  215. $scope.destroy = function(jsonStr){
  216. $scope.peer.destroy();
  217. };
  218. }]);