Skip to content

Instantly share code, notes, and snippets.

@formula1
Last active August 29, 2015 14:18
Show Gist options
  • Save formula1/a9dedaf1160881575141 to your computer and use it in GitHub Desktop.
Save formula1/a9dedaf1160881575141 to your computer and use it in GitHub Desktop.
Angular 1 + icecomm
//<input ng-model="foo">
//https://docs.angularjs.org/guide/directive
angular.module('IceComm',[])
.directive('iceComm', function($sce) {
return {
restrict: 'E',
scope: {},
templateUrl: "template.html",
link: function($scope, ele, atts) {
console.log("link");
var comm = new Icecomm( atts.apikey );
$scope.peers = [];
comm.on("local",function(peer){
$scope.$apply(function () {
peer.stream = $sce.trustAsResourceUrl(peer.stream);
$scope.local = peer;
});
});
comm.on("connected", function(peer){
$scope.$apply(function () {
peer.stream = $sce.trustAsResourceUrl(peer.stream);
$scope.peers.push(peer);
});
});
comm.on("disconnect", function(peer){
$scope.$apply(function () {
$scope.peers.splice($scope.peers.indexOf(peer),1);
});
});
$scope.connect = function(room){
if($scope.current_room) throw new Error("You already have a room");
$scope.current_room = room;
comm.connect(room, {audio: false});
};
$scope.close = function(){
comm.leave();
};
$scope.roomEvent = function(e,value){
if(e.which !== 13) return;
$scope.connect(room.value);
room.value = "";
};
ele.find("button.close").bind("click",$scope.close);
ele.on('$destroy', $scope.close);
if(atts.room){
$scope.connect(atts.room);
}
}
};
});
<!doctype html>
<html>
<head>
<script src="http://cdn.icecomm.io/icecomm.js" ></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js" ></script>
</head>
<body ng-app="Main">
<ice-comm
apikey="hKtqGYhzfZCoXXTn5X.XhbswJ7ECxIto51Q65AxOJBqeVP45ha"
room="custom room"
></ice-comm>
<script src="icecomm.directive.js" ></script>
<script type="text/javascript" >
angular.module("Main", [ "IceComm"]);
</script>
</body>
</html>
<div>
<button ng-if="!!current_room" ng-click="close()">Close</button>
<input ng-if="!current_room" type="text" ng-keyup="roomEvent()" />
<div ng-if="!!local">
<video autoplay class="local" ng-src={{local.stream}}></video>
</div>
<ul><li ng-repeat="peer in peers">
<video autoplay ng-src="{{peer.stream}}" ></video>
</li></ul>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment