Last active
August 29, 2015 14:22
-
-
Save cphyc/03f153ebf4ccf265805e to your computer and use it in GitHub Desktop.
This is a angular module for yjs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* you need an easyRTCService that exposes the following methods : | |
* - myEasyrtcid | |
* - connection() promise | |
* - addDataChannelOpenListener | |
* - addPeerListener | |
* - addDataChannelCloseListener | |
* - sendData | |
* - broadcastData | |
* | |
* An example implementation can be found here: https://ci.open-paas.org/stash/projects/OPANGMOD/repos/angular-liveconference/browse/src/js/services/easyrtc.js | |
* | |
* LICENSE: | |
* AGPLv3, see https://github.com/linagora/hublin/blob/master/LICENSE | |
*/ | |
'use strict'; | |
angular.module('yjs', ['op.live-conference']) | |
.factory('YjsConnectorFactory', ['$log', function($log) { | |
function EasyRTCConnector(webrtc) { | |
var connector = this; | |
connector.webrtc = webrtc; | |
this.connected_peers = []; | |
var messageListeners = []; | |
this.addMessageListener = function(callback) { | |
messageListeners.push(callback); | |
}; | |
this.removeMessageListener = function(callback) { | |
messageListeners = messageListeners.filter(function(cb) { | |
return cb !== callback; | |
}); | |
}; | |
this.getMessageListeners = function() { | |
return messageListeners; | |
}; | |
var add_missing_peers = function() { | |
if (connector.is_initialized) { | |
var peer = connector.connected_peers.pop(); | |
while (peer !== undefined) { | |
connector.userJoined(peer, 'slave'); | |
peer = connector.connected_peers.pop(); | |
} | |
} | |
}; | |
var when_bound_to_y = function() { | |
connector.init({ | |
role: 'slave', | |
syncMethod: 'syncAll', | |
user_id: webrtc.myEasyrtcid() | |
}); | |
connector.connected_peers = webrtc.getOpenedDataChannels(); | |
add_missing_peers(); | |
}; | |
webrtc.connection().then(function() { | |
if (connector.is_bound_to_y) { | |
when_bound_to_y(); | |
} else { | |
connector.on_bound_to_y = when_bound_to_y(); | |
} | |
}, function(errorCode, message) { | |
$log.error('Error while getting connection to server.', errorCode, message); | |
}); | |
webrtc.addDataChannelOpenListener(function(peerId) { | |
if (connector.is_initialized) { | |
connector.connected_peers.push(peerId); | |
add_missing_peers(); | |
} | |
}); | |
webrtc.addPeerListener(function(id, msgType, msgData) { | |
if (connector.is_initialized) { | |
var parsedMessage = JSON.parse(msgData); | |
connector.receiveMessage(id, parsedMessage); | |
var messageListeners = connector.getMessageListeners(); | |
if (messageListeners) { | |
messageListeners.forEach(function(msgListener) { | |
msgListener.call(msgListener, parsedMessage); | |
}); | |
} | |
} | |
}, 'yjs'); | |
webrtc.addDataChannelCloseListener(function(peerId) { | |
var index = connector.connected_peers.indexOf(peerId); | |
if (index > -1) { | |
connector.connected_peers.splice(index, 1); | |
} | |
if (connector.is_initialized) { | |
connector.userLeft(peerId); | |
} | |
}); | |
} | |
EasyRTCConnector.prototype.send = function(user_id, message) { | |
this.webrtc.sendData(user_id, 'yjs', message); | |
}; | |
EasyRTCConnector.prototype.broadcast = function(message) { | |
this.webrtc.broadcastData('yjs', JSON.stringify(message)); | |
}; | |
return EasyRTCConnector; | |
}]) | |
.service('yjsService', ['easyRTCService', 'YjsConnectorFactory', '$log', function(easyRTCService, YjsConnectorFactory, $log) { | |
var connector = new YjsConnectorFactory(easyRTCService); | |
var y = new window.Y(connector); | |
$log.info('Created yjs instance', y, connector); | |
return function() { | |
return { | |
y: y, | |
connector: connector | |
}; | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment