added test connector, webrtc connector, ideas to apply operations with very low overhead
This commit is contained in:
75
src/Connectors/Test.js
Normal file
75
src/Connectors/Test.js
Normal file
@@ -0,0 +1,75 @@
|
||||
// returns a rendom element of o
|
||||
// works on Object, and Array
|
||||
function getRandom (o) {
|
||||
if (o instanceof Array) {
|
||||
return o[Math.floor(Math.random() * o.length)];
|
||||
} else if (o.constructor === Object) {
|
||||
var keys = [];
|
||||
for (var key in o) {
|
||||
keys.push(key);
|
||||
}
|
||||
return o[getRandom(keys)];
|
||||
}
|
||||
}
|
||||
|
||||
var globalRoom = {
|
||||
users: {},
|
||||
buffers: {},
|
||||
removeUser: function(user){
|
||||
for (var u of this.users) {
|
||||
u.userLeft(user);
|
||||
}
|
||||
delete this.users[user];
|
||||
delete this.buffers[user];
|
||||
},
|
||||
addUser: function(connector){
|
||||
for (var u of this.users) {
|
||||
u.userJoined(connector.userId);
|
||||
}
|
||||
this.users[connector.userId] = connector;
|
||||
this.buffers[connector.userId] = [];
|
||||
}
|
||||
};
|
||||
|
||||
setInterval(function(){
|
||||
var bufs = [];
|
||||
for (var i in globalRoom.buffers) {
|
||||
if (globalRoom.buffers[i].length > 0) {
|
||||
bufs.push(i);
|
||||
}
|
||||
}
|
||||
if (bufs.length > 0) {
|
||||
var userId = getRandom(bufs);
|
||||
var m = globalRoom.buffers[userId];
|
||||
var user = globalRoom.users[userId];
|
||||
user.receiveMessage(m);
|
||||
}
|
||||
}, 10);
|
||||
|
||||
var userIdCounter = 0;
|
||||
|
||||
class Test extends AbstractConnector {
|
||||
constructor (options) {
|
||||
if(options === undefined){
|
||||
throw new Error("Options must not be undefined!");
|
||||
}
|
||||
super({
|
||||
role: "master"
|
||||
});
|
||||
|
||||
this.setUserId((userIdCounter++) + "");
|
||||
}
|
||||
send (uid, message) {
|
||||
globalRoom.buffers[uid].push(message);
|
||||
}
|
||||
broadcast (message) {
|
||||
for (var buf of globalRoom.buffers) {
|
||||
buf.push(message);
|
||||
}
|
||||
}
|
||||
disconnect () {
|
||||
globalRoom.removeUser(this.userId);
|
||||
}
|
||||
}
|
||||
|
||||
Y.Test = Test;
|
||||
83
src/Connectors/WebRTC.js
Normal file
83
src/Connectors/WebRTC.js
Normal file
@@ -0,0 +1,83 @@
|
||||
|
||||
class WebRTC extends AbstractConnector {
|
||||
constructor (options) {
|
||||
if(options === undefined){
|
||||
throw new Error("Options must not be undefined!");
|
||||
}
|
||||
super({
|
||||
role: "slave"
|
||||
});
|
||||
|
||||
var room = options.room;
|
||||
|
||||
// connect per default to our server
|
||||
if(options.url == null){
|
||||
options.url = "https://yatta.ninja:8888";
|
||||
}
|
||||
|
||||
var swr = new SimpleWebRTC(options); //eslint-disable-line no-undef
|
||||
this.swr = swr;
|
||||
var self = this;
|
||||
|
||||
swr.once("connectionReady", function(userId){
|
||||
// SimpleWebRTC (swr) is initialized
|
||||
swr.joinRoom(room);
|
||||
|
||||
swr.once("joinedRoom", function(){
|
||||
self.setUserId(userId);
|
||||
var i;
|
||||
// notify the connector class about all the users that already
|
||||
// joined the session
|
||||
for(i in self.swr.webrtc.peers){
|
||||
self.userJoined(self.swr.webrtc.peers[i].id, "master");
|
||||
}
|
||||
swr.on("channelMessage", function(peer, room_, message){
|
||||
// The client received a message
|
||||
// Check if the connector is already initialized,
|
||||
// only then forward the message to the connector class
|
||||
if(message.type != null ){
|
||||
self.receiveMessage(peer.id, message.payload);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
swr.on("createdPeer", function(peer){
|
||||
// a new peer/client joined the session.
|
||||
// Notify the connector class, if the connector
|
||||
// is already initialized
|
||||
self.userJoined(peer.id, "master");
|
||||
});
|
||||
|
||||
swr.on("peerStreamRemoved", function(peer){
|
||||
// a client left the session.
|
||||
// Notify the connector class, if the connector
|
||||
// is already initialized
|
||||
self.userLeft(peer.id);
|
||||
});
|
||||
});
|
||||
}
|
||||
send (uid, message) {
|
||||
var self = this;
|
||||
// we have to make sure that the message is sent under all circumstances
|
||||
var send = function(){
|
||||
// check if the clients still exists
|
||||
var peer = self.swr.webrtc.getPeers(uid)[0];
|
||||
var success;
|
||||
if(peer){
|
||||
// success is true, if the message is successfully sent
|
||||
success = peer.sendDirectly("simplewebrtc", "yjs", message);
|
||||
}
|
||||
if(!success){
|
||||
// resend the message if it didn't work
|
||||
setTimeout(send, 500);
|
||||
}
|
||||
};
|
||||
// try to send the message
|
||||
send();
|
||||
}
|
||||
broadcast (message) {
|
||||
this.swr.sendDirectlyToAll("simplewebrtc", "yjs", message);
|
||||
}
|
||||
}
|
||||
|
||||
Y.WebRTC = WebRTC;
|
||||
Reference in New Issue
Block a user