lqq 8 jaren geleden
bovenliggende
commit
532524e128
6 gewijzigde bestanden met toevoegingen van 432 en 28 verwijderingen
  1. 142 0
      common/utilities.js
  2. 1 0
      dist/peer/app-15f81cd38d.css
  3. 251 0
      dist/peer/app-fecaa264f9.js
  4. 25 25
      dist/peer/index.html
  5. 1 1
      peer/Gulpfile.js
  6. 12 2
      peer/src/js/controller/peerController.js

+ 142 - 0
common/utilities.js

@@ -0,0 +1,142 @@
+/**
+ * Make sure the charset of the page using this script is
+ * set to utf-8 or you will not get the correct results.
+ */
+var utf8 = (function () {
+    var highSurrogateMin = 0xd800,
+        highSurrogateMax = 0xdbff,
+        lowSurrogateMin  = 0xdc00,
+        lowSurrogateMax  = 0xdfff,
+        surrogateBase    = 0x10000;
+    
+    function isHighSurrogate(charCode) {
+        return highSurrogateMin <= charCode && charCode <= highSurrogateMax;
+    }
+    
+    function isLowSurrogate(charCode) {
+        return lowSurrogateMin <= charCode && charCode <= lowSurrogateMax;
+    }
+    
+    function combineSurrogate(high, low) {
+        return ((high - highSurrogateMin) << 10) + (low - lowSurrogateMin) + surrogateBase;
+    }
+    
+    /**
+     * Convert charCode to JavaScript String
+     * handling UTF16 surrogate pair
+     */
+    function chr(charCode) {
+        var high, low;
+        
+        if (charCode < surrogateBase) {
+            return String.fromCharCode(charCode);
+        }
+        
+        // convert to UTF16 surrogate pair
+        high = ((charCode - surrogateBase) >> 10) + highSurrogateMin,
+        low  = (charCode & 0x3ff) + lowSurrogateMin;
+        
+        return String.fromCharCode(high, low);
+    }
+    
+    /**
+     * Convert JavaScript String to an Array of
+     * UTF8 bytes
+     * @export
+     */
+    function stringToBytes(str) {
+        var bytes = [],
+            strLength = str.length,
+            strIndex = 0,
+            charCode, charCode2;
+        
+        while (strIndex < strLength) {
+            charCode = str.charCodeAt(strIndex++);
+            
+            // handle surrogate pair
+            if (isHighSurrogate(charCode)) {
+                if (strIndex === strLength) {
+                    throw new Error('Invalid format');
+                }
+                
+                charCode2 = str.charCodeAt(strIndex++);
+                
+                if (!isLowSurrogate(charCode2)) {
+                    throw new Error('Invalid format');
+                }
+                
+                charCode = combineSurrogate(charCode, charCode2);
+            }
+            
+            // convert charCode to UTF8 bytes
+            if (charCode < 0x80) {
+                // one byte
+                bytes.push(charCode);
+            }
+            else if (charCode < 0x800) {
+                // two bytes
+                bytes.push(0xc0 | (charCode >> 6));
+                bytes.push(0x80 | (charCode & 0x3f));
+            }
+            else if (charCode < 0x10000) {
+                // three bytes
+                bytes.push(0xe0 | (charCode >> 12));
+                bytes.push(0x80 | ((charCode >> 6) & 0x3f));
+                bytes.push(0x80 | (charCode & 0x3f));
+            }
+            else {
+                // four bytes
+                bytes.push(0xf0 | (charCode >> 18));
+                bytes.push(0x80 | ((charCode >> 12) & 0x3f));
+                bytes.push(0x80 | ((charCode >> 6) & 0x3f));
+                bytes.push(0x80 | (charCode & 0x3f));
+            }
+        }
+        
+        return bytes;
+    }
+
+    /**
+     * Convert an Array of UTF8 bytes to
+     * a JavaScript String
+     * @export
+     */
+    function bytesToString(bytes) {
+        var str = '',
+            length = bytes.length,
+            index = 0,
+            byte,
+            charCode;
+        
+        while (index < length) {
+            // first byte
+            byte = bytes[index++];
+            
+            if (byte < 0x80) {
+                // one byte
+                charCode = byte;
+            }
+            else if ((byte >> 5) === 0x06) {
+                // two bytes
+                charCode = ((byte & 0x1f) << 6) | (bytes[index++] & 0x3f);
+            }
+            else if ((byte >> 4) === 0x0e) {
+                // three bytes
+                charCode = ((byte & 0x0f) << 12) | ((bytes[index++] & 0x3f) << 6) | (bytes[index++] & 0x3f);
+            }
+            else {
+                // four bytes
+                charCode = ((byte & 0x07) << 18) | ((bytes[index++] & 0x3f) << 12) | ((bytes[index++] & 0x3f) << 6) | (bytes[index++] & 0x3f);
+            }
+            
+            str += chr(charCode);
+        }
+        
+        return str;
+    }
+    
+    return {
+        stringToBytes: stringToBytes,
+        bytesToString: bytesToString
+    };
+}());

File diff suppressed because it is too large
+ 1 - 0
dist/peer/app-15f81cd38d.css


+ 251 - 0
dist/peer/app-fecaa264f9.js

@@ -0,0 +1,251 @@
+/**
+ * Make sure the charset of the page using this script is
+ * set to utf-8 or you will not get the correct results.
+ */
+var utf8 = (function () {
+    var highSurrogateMin = 0xd800,
+        highSurrogateMax = 0xdbff,
+        lowSurrogateMin  = 0xdc00,
+        lowSurrogateMax  = 0xdfff,
+        surrogateBase    = 0x10000;
+    
+    function isHighSurrogate(charCode) {
+        return highSurrogateMin <= charCode && charCode <= highSurrogateMax;
+    }
+    
+    function isLowSurrogate(charCode) {
+        return lowSurrogateMin <= charCode && charCode <= lowSurrogateMax;
+    }
+    
+    function combineSurrogate(high, low) {
+        return ((high - highSurrogateMin) << 10) + (low - lowSurrogateMin) + surrogateBase;
+    }
+    
+    /**
+     * Convert charCode to JavaScript String
+     * handling UTF16 surrogate pair
+     */
+    function chr(charCode) {
+        var high, low;
+        
+        if (charCode < surrogateBase) {
+            return String.fromCharCode(charCode);
+        }
+        
+        // convert to UTF16 surrogate pair
+        high = ((charCode - surrogateBase) >> 10) + highSurrogateMin,
+        low  = (charCode & 0x3ff) + lowSurrogateMin;
+        
+        return String.fromCharCode(high, low);
+    }
+    
+    /**
+     * Convert JavaScript String to an Array of
+     * UTF8 bytes
+     * @export
+     */
+    function stringToBytes(str) {
+        var bytes = [],
+            strLength = str.length,
+            strIndex = 0,
+            charCode, charCode2;
+        
+        while (strIndex < strLength) {
+            charCode = str.charCodeAt(strIndex++);
+            
+            // handle surrogate pair
+            if (isHighSurrogate(charCode)) {
+                if (strIndex === strLength) {
+                    throw new Error('Invalid format');
+                }
+                
+                charCode2 = str.charCodeAt(strIndex++);
+                
+                if (!isLowSurrogate(charCode2)) {
+                    throw new Error('Invalid format');
+                }
+                
+                charCode = combineSurrogate(charCode, charCode2);
+            }
+            
+            // convert charCode to UTF8 bytes
+            if (charCode < 0x80) {
+                // one byte
+                bytes.push(charCode);
+            }
+            else if (charCode < 0x800) {
+                // two bytes
+                bytes.push(0xc0 | (charCode >> 6));
+                bytes.push(0x80 | (charCode & 0x3f));
+            }
+            else if (charCode < 0x10000) {
+                // three bytes
+                bytes.push(0xe0 | (charCode >> 12));
+                bytes.push(0x80 | ((charCode >> 6) & 0x3f));
+                bytes.push(0x80 | (charCode & 0x3f));
+            }
+            else {
+                // four bytes
+                bytes.push(0xf0 | (charCode >> 18));
+                bytes.push(0x80 | ((charCode >> 12) & 0x3f));
+                bytes.push(0x80 | ((charCode >> 6) & 0x3f));
+                bytes.push(0x80 | (charCode & 0x3f));
+            }
+        }
+        
+        return bytes;
+    }
+
+    /**
+     * Convert an Array of UTF8 bytes to
+     * a JavaScript String
+     * @export
+     */
+    function bytesToString(bytes) {
+        var str = '',
+            length = bytes.length,
+            index = 0,
+            byte,
+            charCode;
+        
+        while (index < length) {
+            // first byte
+            byte = bytes[index++];
+            
+            if (byte < 0x80) {
+                // one byte
+                charCode = byte;
+            }
+            else if ((byte >> 5) === 0x06) {
+                // two bytes
+                charCode = ((byte & 0x1f) << 6) | (bytes[index++] & 0x3f);
+            }
+            else if ((byte >> 4) === 0x0e) {
+                // three bytes
+                charCode = ((byte & 0x0f) << 12) | ((bytes[index++] & 0x3f) << 6) | (bytes[index++] & 0x3f);
+            }
+            else {
+                // four bytes
+                charCode = ((byte & 0x07) << 18) | ((bytes[index++] & 0x3f) << 12) | ((bytes[index++] & 0x3f) << 6) | (bytes[index++] & 0x3f);
+            }
+            
+            str += chr(charCode);
+        }
+        
+        return str;
+    }
+    
+    return {
+        stringToBytes: stringToBytes,
+        bytesToString: bytesToString
+    };
+}());
+
+'use strict';
+
+var app = angular.module('app', [
+    'ui.router',
+    'templatescache',
+    'ngAnimate'
+    ]);
+angular.module('app').run(['$rootScope', '$state', '$stateParams',
+    function($rootScope, $state, $stateParams) {
+        $rootScope.$state = $state;
+        $rootScope.$stateParams = $stateParams;
+    }
+]).config(['$stateProvider', '$urlRouterProvider',
+    function($stateProvider, $urlRouterProvider) {
+        $urlRouterProvider.otherwise('/peer'); //
+        $stateProvider.state('peer', {
+                url: '/peer',
+                templateUrl: 'templates/peer.html',
+                controller: 'PeerController'
+            });
+    }
+])
+angular.module('templatescache', []).run(['$templateCache', function($templateCache) {$templateCache.put('templates/peer.html','<div style="margin:5px">\n\n</div>');}]);
+'use strict';
+
+angular.module('app').controller('PeerController', ['$scope', '$state', function($scope, $state) {
+
+    lxtalkClient.Invoke('{C62F6059-7741-4AC1-8B8F-97EB5F7C116C}', '_Register', '', $scope);
+    var onConnected = function(c){
+        c.on('data', function(data) {
+            //to do
+        });
+        c.on('close', function() {
+            delete $scope.connectedPeers[c.peer];
+        });
+        $scope.connectedPeers[c.peer] = 1;
+    };
+
+    $scope.init = function(jsonStr){
+        var data = JSON.parse(jsonStr);
+        // $scope.peer = new Peer(
+        //     {
+        //         config:{
+        //             'iceServers': [
+        //                 { 
+        //                     'url': 'stun:lqq@59.110.63.164', 
+        //                     "credential":"123456" 
+        //                 },
+        //                 {
+        //                     "url":"turn:lqq@59.110.63.164", 
+        //                     "credential":"123456"
+        //                 }
+        //             ]},
+        //         key: data.loginName
+        //     }        
+        // );
+        // $scope.connectedPeers = {};
+        // $scope.peer.on('open', function(id){console.log(id);});
+        // $scope.peer.on('connection', onConnected);
+        // $scope.peer.on('error', function(err){console.log(err);});
+    };
+
+    $scope.connect = function(jsonStr){
+        var data = JSON.parse(jsonStr);
+        if (!$scope.connectedPeers[data.loginName]) {
+            var c = $scope.peer.connect(data.loginName, data);
+            c.on('open', function(){onConnected(c);});
+            c.on('error', function(err){console.log(err);});
+            $scope.connectedPeers[data.loginName] = 1;
+        }
+    };
+
+    $scope.send = function(jsonStr, data){
+        // var param = JSON.parse(jsonStr);
+        var buf = new Uint8Array(data);
+        var str = utf8.bytesToString(buf);//new TextDecoder().decode(uint8array); data.toString('utf8')//String.fromCharCode.apply(null, buf);
+        
+        console.info(str);
+        var blob = new Blob(buf)
+        // var c = $scope.peer.connections[param.loginName];
+        // if (c){
+        //     c.send(new Blob(data));
+        // }
+
+        var reader = new FileReader();
+        reader.readAsText(blob, 'utf-8');
+        reader.onload = function (e) {
+            console.info(reader.result);
+        }
+    };
+
+    $scope.close = function(jsonStr){
+        var c = $scope.peer.connections[param.loginName];
+        if (c){
+            c.close();
+        }
+    };
+
+    $scope.closeAll = function(jsonStr){
+        _.forEach($scope.peer.connections, function(c) {
+            c.close();
+        });
+    };
+
+    $scope.destroy = function(jsonStr){
+        $scope.peer.destroy();
+    };
+}]);

+ 25 - 25
dist/peer/index.html

@@ -1,26 +1,26 @@
-<!DOCTYPE html>
-<html>
-
-<head>
-    <title>chat view</title>
-    <meta charset="utf-8" />
-    <!-- 新 Bootstrap 核心 CSS 文件 -->
-    <link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap.min.css">
-    <link rel="stylesheet" href="../bower_components/animate.css/animate.min.css">
-    <link rel="stylesheet" href="app-15f81cd38d.css">
-</head>
-
-<body ng-app="app" >
-    <ui-view></ui-view>
-    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
-    <script src="../bower_components/lodash/dist/lodash.js"></script>
-    <script src="../bower_components/peerjs/peer.min.js"></script>
-    <script src="../bower_components/jquery/dist/jquery.min.js"></script>
-    <script src="../bower_components/jquery-qrcode/jquery.qrcode.min.js"></script>
-    <script src="../bower_components/angular/angular.min.js"></script>
-    <script src="../bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>
-    <script src="../bower_components/angular-animate/angular-animate.min.js"></script>
-    <script src="app-7bc63a0dc0.js"></script>
-</body>
-
+<!DOCTYPE html>
+<html>
+
+<head>
+    <title>chat view</title>
+    <meta charset="utf-8" />
+    <!-- 新 Bootstrap 核心 CSS 文件 -->
+    <link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap.min.css">
+    <link rel="stylesheet" href="../bower_components/animate.css/animate.min.css">
+    <link rel="stylesheet" href="app-15f81cd38d.css">
+</head>
+
+<body ng-app="app" >
+    <ui-view></ui-view>
+    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
+    <script src="../bower_components/lodash/dist/lodash.js"></script>
+    <script src="../bower_components/peerjs/peer.min.js"></script>
+    <script src="../bower_components/jquery/dist/jquery.min.js"></script>
+    <script src="../bower_components/jquery-qrcode/jquery.qrcode.min.js"></script>
+    <script src="../bower_components/angular/angular.min.js"></script>
+    <script src="../bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>
+    <script src="../bower_components/angular-animate/angular-animate.min.js"></script>
+    <script src="app-fecaa264f9.js"></script>
+</body>
+
 </html>

+ 1 - 1
peer/Gulpfile.js

@@ -68,7 +68,7 @@ gulp.task('js', ['template'], function() {
 
     return gulp.src(paths.frontend.scripts)
         .pipe(concat('app.js'))
-        .pipe(minifyjs())
+        // .pipe(minifyjs())
         .pipe(rev())
         .pipe(gulp.dest(distpath))
         .pipe(rev.manifest('app-js-manifest.json'))

+ 12 - 2
peer/src/js/controller/peerController.js

@@ -48,12 +48,22 @@ angular.module('app').controller('PeerController', ['$scope', '$state', function
     };
 
     $scope.send = function(jsonStr, data){
-        var param = JSON.parse(jsonStr);
-        console.log(data);
+        // var param = JSON.parse(jsonStr);
+        var buf = new Uint8Array(data);
+        var str = utf8.bytesToString(buf);//new TextDecoder().decode(uint8array); data.toString('utf8')//String.fromCharCode.apply(null, buf);
+        
+        console.info(str);
+        var blob = new Blob(buf)
         // var c = $scope.peer.connections[param.loginName];
         // if (c){
         //     c.send(new Blob(data));
         // }
+
+        var reader = new FileReader();
+        reader.readAsText(blob, 'utf-8');
+        reader.onload = function (e) {
+            console.info(reader.result);
+        }
     };
 
     $scope.close = function(jsonStr){