switched to *standard* coding style
This commit is contained in:
@@ -1,102 +1,104 @@
|
||||
/* global getRandom, AbstractConnector, Y, wait */
|
||||
|
||||
var globalRoom = {
|
||||
users: {},
|
||||
buffers: {},
|
||||
removeUser: function(user : AbstractConnector){
|
||||
removeUser: function (user) {
|
||||
for (var i in this.users) {
|
||||
this.users[i].userLeft(user);
|
||||
this.users[i].userLeft(user)
|
||||
}
|
||||
delete this.users[user];
|
||||
delete this.buffers[user];
|
||||
delete this.users[user]
|
||||
delete this.buffers[user]
|
||||
},
|
||||
addUser: function(connector){
|
||||
this.users[connector.userId] = connector;
|
||||
this.buffers[connector.userId] = [];
|
||||
addUser: function (connector) {
|
||||
this.users[connector.userId] = connector
|
||||
this.buffers[connector.userId] = []
|
||||
for (var uname in this.users) {
|
||||
if (uname !== connector.userId) {
|
||||
var u = this.users[uname];
|
||||
u.userJoined(connector.userId, "master");
|
||||
connector.userJoined(u.userId, "master");
|
||||
var u = this.users[uname]
|
||||
u.userJoined(connector.userId, 'master')
|
||||
connector.userJoined(u.userId, 'master')
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
function flushOne(){
|
||||
var bufs = [];
|
||||
}
|
||||
function flushOne () {
|
||||
var bufs = []
|
||||
for (var i in globalRoom.buffers) {
|
||||
if (globalRoom.buffers[i].length > 0) {
|
||||
bufs.push(i);
|
||||
bufs.push(i)
|
||||
}
|
||||
}
|
||||
if (bufs.length > 0) {
|
||||
var userId = getRandom(bufs);
|
||||
var m = globalRoom.buffers[userId].shift();
|
||||
var user = globalRoom.users[userId];
|
||||
user.receiveMessage(m[0], m[1]);
|
||||
return true;
|
||||
var userId = getRandom(bufs)
|
||||
var m = globalRoom.buffers[userId].shift()
|
||||
var user = globalRoom.users[userId]
|
||||
user.receiveMessage(m[0], m[1])
|
||||
return true
|
||||
} else {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// setInterval(flushOne, 10);
|
||||
// setInterval(flushOne, 10)
|
||||
|
||||
var userIdCounter = 0;
|
||||
var userIdCounter = 0
|
||||
|
||||
class Test extends AbstractConnector {
|
||||
constructor (y, options) {
|
||||
if(options === undefined){
|
||||
throw new Error("Options must not be undefined!");
|
||||
if (options === undefined) {
|
||||
throw new Error('Options must not be undefined!')
|
||||
}
|
||||
options.role = "master";
|
||||
options.forwardToSyncingClients = false;
|
||||
super(y, options);
|
||||
this.setUserId((userIdCounter++) + "");
|
||||
globalRoom.addUser(this);
|
||||
this.globalRoom = globalRoom;
|
||||
options.role = 'master'
|
||||
options.forwardToSyncingClients = false
|
||||
super(y, options)
|
||||
this.setUserId((userIdCounter++) + '')
|
||||
globalRoom.addUser(this)
|
||||
this.globalRoom = globalRoom
|
||||
}
|
||||
send (userId, message) {
|
||||
globalRoom.buffers[userId].push(JSON.parse(JSON.stringify([this.userId, message])));
|
||||
globalRoom.buffers[userId].push(JSON.parse(JSON.stringify([this.userId, message])))
|
||||
}
|
||||
broadcast (message) {
|
||||
for (var key in globalRoom.buffers) {
|
||||
globalRoom.buffers[key].push(JSON.parse(JSON.stringify([this.userId, message])));
|
||||
globalRoom.buffers[key].push(JSON.parse(JSON.stringify([this.userId, message])))
|
||||
}
|
||||
}
|
||||
disconnect () {
|
||||
globalRoom.removeUser(this.userId);
|
||||
globalRoom.removeUser(this.userId)
|
||||
}
|
||||
flush() {
|
||||
var buff = globalRoom.buffers[this.userId];
|
||||
flush () {
|
||||
var buff = globalRoom.buffers[this.userId]
|
||||
while (buff.length > 0) {
|
||||
var m = buff.shift();
|
||||
this.receiveMessage(m[0], m[1]);
|
||||
var m = buff.shift()
|
||||
this.receiveMessage(m[0], m[1])
|
||||
}
|
||||
}
|
||||
flushAll () {
|
||||
var def = Promise.defer();
|
||||
var def = Promise.defer()
|
||||
// flushes may result in more created operations,
|
||||
// flush until there is nothing more to flush
|
||||
function nextFlush() {
|
||||
var c = flushOne();
|
||||
function nextFlush () {
|
||||
var c = flushOne()
|
||||
if (c) {
|
||||
while(flushOne()) {
|
||||
//nop
|
||||
while (flushOne()) {
|
||||
// nop
|
||||
}
|
||||
wait().then(nextFlush);
|
||||
wait().then(nextFlush)
|
||||
} else {
|
||||
wait().then(function(){
|
||||
def.resolve();
|
||||
});
|
||||
wait().then(function () {
|
||||
def.resolve()
|
||||
})
|
||||
}
|
||||
}
|
||||
// in the case that there are
|
||||
// still actions that want to be performed
|
||||
wait(0).then(nextFlush);
|
||||
return def.promise;
|
||||
wait(0).then(nextFlush)
|
||||
return def.promise
|
||||
}
|
||||
flushOne() {
|
||||
flushOne();
|
||||
flushOne () {
|
||||
flushOne()
|
||||
}
|
||||
}
|
||||
|
||||
Y.Test = Test;
|
||||
Y.Test = Test
|
||||
|
||||
@@ -1,86 +1,87 @@
|
||||
/* global AbstractConnector, Y */
|
||||
|
||||
class WebRTC extends AbstractConnector {
|
||||
constructor (y, options) {
|
||||
if(options === undefined){
|
||||
throw new Error("Options must not be undefined!");
|
||||
if (options === undefined) {
|
||||
throw new Error('Options must not be undefined!')
|
||||
}
|
||||
if(options.room == null) {
|
||||
throw new Error("You must define a room name!");
|
||||
if (options.room == null) {
|
||||
throw new Error('You must define a room name!')
|
||||
}
|
||||
options.role = "slave";
|
||||
super(y, options);
|
||||
options.role = 'slave'
|
||||
super(y, options)
|
||||
|
||||
var room = options.room;
|
||||
var room = options.room
|
||||
|
||||
var webrtcOptions = {
|
||||
url: options.url || "https://yatta.ninja:8888",
|
||||
url: options.url || 'https://yatta.ninja:8888',
|
||||
room: options.room
|
||||
};
|
||||
}
|
||||
|
||||
var swr = new SimpleWebRTC(webrtcOptions); //eslint-disable-line no-undef
|
||||
this.swr = swr;
|
||||
var self = this;
|
||||
var swr = new SimpleWebRTC(webrtcOptions) // eslint-disable-line no-undef
|
||||
this.swr = swr
|
||||
var self = this
|
||||
|
||||
swr.once("connectionReady", function(userId){
|
||||
swr.once('connectionReady', function (userId) {
|
||||
// SimpleWebRTC (swr) is initialized
|
||||
swr.joinRoom(room);
|
||||
swr.joinRoom(room)
|
||||
|
||||
swr.once("joinedRoom", function(){
|
||||
self.setUserId(userId);
|
||||
swr.once('joinedRoom', function () {
|
||||
self.setUserId(userId)
|
||||
/*
|
||||
var i;
|
||||
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");
|
||||
self.userJoined(self.swr.webrtc.peers[i].id, "master")
|
||||
}*/
|
||||
swr.on("channelMessage", function(peer, room_, message){
|
||||
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);
|
||||
if (message.type != null) {
|
||||
self.receiveMessage(peer.id, message.payload)
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
swr.on("createdPeer", function(peer){
|
||||
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");
|
||||
});
|
||||
self.userJoined(peer.id, 'master')
|
||||
})
|
||||
|
||||
swr.on("peerStreamRemoved", function(peer){
|
||||
swr.on('peerStreamRemoved', function (peer) {
|
||||
// a client left the session.
|
||||
// Notify the connector class, if the connector
|
||||
// is already initialized
|
||||
self.userLeft(peer.id);
|
||||
});
|
||||
});
|
||||
self.userLeft(peer.id)
|
||||
})
|
||||
})
|
||||
}
|
||||
send (uid, message) {
|
||||
var self = this;
|
||||
var self = this
|
||||
// we have to make sure that the message is sent under all circumstances
|
||||
var send = function(){
|
||||
var send = function () {
|
||||
// check if the clients still exists
|
||||
var peer = self.swr.webrtc.getPeers(uid)[0];
|
||||
var success;
|
||||
if(peer){
|
||||
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);
|
||||
success = peer.sendDirectly('simplewebrtc', 'yjs', message)
|
||||
}
|
||||
if(!success){
|
||||
if (!success) {
|
||||
// resend the message if it didn't work
|
||||
setTimeout(send, 500);
|
||||
setTimeout(send, 500)
|
||||
}
|
||||
};
|
||||
}
|
||||
// try to send the message
|
||||
send();
|
||||
send()
|
||||
}
|
||||
broadcast (message) {
|
||||
this.swr.sendDirectlyToAll("simplewebrtc", "yjs", message);
|
||||
this.swr.sendDirectlyToAll('simplewebrtc', 'yjs', message)
|
||||
}
|
||||
}
|
||||
|
||||
Y.WebRTC = WebRTC;
|
||||
Y.WebRTC = WebRTC
|
||||
|
||||
Reference in New Issue
Block a user