ready for 0.4.0 release
This commit is contained in:
parent
d5cf2c88ff
commit
34038297bd
15 changed files with 11361 additions and 95 deletions
21
LICENSE.txt
Normal file
21
LICENSE.txt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Kevin Jahns <kevin.jahns@rwth-aachen.de>.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
67
README.md
Normal file
67
README.md
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
This is a WebRTC Connector for [Yjs](https://dadamonad.github.io/yjs/). It propagates document updates directly to all users via WebRTC. While WebRTC is not the most reliable connector, messages are propagated with almost no delay.
|
||||||
|
|
||||||
|
* Very fast message propagation (not noticeable)
|
||||||
|
* Very easy to use
|
||||||
|
* Very little server load (you still have to set up a [signaling server](http://www.html5rocks.com/en/tutorials/webrtc/infrastructure/))
|
||||||
|
* Not suited for a large amount of collaborators
|
||||||
|
* WebRTC is not supported in all browsers, and some have troubles communicating with each other
|
||||||
|
|
||||||
|
We provide you with a free signaling server (it is used by default), but in production you should set up your own signaling server. You could use the [signalmaster](https://github.com/andyet/signalmaster) from &yet, which is very easy to set up.
|
||||||
|
|
||||||
|
## Use it!
|
||||||
|
Retrieve this with bower or npm, and use it as a js library or as a custom polymer element.
|
||||||
|
|
||||||
|
##### NPM
|
||||||
|
```
|
||||||
|
npm install y-webrtc --save
|
||||||
|
```
|
||||||
|
and put it on the `Y` object.
|
||||||
|
|
||||||
|
```
|
||||||
|
Y.WebRTC = require("y-webrtc");
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Bower
|
||||||
|
```
|
||||||
|
bower install y-webrtc --save
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Polymer
|
||||||
|
On the website you find a bunch of examples on how you can use Yjs as polymer element.
|
||||||
|
```
|
||||||
|
<link rel="import" href="../y-webrtc/y-webrtc.html">
|
||||||
|
<y-webrtc connector={{connector}} room="my-room-name"></y-webrtc>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Create the connection object
|
||||||
|
This connector uses [SimpleWebRTC](https://simplewebrtc.com/) as an underlaying WebRTC framework, which supports the concept of rooms.
|
||||||
|
|
||||||
|
```
|
||||||
|
var options = {};
|
||||||
|
var conn = new Y.WebRTC("my_room_name", options); // will connect to the default signaling server
|
||||||
|
```
|
||||||
|
|
||||||
|
On the options object you can put the following properties:
|
||||||
|
* url (optional)
|
||||||
|
* Set the url of your signaling server. E.g. url = "https://yatta.ninja:8888" (which is the default endpoint)
|
||||||
|
* debug (optional)
|
||||||
|
* Whether to enable debugging mode (defaults to false)
|
||||||
|
|
||||||
|
# Start Hacking
|
||||||
|
This connector is also a nice starting point to build your own connector. The only 75 SLOCs of code are pretty well documented and understandable. If you have any troubles, don't hesitate to ask me for help!
|
||||||
|
|
||||||
|
### Directory Structure
|
||||||
|
* lib/
|
||||||
|
* Source files
|
||||||
|
* build/browser
|
||||||
|
* Unminified, but [browserified](http://browserify.org/) source files
|
||||||
|
* build/node
|
||||||
|
* npm modules
|
||||||
|
|
||||||
|
|
||||||
|
## License
|
||||||
|
Yjs is licensed under the [MIT License](./LICENSE.txt).
|
||||||
|
|
||||||
|
<kevin.jahns@rwth-aachen.de>
|
||||||
|
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
<!--
|
|
||||||
To change this template use Tools | Templates.
|
|
||||||
-->
|
|
||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<script src="./y-webrtc.js"></script>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
11018
build/browser/y-webrtc-polymer.js
Normal file
11018
build/browser/y-webrtc-polymer.js
Normal file
File diff suppressed because one or more lines are too long
6
build/browser/y-webrtc.html
Normal file
6
build/browser/y-webrtc.html
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
<polymer-element name="y-webrtc" hidden attributes="connector room debug">
|
||||||
|
<template>
|
||||||
|
</template>
|
||||||
|
<script src="./y-webrtc-polymer.js"></script>
|
||||||
|
</polymer-element>
|
File diff suppressed because one or more lines are too long
20
build/node/y-webrtc-polymer.js
Normal file
20
build/node/y-webrtc-polymer.js
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
var WebRTC = require('./y-webrtc');
|
||||||
|
|
||||||
|
new Polymer('y-webrtc',{
|
||||||
|
ready: function(){
|
||||||
|
this.is_initialized = false;
|
||||||
|
this.initialize();
|
||||||
|
},
|
||||||
|
initialize: function(){
|
||||||
|
if(!this.is_initialized && this.room !== undefined){
|
||||||
|
this.is_initialized = true;
|
||||||
|
this.connector = new WebRTC(this.room);
|
||||||
|
if(this.debug !== undefined){
|
||||||
|
this.connector.debug = this.debug;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
roomChanged: function(){
|
||||||
|
this.initialize();
|
||||||
|
}
|
||||||
|
});
|
|
@ -2,65 +2,122 @@
|
||||||
var SimpleWebRTC = require('simplewebrtc');
|
var SimpleWebRTC = require('simplewebrtc');
|
||||||
|
|
||||||
function WebRTC(room, webrtc_options){
|
function WebRTC(room, webrtc_options){
|
||||||
|
if(webrtc_options === undefined){
|
||||||
|
webrtc_options = {};
|
||||||
|
}
|
||||||
|
|
||||||
var swr = new SimpleWebRTC({
|
// connect per default to our server
|
||||||
debug: true
|
if(webrtc_options.url === undefined){
|
||||||
})
|
webrtc_options.url = "https://yatta.ninja:8888";
|
||||||
|
}
|
||||||
|
|
||||||
|
var swr = new SimpleWebRTC(webrtc_options);
|
||||||
this.swr = swr;
|
this.swr = swr;
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var channel;
|
var channel;
|
||||||
|
|
||||||
swr.on('createdPeer',function(conn){
|
swr.once('connectionReady',function(user_id){
|
||||||
|
// SimpleWebRTC (swr) is initialized
|
||||||
|
swr.joinRoom(room);
|
||||||
|
|
||||||
swr.joinRoom(room, function(){
|
swr.once('joinedRoom', function(){
|
||||||
swr.on("channelOpen", function(){
|
// the client joined the specified room
|
||||||
var when_bound_to_y = function(){
|
var when_bound_to_y = function(){
|
||||||
|
// when the connector is bound to Y,
|
||||||
|
// e.g. by creating a new instance of Y,
|
||||||
|
// initialize the connector with the required parameters.
|
||||||
|
// You always should specify `role`, `syncMethod`, and `user_id`
|
||||||
self.init({
|
self.init({
|
||||||
role : "slave",
|
role : "slave",
|
||||||
syncMethod : "syncAll",
|
syncMethod : "syncAll",
|
||||||
user_id : conn.id
|
user_id : user_id
|
||||||
})
|
});
|
||||||
|
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, "slave");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if(self.is_bound_to_y){
|
||||||
|
// The connector is already bound to Y, so we can execute
|
||||||
|
// `when_bound_to_y` immediately
|
||||||
|
when_bound_to_y();
|
||||||
|
} else {
|
||||||
|
// The connector has not yet been bound to Y
|
||||||
|
self.on_bound_to_y = when_bound_to_y;
|
||||||
}
|
}
|
||||||
|
|
||||||
swr.on("channelMessage", function(peer, room, message){
|
swr.on("channelMessage", function(peer, room, message){
|
||||||
if(message.type === "yjs"){
|
// The client received a message
|
||||||
console.log(message.payload);
|
// Check if the connector is already initialized,
|
||||||
|
// only then forward the message to the connector class
|
||||||
|
if(self.is_initialized && message.type === "yjs"){
|
||||||
|
self.receiveMessage(peer.id, message.payload);
|
||||||
}
|
}
|
||||||
if(this.is_bound_to_y && message.type === "yjs"){
|
});
|
||||||
this.receiveMessage(peer.id, JSON.parse(message.payload))
|
});
|
||||||
|
|
||||||
|
swr.on("createdPeer", function(peer){
|
||||||
|
// a new peer/client joined the session.
|
||||||
|
// Notify the connector class, if the connector
|
||||||
|
// is already initialized
|
||||||
|
if(self.is_initialized){
|
||||||
|
// note: Since the WebRTC Connector only supports the SyncAll
|
||||||
|
// syncmethod, every client is a slave.
|
||||||
|
self.userJoined(peer.id, "slave");
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
})
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
swr.on("peerStreamRemoved",function(peer){
|
||||||
|
// a client left the session.
|
||||||
|
// Notify the connector class, if the connector
|
||||||
|
// is already initialized
|
||||||
|
if(self.is_initialized){
|
||||||
|
self.userLeft(peer.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Specify how to send a message to a specific user (by uid)
|
||||||
WebRTC.prototype.send = function(uid, message){
|
WebRTC.prototype.send = function(uid, message){
|
||||||
var peer = this.swr.webrtc.getPeers(uid)[0];
|
var self = this;
|
||||||
peer.sendDirectly("simplewebrtc", "yjs", "message");
|
// 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
|
||||||
|
window.setTimeout(send,500);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// try to send the message
|
||||||
|
send();
|
||||||
|
};
|
||||||
|
|
||||||
|
// specify how to broadcast a message to all users
|
||||||
|
// (it may send the message back to itself).
|
||||||
|
// The webrtc connecor tries to send it to every single clients directly
|
||||||
WebRTC.prototype.broadcast = function(message){
|
WebRTC.prototype.broadcast = function(message){
|
||||||
this.swr.sendDirectlyToAll("simplewebrtc","yjs",message);
|
this.swr.sendDirectlyToAll("simplewebrtc","yjs",message);
|
||||||
}
|
};
|
||||||
|
|
||||||
if(window != null){
|
if(window !== undefined){
|
||||||
if(window.Y != null){
|
if(window.Y !== undefined){
|
||||||
window.Y.WebRTC = WebRTC;
|
window.Y.WebRTC = WebRTC;
|
||||||
} else {
|
} else {
|
||||||
// console.err("You must first include Y, and then the WebRTC Connector!")
|
console.err("You must first include Y, and then the WebRTC Connector!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(module != null){
|
if(module !== undefined){
|
||||||
module.exports = WebRTC;
|
module.exports = WebRTC;
|
||||||
}
|
}
|
||||||
|
|
||||||
window.webrtc = new WebRTC("stuffy", {
|
|
||||||
debug: true
|
|
||||||
});
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ ignore = require 'gulp-ignore'
|
||||||
jshint = require 'gulp-jshint'
|
jshint = require 'gulp-jshint'
|
||||||
|
|
||||||
paths =
|
paths =
|
||||||
webrtc: ['./lib/y-webrtc.js']
|
webrtc: ['./lib/y-webrtc*.js']
|
||||||
|
|
||||||
buildConnector = (connector_name)->
|
buildConnector = (connector_name)->
|
||||||
()->
|
()->
|
||||||
|
|
20
lib/y-webrtc-polymer.js
Normal file
20
lib/y-webrtc-polymer.js
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
var WebRTC = require('./y-webrtc');
|
||||||
|
|
||||||
|
new Polymer('y-webrtc',{
|
||||||
|
ready: function(){
|
||||||
|
this.is_initialized = false;
|
||||||
|
this.initialize();
|
||||||
|
},
|
||||||
|
initialize: function(){
|
||||||
|
if(!this.is_initialized && this.room !== undefined){
|
||||||
|
this.is_initialized = true;
|
||||||
|
this.connector = new WebRTC(this.room);
|
||||||
|
if(this.debug !== undefined){
|
||||||
|
this.connector.debug = this.debug;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
roomChanged: function(){
|
||||||
|
this.initialize();
|
||||||
|
}
|
||||||
|
});
|
|
@ -3,11 +3,12 @@ var SimpleWebRTC = require('simplewebrtc');
|
||||||
|
|
||||||
function WebRTC(room, webrtc_options){
|
function WebRTC(room, webrtc_options){
|
||||||
if(webrtc_options === undefined){
|
if(webrtc_options === undefined){
|
||||||
webrtc_options = {}
|
webrtc_options = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// connect per default to our server
|
||||||
if(webrtc_options.url === undefined){
|
if(webrtc_options.url === undefined){
|
||||||
webrtc_options.url = "http://yatta.ninja:8888"
|
webrtc_options.url = "https://yatta.ninja:8888";
|
||||||
}
|
}
|
||||||
|
|
||||||
var swr = new SimpleWebRTC(webrtc_options);
|
var swr = new SimpleWebRTC(webrtc_options);
|
||||||
|
@ -17,60 +18,94 @@ function WebRTC(room, webrtc_options){
|
||||||
var channel;
|
var channel;
|
||||||
|
|
||||||
swr.once('connectionReady',function(user_id){
|
swr.once('connectionReady',function(user_id){
|
||||||
swr.joinRoom(room)
|
// SimpleWebRTC (swr) is initialized
|
||||||
|
swr.joinRoom(room);
|
||||||
|
|
||||||
swr.once('joinedRoom', function(){
|
swr.once('joinedRoom', function(){
|
||||||
swr.once('')
|
// the client joined the specified room
|
||||||
var when_bound_to_y = function(){
|
var when_bound_to_y = function(){
|
||||||
|
// when the connector is bound to Y,
|
||||||
|
// e.g. by creating a new instance of Y,
|
||||||
|
// initialize the connector with the required parameters.
|
||||||
|
// You always should specify `role`, `syncMethod`, and `user_id`
|
||||||
self.init({
|
self.init({
|
||||||
role : "slave",
|
role : "slave",
|
||||||
syncMethod : "syncAll",
|
syncMethod : "syncAll",
|
||||||
user_id : user_id
|
user_id : user_id
|
||||||
});
|
});
|
||||||
|
var i;
|
||||||
|
// notify the connector class about all the users that already
|
||||||
|
// joined the session
|
||||||
for(i in self.swr.webrtc.peers){
|
for(i in self.swr.webrtc.peers){
|
||||||
self.userJoined(self.swr.webrtc.peers[i].id, "slave");
|
self.userJoined(self.swr.webrtc.peers[i].id, "slave");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if(self.is_bound_to_y !== undefined && self.is_bound_to_y){
|
if(self.is_bound_to_y){
|
||||||
when_bound_to_y()
|
// The connector is already bound to Y, so we can execute
|
||||||
|
// `when_bound_to_y` immediately
|
||||||
|
when_bound_to_y();
|
||||||
} else {
|
} else {
|
||||||
|
// The connector has not yet been bound to Y
|
||||||
self.on_bound_to_y = when_bound_to_y;
|
self.on_bound_to_y = when_bound_to_y;
|
||||||
}
|
}
|
||||||
|
|
||||||
swr.on("channelMessage", function(peer, room, message){
|
swr.on("channelMessage", function(peer, room, message){
|
||||||
if(self.is_bound_to_y && message.type === "yjs"){
|
// The client received a message
|
||||||
|
// Check if the connector is already initialized,
|
||||||
|
// only then forward the message to the connector class
|
||||||
|
if(self.is_initialized && message.type === "yjs"){
|
||||||
self.receiveMessage(peer.id, message.payload);
|
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
|
||||||
if(self.is_initialized){
|
if(self.is_initialized){
|
||||||
|
// note: Since the WebRTC Connector only supports the SyncAll
|
||||||
|
// syncmethod, every client is a slave.
|
||||||
self.userJoined(peer.id, "slave");
|
self.userJoined(peer.id, "slave");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
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
|
||||||
if(self.is_initialized){
|
if(self.is_initialized){
|
||||||
self.userLeft(peer.id);
|
self.userLeft(peer.id);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Specify how to send a message to a specific user (by uid)
|
||||||
WebRTC.prototype.send = function(uid, message){
|
WebRTC.prototype.send = function(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 peer = self.swr.webrtc.getPeers(uid)[0];
|
||||||
|
var success;
|
||||||
if(peer){
|
if(peer){
|
||||||
var success = peer.sendDirectly("simplewebrtc", "yjs", message);
|
// success is true, if the message is successfully sent
|
||||||
|
success = peer.sendDirectly("simplewebrtc", "yjs", message);
|
||||||
}
|
}
|
||||||
if(!success){
|
if(!success){
|
||||||
window.setTimeout(send,500)
|
// resend the message if it didn't work
|
||||||
|
window.setTimeout(send,500);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
send()
|
// try to send the message
|
||||||
|
send();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// specify how to broadcast a message to all users
|
||||||
|
// (it may send the message back to itself).
|
||||||
|
// The webrtc connecor tries to send it to every single clients directly
|
||||||
WebRTC.prototype.broadcast = function(message){
|
WebRTC.prototype.broadcast = function(message){
|
||||||
this.swr.sendDirectlyToAll("simplewebrtc","yjs",message);
|
this.swr.sendDirectlyToAll("simplewebrtc","yjs",message);
|
||||||
};
|
};
|
||||||
|
@ -79,7 +114,7 @@ if(window !== undefined){
|
||||||
if(window.Y !== undefined){
|
if(window.Y !== undefined){
|
||||||
window.Y.WebRTC = WebRTC;
|
window.Y.WebRTC = WebRTC;
|
||||||
} else {
|
} else {
|
||||||
// console.err("You must first include Y, and then the WebRTC Connector!")
|
console.err("You must first include Y, and then the WebRTC Connector!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(module !== undefined){
|
if(module !== undefined){
|
||||||
|
|
12
test.html
12
test.html
|
@ -1,12 +0,0 @@
|
||||||
<!--
|
|
||||||
To change this template use Tools | Templates.
|
|
||||||
-->
|
|
||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<script src="./y-webrtc.js"></script>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
5
y-webrtc-polymer.js
Normal file
5
y-webrtc-polymer.js
Normal file
File diff suppressed because one or more lines are too long
6
y-webrtc.html
Normal file
6
y-webrtc.html
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
<polymer-element name="y-webrtc" hidden attributes="connector room debug">
|
||||||
|
<template>
|
||||||
|
</template>
|
||||||
|
<script src="./y-webrtc-polymer.js"></script>
|
||||||
|
</polymer-element>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue