1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-30 04:23:07 +03:00

Add noddy voip example app.

This commit is contained in:
Kegan Dougal
2015-07-14 17:11:30 +01:00
parent 053a5b1bea
commit e3fdcaaff5
5 changed files with 129 additions and 4 deletions

9
examples/voip/README.md Normal file
View File

@ -0,0 +1,9 @@
To try it out, **you must build the SDK first** and then host this folder:
```
$ npm run build
$ cd examples/browser
$ python -m SimpleHTTPServer 8003
```
Then visit ``http://localhost:8003``.

View File

@ -0,0 +1,89 @@
"use strict";
console.log("Loading browser sdk");
var BASE_URL = "https://matrix.org";
var TOKEN = "accesstokengoeshere";
var USER_ID = "@username:localhost";
var ROOM_ID = "!room:id";
var client = matrixcs.createClient({
baseUrl: BASE_URL,
accessToken: TOKEN,
userId: USER_ID
});
var call;
function disableButtons(place, answer, hangup) {
document.getElementById("hangup").disabled = hangup;
document.getElementById("answer").disabled = answer;
document.getElementById("call").disabled = place;
}
function addListeners(call) {
var lastError = "";
call.on("hangup", function() {
disableButtons(false, true, true);
document.getElementById("result").innerHTML = (
"<p>Call ended. Last error: "+lastError+"</p>"
);
});
call.on("error", function(err) {
lastError = err.message;
call.hangup();
disableButtons(false, true, true);
});
}
window.onload = function() {
document.getElementById("result").innerHTML = "<p>Please wait. Syncing...</p>";
document.getElementById("config").innerHTML = "<p>" +
"Homeserver: <code>"+BASE_URL+"</code><br/>"+
"Room: <code>"+ROOM_ID+"</code><br/>"+
"User: <code>"+USER_ID+"</code><br/>"+
"</p>";
disableButtons(true, true, true);
};
client.on("syncComplete", function () {
document.getElementById("result").innerHTML = "<p>Ready for calls.</p>";
disableButtons(false, true, true);
document.getElementById("call").onclick = function() {
console.log("Placing call...");
call = matrixcs.createNewMatrixCall(
client, ROOM_ID
);
console.log("Call => %s", call);
addListeners(call);
call.placeVideoCall(
document.getElementById("remote"),
document.getElementById("local")
);
document.getElementById("result").innerHTML = "<p>Placed call.</p>";
disableButtons(true, true, false);
};
document.getElementById("hangup").onclick = function() {
console.log("Hanging up call...");
console.log("Call => %s", call);
call.hangup();
document.getElementById("result").innerHTML = "<p>Hungup call.</p>";
};
document.getElementById("answer").onclick = function() {
console.log("Answering call...");
console.log("Call => %s", call);
call.answer();
disableButtons(true, true, false);
document.getElementById("result").innerHTML = "<p>Answered call.</p>";
};
client.on("Call.incoming", function(c) {
console.log("Call ringing");
disableButtons(true, false, false);
document.getElementById("result").innerHTML = "<p>Incoming call...</p>";
call = c;
addListeners(call);
});
});
client.startClient();

26
examples/voip/index.html Normal file
View File

@ -0,0 +1,26 @@
<html>
<head>
<title>VoIP Test</title>
<script src="lib/matrix.js"></script>
<script src="browserTest.js"></script>
</head>
<body>
You can place and receive calls with this example. Make sure to edit the
constants in <code>browserTest.js</code> first.
<div id="config"></div>
<div id="result"></div>
<button id="call">Place Call</button>
<button id="answer">Answer Call</button>
<button id="hangup">Hangup Call</button>
<div id="videoBackground">
<div id="videoContainer">
<video id="remote"></video>
</div>
</div>
<div id="videoBackground">
<div id="videoContainer">
<video id="local"></video>
</div>
</div>
</body>
</html>

1
examples/voip/lib/matrix.js Symbolic link
View File

@ -0,0 +1 @@
../../../dist/browser-matrix-dev.js

View File

@ -7,7 +7,7 @@ var utils = require("../utils");
var EventEmitter = require("events").EventEmitter; var EventEmitter = require("events").EventEmitter;
var DEBUG = true; // set true to enable console logging. var DEBUG = true; // set true to enable console logging.
// events: onHangup, error, replaced // events: hangup, error, replaced
/** /**
* Construct a new Matrix Call. * Construct a new Matrix Call.
@ -147,7 +147,7 @@ MatrixCall.prototype._initWithInvite = function(event) {
if (self.peerConn.signalingState != 'closed') { if (self.peerConn.signalingState != 'closed') {
self.peerConn.close(); self.peerConn.close();
} }
self.emit("onHangup", self); self.emit("hangup", self);
} }
}, this.msg.lifetime - event.getAge()); }, this.msg.lifetime - event.getAge());
} }
@ -568,7 +568,7 @@ MatrixCall.prototype._onRemoteStreamEnded = function(event) {
if (this.peerConn.signalingState != 'closed') { if (this.peerConn.signalingState != 'closed') {
this.peerConn.close(); this.peerConn.close();
} }
this.emit("onHangup", this); this.emit("hangup", this);
}; };
/** /**
@ -638,7 +638,7 @@ var terminate = function(self, hangupParty, hangupReason, shouldEmit) {
self.peerConn.close(); self.peerConn.close();
} }
if (shouldEmit) { if (shouldEmit) {
self.emit("onHangup", self); self.emit("hangup", self);
} }
}; };