Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions examples/public/json.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@

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);

doc.subscribe();

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);
}
Expand All @@ -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
Expand All @@ -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() ); // => []
Expand Down Expand Up @@ -74,9 +92,11 @@
// Get the document JSON object again
console.log(doc.get()); // => [{completed: []},{todo:'some string value'}]
})


});*/


});
}

</script>
Expand Down
83 changes: 83 additions & 0 deletions examples/server-mongo.coffee
Original file line number Diff line number Diff line change
@@ -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}/"
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down