app-9a1ffb26eb.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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', '$timeout', function ($scope, $state, $timeout) {
  146. lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', '_Register', '', $scope);
  147. var onConnected = function (c) {
  148. c.on('data', function (data) {
  149. // console.log('接收数据:', data.byteLength);
  150. lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', 'OnRecv', data, $scope);
  151. });
  152. c.on('close', function () {
  153. delete $scope.connectedPeers[c.peer];
  154. });
  155. $scope.connectedPeers[c.peer] = 1;
  156. lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', 'OnConnected', '{"loginName":"'+c.peer+'"}', $scope);
  157. };
  158. $scope.options = {
  159. host: '120.26.136.253',
  160. port: 9000,
  161. debug: 3,
  162. config: {
  163. 'iceServers': [{
  164. 'url': 'stun:lqq@59.110.63.164',
  165. "credential": "123456"
  166. },
  167. {
  168. "url": "turn:lqq@59.110.63.164",
  169. "credential": "123456"
  170. }
  171. ]
  172. },
  173. key: 'peerjs',
  174. logFunction: function () {
  175. var copy = Array.prototype.slice.call(arguments).join(' ');
  176. console.info(copy);
  177. }
  178. }
  179. $scope.init = function (jsonStr) {
  180. console.log(jsonStr);
  181. var data = JSON.parse(jsonStr);
  182. $scope.loginName = data.loginName;
  183. $scope.peer = new Peer(
  184. $scope.loginName,
  185. $scope.options
  186. );
  187. $scope.connectedPeers = {};
  188. $scope.peer.on('open', function (id) {
  189. console.log(id);
  190. });
  191. $scope.peer.on('connection', function(c){
  192. c.on('open', function () {
  193. onConnected(c);
  194. });
  195. });
  196. $scope.peer.on('error', function (err) {
  197. $timeout(function () {
  198. $scope.init(jsonStr);
  199. }, 50000);
  200. });
  201. };
  202. $scope.connect = function (jsonStr) {
  203. var data = JSON.parse(jsonStr);
  204. data = _.assign(data, {
  205. label: 'file',
  206. serialization:'binary'
  207. });
  208. console.info(data);
  209. if (!$scope.connectedPeers[data.loginName]) {
  210. var c = $scope.peer.connect(data.loginName, data);
  211. c.on('open', function () {
  212. onConnected(c);
  213. });
  214. c.on('error', function (err) {
  215. console.log(err);
  216. lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', 'OnDisconnected', '{"loginName":"'+c.peer+'"}', $scope);
  217. });
  218. $scope.connectedPeers[data.loginName] = 1;
  219. }
  220. };
  221. $scope.send = function (jsonStr, data) {
  222. var param = JSON.parse(jsonStr);
  223. data = data.slice(0);
  224. // console.log('发送数据:', data.byteLength);
  225. var c = $scope.peer.connections[param.loginName];
  226. if (c && c[0]) {
  227. c[0].send(data);
  228. }
  229. else{
  230. lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', 'OnDisconnected', '{"loginName":"'+param.loginName+'"}', $scope);
  231. }
  232. };
  233. $scope.close = function (jsonStr) {
  234. var c = $scope.peer.connections[param.loginName];
  235. if (c) {
  236. c.close();
  237. }
  238. };
  239. $scope.closeAll = function (jsonStr) {
  240. _.forEach($scope.peer.connections, function (c) {
  241. c.close();
  242. });
  243. };
  244. $scope.destroy = function (jsonStr) {
  245. $scope.peer.destroy();
  246. };
  247. }]);