Permalink
Browse files

readme and moved api out to lib

  • Loading branch information...
1 parent 9a86038 commit cc55e1d6a129869b114e125ece0708325bcd611f Bradley Meck committed Jul 14, 2010
View
@@ -0,0 +1,54 @@
+!ConcurrentEditor
+
+!! Requirements
+ * node-websocket-server
+ * example requires connect
+
+!! Files
+
+ * lib/client/api.js
+include in your web page
+
+ <script src="...">
+ * lib/server/api.js
+include on your server
+
+ require(...)
+
+!! new Synchronizer()
+
+!!! Synchronizer.prototype.sync(callback/*(err)*/)
+
+ * client:
+save(...) with all server connections
+
+ * server:
+broadcast doc to all clients
+
+!!! Synchronizer.prototype.save(doc_or_stream,callback/*(err)*/)
+
+ * client:
+update the server to contain this text
+
+ * server:
+set the doc to the document or stream provided and broadcast results
+
+!!! Synchronizer.prototype.get(callback/*(err,doc)*/)
+
+grab the doc as currently known
+
+!!! Synchronizer.prototype.open(websocket,callback/*(err)*/)
+
+attach the synchronizer to the given websocket
+
+!!! Synchronizer.prototype.close(websocket,callback/*(err)*/)
+
+remove the synchronizer on the given websocket
+
+!!! Synchronizer.onupdate(doc)
+
+fires whenever doc is updated
+
+!! new TextSynchronizer(name)
+
+creates a text synchronizer that uses a String document and is accessable by name from both ends.
View
@@ -1,13 +1,15 @@
var connect = require('connect')
, ws = require('websocket-server')
, sys = require('sys')
- , SynchronizedText = require('./api').SynchronizedText
+ , SynchronizedText = require('../lib/server/api').SynchronizedText
var input = new SynchronizedText('input')
, input_color = new SynchronizedText('input.color')
+
input.onupdate = function(doc) {
input_color.save(doc)
}
+
var texts = [
new SynchronizedText('textarea')
, input
@@ -1,3 +1,5 @@
+//included from ../../lib/client/api.js
+
//@theory
Synchronizer = function(){}
@@ -4,7 +4,11 @@ function SyncInput(element,websocket,name) {
sync.open(websocket)
sync.onupdate = function(doc) {
element.disabled = false
+ var caret = element.selectionStart
+ , selection = element.selectionEnd
element.value = doc
+ element.selectionStart = caret
+ element.selectionEnd = selection
}
element.onkeydown = function(e) {
if(!element.disabled) {
@@ -6,6 +6,7 @@
</head>
<body>
<textarea disabled="true" style="width:100%;height:90%;line-height: 19px;font-size: 18px; vertical-align: top"></textarea>
+Set me to a CSS color!
<input disabled="true">
</body>
</html>
View
Binary file not shown.
View
@@ -0,0 +1,67 @@
+//@theory
+Synchronizer = function(){}
+
+
+//@use
+SynchronizedText = function(name) {
+ this.name = name
+ this.id = 0
+ this.clients = []
+ this.doc = ""
+}
+SynchronizedText.prototype = new Synchronizer
+
+
+//TODO: Resolve merge errors...
+SynchronizedText.prototype.save = function(doc,callback) {
+ this.doc = doc
+ this.sync()
+ if(callback) callback(false)
+}
+
+
+SynchronizedText.prototype.sync = function(callback) {
+ var name = this.name
+ , doc = this.doc
+ this.clients.forEach(function(client){
+ client.websocket.send(name+"@"+doc)
+ })
+ if(callback) callback(false)
+}
+
+
+SynchronizedText.prototype.get = function(callback) {
+ if(callback) callback(false,this.doc)
+}
+
+
+SynchronizedText.prototype.open = function(websocket,callback) {
+ var $this = this
+ , listener = function(msg) {
+ msg = String(msg.data)
+ var index = msg.indexOf("@")
+ if(index !== -1) {
+ var target = msg.slice(0,index)
+ if(target === $this.name) {
+ var doc = msg.slice(index+1)
+ $this.doc = doc
+ if($this.onupdate) $this.onupdate(doc)
+ }
+ }
+ }
+ this.clients.push({websocket:websocket,listener:listener})
+ websocket.addEventListener("message",listener)
+ if(callback) callback(false)
+}
+
+
+SynchronizedText.prototype.close = function(websocket,callback) {
+ var notFound = true
+ this.clients.forEach(function(client){
+ if(client.websocket === websocket) {
+ notFound = false
+ websocket.removeEventListener(client.listener)
+ }
+ })
+ if(callback) callback(notFound)
+}
File renamed without changes.
View
@@ -1,3 +1,2 @@
sudo npm install connect@latest
sudo npm install websocket-server@latest
-sudo npm install nStore@latest

0 comments on commit cc55e1d

Please sign in to comment.