Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bradley Meck
committed
Jul 14, 2010
1 parent
9a86038
commit cc55e1d
Showing
9 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//included from ../../lib/client/api.js | ||
|
||
//@theory | ||
Synchronizer = function(){} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
sudo npm install connect@latest | ||
sudo npm install websocket-server@latest | ||
sudo npm install nStore@latest |