diff --git a/examples/public/json.html b/examples/public/json.html index 7c93a7f5..a1b8f482 100644 --- a/examples/public/json.html +++ b/examples/public/json.html @@ -6,7 +6,7 @@ var sjs = new window.sharejs.Connection(s); -var doc = sjs.getOrCreate('json_test', 'test_doc_1'); +var doc = window.doc = sjs.get('json_test', 'test_doc_1'); console.log(doc); @@ -14,8 +14,13 @@ doc.whenReady(function () { console.log(doc); - if (!doc.type) doc.create('json0'); + if (!doc.type) doc.create('json0', []); if (doc.type && doc.type.name === 'json0'){ + + doc.on('op', function() { + + console.log("doc.on `op`", arguments); + }); var context = doc.createContext(); clientExample(context); } @@ -26,14 +31,27 @@ console.log( 'JSON Client API', - 'https://github.com/share/ShareJS/wiki/JSON-Client-API'); + '0.7'); console.log('json client',context); + + // Create some JSON object - var myObject = [{ todo: [] },{completed: []}]; + var myObject = doc.getSnapshot(); + + if(!myObject.length) { + myObject = [{ todo: [] },{completed: []}]; + context.submitOp({p:[0], li : myObject[0]}); + context.submitOp({p:[1], li : myObject[1]}); + } + + + + // Set the structure of the document to the JSON object + /* context.set( myObject, function(){ // Get the document's JSON object @@ -42,7 +60,7 @@ // Get the "todo" subdoc todo = context.createContextAt([0,'todo']); - console.log('todo',todo); + console.log('todo',todo); return; // print out the "todo" subdoc console.log( todo.get() ); // => [] @@ -74,9 +92,11 @@ // Get the document JSON object again console.log(doc.get()); // => [{completed: []},{todo:'some string value'}] }) + + + });*/ - }); } diff --git a/examples/server-mongo.coffee b/examples/server-mongo.coffee new file mode 100644 index 00000000..b3e0fa95 --- /dev/null +++ b/examples/server-mongo.coffee @@ -0,0 +1,83 @@ +# This is a little prototype browserchannel wrapper for the session code. +{Duplex} = require 'stream' +browserChannel = require('browserchannel').server +connect = require 'connect' +serveStatic = require 'serve-static' +argv = require('optimist').argv +livedb = require 'livedb' + +livedbMongo = require 'livedb-mongo' +mongoskin = require 'mongoskin' +skin = mongoskin.db 'mongodb://localhost:27017/mongo_test?auto_reconnect', safe:true +mongo = livedbMongo skin + + +try + require 'heapdump' + +sharejs = require '../lib' + +webserver = connect() + +webserver.use serveStatic "#{__dirname}/public" +webserver.use serveStatic sharejs.scriptsDir + +#backend = livedb.client livedb.memory() +backend = livedb.client mongo + +backend.addProjection '_users', 'users', 'json0', {x:true} + +share = sharejs.server.createClient {backend} + + +### +share.use 'validate', (req, callback) -> + err = 'noooo' if req.snapshot.data?.match /x/ + callback err + +share.use 'connect', (req, callback) -> + console.log req.agent + callback() +### + +numClients = 0 + +webserver.use browserChannel {webserver, sessionTimeoutInterval:5000}, (client) -> + numClients++ + stream = new Duplex objectMode:yes + stream._write = (chunk, encoding, callback) -> + console.log 's->c ', JSON.stringify(chunk) + if client.state isnt 'closed' # silently drop messages after the session is closed + client.send chunk + callback() + + stream._read = -> # Ignore. You can't control the information, man! + + stream.headers = client.headers + stream.remoteAddress = stream.address + + client.on 'message', (data) -> + console.log 'c->s ', JSON.stringify(data) + stream.push data + + stream.on 'error', (msg) -> + client.stop() + + client.on 'close', (reason) -> + stream.push null + stream.emit 'close' + + numClients-- + console.log 'client went away', numClients + + stream.on 'end', -> + client.close() + + # ... and give the stream to ShareJS. + share.listen stream + +webserver.use '/doc', share.rest() + +port = argv.p or 7007 +webserver.listen port +console.log "Listening on http://localhost:#{port}/" diff --git a/package.json b/package.json index 93c30fa3..c650086e 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,9 @@ "async": "^0.9.0", "hat": "^0.0.3", "livedb": "^0.5.12", + "livedb-mongo": "^0.4.1", + "mongodb": "^1.4.38", + "mongoskin": "^1.4.13", "ot-json0": "^1.0.0", "ot-text": "^1.0.0", "ot-text-tp2": "^1.0.0"