UNPKG

2.15 kBJavaScriptView Raw
1var server = holla.connect();
2
3var sendChat = function(){
4 var msg = $("#whatSay").val();
5 if (msg === "") return;
6 this.chat(msg);
7 $("#chat").append("<b>"+server.user+"</b>: " + msg + "<br/>");
8 $("#whatSay").val('');
9};
10
11var handleChat = function(msg){
12 $("#chat").append("<b>"+this.user+"</b>: " + msg + "<br/>");
13};
14
15$(function(){
16 $("#me").hide();
17 $("#them").hide();
18 $("#whoCall").hide();
19 $("#hangup").hide();
20 $("#messages").hide();
21
22 server.on("presence", function(user){
23 if (user.online) {
24 console.log(user.name + " is online.");
25 } else {
26 console.log(user.name + " is offline.");
27 }
28 });
29
30 $("#whoAmI").change(function(){
31 var name = $("#whoAmI").val();
32 $("#me").show();
33 $("#them").show();
34 $("#whoAmI").remove();
35 $("#whoCall").show();
36 $("#hangup").show();
37 $("#messages").show();
38
39 holla.createFullStream(function(err, stream) {
40 holla.pipe(stream, $("#me"));
41
42 // accept inbound
43 server.identify(name, function(worked) {
44 server.on("call", function(call) {
45 console.log("Inbound call", call);
46
47 call.addStream(stream);
48 call.answer();
49
50 call.ready(function(stream) {
51 holla.pipe(stream, $("#them"));
52 });
53 call.on("hangup", function() {
54 $("#them").attr('src', '');
55 });
56 $("#hangup").click(function(){
57 call.end();
58 });
59
60 $("#whatSay").change(sendChat.bind(call));
61 call.on("chat", handleChat.bind(call));
62 });
63
64 //place outbound
65 $("#whoCall").change(function(){
66 var toCall = $("#whoCall").val();
67 var call = server.call(toCall);
68 call.addStream(stream);
69 call.ready(function(stream) {
70 holla.pipe(stream, $("#them"));
71 });
72 call.on("hangup", function() {
73 $("#them").attr('src', '');
74 });
75 $("#hangup").click(function(){
76 call.end();
77 });
78 $("#whatSay").change(sendChat.bind(call));
79 call.on("chat", handleChat.bind(call));
80 });
81
82 });
83 });
84
85 });
86});
\No newline at end of file