Skip to content

Commit

Permalink
readme and moved api out to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Bradley Meck committed Jul 14, 2010
1 parent 9a86038 commit cc55e1d
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 2 deletions.
54 changes: 54 additions & 0 deletions README.md
@@ -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.
4 changes: 3 additions & 1 deletion example/server.js
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions example/static/api.js
@@ -1,3 +1,5 @@
//included from ../../lib/client/api.js

//@theory
Synchronizer = function(){}

Expand Down
4 changes: 4 additions & 0 deletions example/static/client.js
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions example/static/index.html
Expand Up @@ -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>
Binary file added lib/.DS_Store
Binary file not shown.
67 changes: 67 additions & 0 deletions lib/client/api.js
@@ -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.
1 change: 0 additions & 1 deletion npm.sh
@@ -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.