Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script src="socket.io/socket.io.js"></script>
- <script src="/coffee/object-sync-client.js"></script>
- <script>
- sync = new ObjectSync();
- sync.connect();
- sync.on('update', function(obj) {
- switch(obj.type) {
- case 'player':
- drawPlayer(obj);
- break;
- case 'score':
- updateScore(obj);
- break;
- // ... etc
- }
- });
- sync.on('destroy', function(id){/*...*/});
- sync.on('create', function(obj){/*...*/});
- # Create a new object (saving something without an id)
- sync.save({
- is_this_new: 'yes'
- }, function(err, obj) {
- // returns either an error or a server-provided object
- // if there was no error, a 'create' event will fire here
- // and on every other connected client
- });
- sync.save({
- id: 5,
- is_this_new: false,
- is_this_updated: true
- }, function(err, obj){
- // updates an object
- });
- sync.fetch([1,2,3,4], function(err, results) {
- // fetches objects 1,2,3 and 4
- });
- sync.destroy(1, function(err) {
- // if there was no error, object 1 is now destroyed
- // a 'destroy' event will fire shortly
- })
- sync.allObjects(); // returns a map of all objects on the client
- </script>
Add Comment
Please, Sign In to add comment