Skip to content

Instantly share code, notes, and snippets.

@formula1
Created April 6, 2015 04:36
Show Gist options
  • Save formula1/8dc1377ae98d55fe981d to your computer and use it in GitHub Desktop.
Save formula1/8dc1377ae98d55fe981d to your computer and use it in GitHub Desktop.
Angular2 + Icecomm if angular could handle setTimeouts
/* jshint esnext: true */
import {Component, Template, NgElement, For, If, bootstrap} from 'angular2/angular2';
// Annotation section
@Component({
selector: 'ice-comm'
})
@Template({
inline: `
<div>
<button (click)="close()">Close</button>
<input
template="if: !current_room"
#room type="text"
(keyup)="roomEvent($event, room)"
/>
<div template="if: !!local" >
<video autoplay [src]="local.stream" ></video>
</div>
<ul class="peer_list" template="if: !!peers && peers.length > 0">
<li *for="#p of peers" >
<video autoplay [src]="p.stream" ></video>
</li>
</ul>
</div>
`,
directives: [For, If]
})
// Component controller
class IceCommComponent {
constructor(el: NgElement) {
this.peers = [];
var _this = this;
this.comm = new Icecomm( el.getAttribute("apikey"); );
this.comm.on("local",function(peer){
console.log("on local");
_this.local = peer;
});
this.comm.on("connected", function(peer){
console.log("on connect");
_this.peers.push(peer);
});
this.comm.on("disconnect", function(peer){
_this.peers.splice(_this.peers.indexOf(peer),1);
});
var room = el.getAttribute("room");
if(!!room) this.enterRoom(room);
}
roomEvent(e,room){
if(e.which !== 13) return;
this.enterRoom(room.value);
room.value = "";
}
enterRoom(room){
if(!!this.current_room){
throw new Error("already in a room");
}
this.current_room = room;
this.comm.connect(room, {audio: false});
}
close(){
this.comm.leave();
}
};
bootstrap(IceCommComponent);
<!-- index.html -->
<html>
<head>
<title>Angular 2 Quickstart</title>
<script src="/dist/es6-shim.js"></script>
<script src="http://cdn.icecomm.io/icecomm.js" ></script>
<script>
//Related to issue below
//https://github.com/angular/angular/issues/1050
</script>
</head>
<body>
<!-- The app component created in app.es6 -->
<ice-comm apikey="hKtqGYhzfZCoXXTn5X.XhbswJ7ECxIto51Q65AxOJBqeVP45ha" room="custom room"></ice-comm>
<script type="text/javascript" >
// Rewrite the paths to load the files
System.paths = {
'angular2/*':'/angular2/*.js', // Angular
'rtts_assert/*': '/rtts_assert/*.js', // Runtime assertions
'app': 'icecomm.es6' // The my-app component
};
// Kick off the application
System.import('app');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment