Commit 2546aae7 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Eliminate login from protocol.

The login message is replaced with handshake, which only carries
the client id.  Username and password is now in the join message.
Permissions is replaced with joined.
parent b30d4fe5
......@@ -29,7 +29,7 @@ let sc = new ServerConnection()
serverConnection.onconnected = ...;
serverConnection.onclose = ...;
serverConnection.onusermessage = ...;
serverConnection.onpermissions = ...;
serverConnection.onjoined = ...;
serverConnection.onuser = ...;
serverConnection.onchat = ...;
serverConnection.onclearchat = ...;
......@@ -55,18 +55,17 @@ You may now connect to the server.
serverConnection.connect(`wss://${location.host}/ws`);
```
You log-in, join a group and request media in the `onconnected` callback.
You typically join a group and request media in the `onconnected` callback:
```javascript
serverConnection.onconnected = function() {
this.login(username, password);
this.join(group);
this.join(group, 'join', username, password);
this.request('everything');
}
```
You should not attempt to push a stream to the server until it has granted
you the `present` permission through the `onpermissions` callback.
you the `present` permission through the `onjoined` callback.
## Managing groups and users
......
This diff is collapsed.
......@@ -108,11 +108,14 @@ function ServerConnection() {
*/
this.onuser = null;
/**
* onpermissions is called whenever the current user's permissions change
* onjoined is called whenever we join or leave a group or whenever the
* permissions we have in a group change.
*
* @type{(this: ServerConnection, permissions: Object<string,boolean>) => void}
* kind is one of 'join', 'fail', 'change' or 'leave'.
*
* @type{(this: ServerConnection, kind: string, group: string, permissions: Object<string,boolean>, message: string) => void}
*/
this.onpermissions = null;
this.onjoined = null;
/**
* ondownstream is called whenever a new down stream is added. It
* should set up the stream's callbacks; actually setting up the UI
......@@ -237,14 +240,16 @@ ServerConnection.prototype.connect = async function(url) {
reject(e);
};
this.socket.onopen = function(e) {
sc.send({
type: 'handshake',
id: sc.id,
});
if(sc.onconnected)
sc.onconnected.call(sc);
resolve(sc);
};
this.socket.onclose = function(e) {
sc.permissions = {};
if(sc.onpermissions)
sc.onpermissions.call(sc, {});
for(let id in sc.down) {
let c = sc.down[id];
delete(sc.down[id]);
......@@ -252,6 +257,9 @@ ServerConnection.prototype.connect = async function(url) {
if(c.onclose)
c.onclose.call(c);
}
if(sc.group && sc.onjoined)
sc.onjoined.call(sc, 'leave', sc.group, {}, '');
sc.group = null;
if(sc.onclose)
sc.onclose.call(sc, e.code, e.reason);
reject(new Error('websocket close ' + e.code + ' ' + e.reason));
......@@ -280,10 +288,19 @@ ServerConnection.prototype.connect = async function(url) {
case 'label':
sc.gotLabel(m.id, m.value);
break;
case 'permissions':
case 'joined':
if(sc.group) {
if(m.group !== sc.group) {
throw new Error('Joined multiple groups');
}
} else {
sc.group = m.group;
}
sc.permissions = m.permissions;
if(sc.onpermissions)
sc.onpermissions.call(sc, m.permissions);
if(sc.onjoined)
sc.onjoined.call(sc, m.kind, m.group,
m.permissions || {},
m.value || null);
break;
case 'user':
if(sc.onuser)
......@@ -324,28 +341,33 @@ ServerConnection.prototype.connect = async function(url) {
}
/**
* login authenticates with the server.
* join requests to join a group. The onjoined callback will be called
* when we've effectively joined.
*
* @param {string} username - the username to login as.
* @param {string} group - The name of the group to join.
* @param {string} username - the username to join as.
* @param {string} password - the password.
*/
ServerConnection.prototype.login = function(username, password) {
ServerConnection.prototype.join = function(group, username, password) {
this.send({
type: 'login',
id: this.id,
type: 'join',
kind: 'join',
group: group,
username: username,
password: password,
});
}
/**
* join joins a group.
* leave leaves a group. The onjoined callback will be called when we've
* effectively left.
*
* @param {string} group - The name of the group to join.
*/
ServerConnection.prototype.join = function(group) {
ServerConnection.prototype.leave = function(group) {
this.send({
type: 'join',
kind: 'leave',
group: group,
});
}
......
......@@ -281,9 +281,7 @@ function setConnected(connected) {
function gotConnected() {
setConnected(true);
let up = getUserPass();
this.login(up.username, up.password);
this.join(group);
this.request(getSettings().request);
this.join(group, up.username, up.password);
}
/**
......@@ -1409,14 +1407,26 @@ function displayUsername() {
let presentRequested = null;
/**
* @this {ServerConnection}
* @param {string} group
* @param {Object<string,boolean>} perms
*/
async function gotPermissions(perms) {
async function gotJoined(kind, group, perms, message) {
if(kind === 'fail') {
displayError('The server said: ' + message);
this.close();
return;
}
displayUsername();
setButtonsVisibility();
if(kind !== 'leave')
this.request(getSettings().request);
try {
if(serverConnection.permissions.present && !findUpMedia('local')) {
if(kind === 'join' &&
serverConnection.permissions.present && !findUpMedia('local')) {
if(presentRequested) {
if(presentRequested === 'mike')
updateSettings({video: ''});
......@@ -2172,7 +2182,7 @@ async function serverConnect() {
serverConnection.onclose = gotClose;
serverConnection.ondownstream = gotDownStream;
serverConnection.onuser = gotUser;
serverConnection.onpermissions = gotPermissions;
serverConnection.onjoined = gotJoined;
serverConnection.onchat = addToChatbox;
serverConnection.onclearchat = clearChat;
serverConnection.onusermessage = function(id, dest, username, time, priviledged, kind, message) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment