Created
September 13, 2016 18:42
-
-
Save kuzyn/9b18ac3c948c203dd25b99784cd8ea2c to your computer and use it in GitHub Desktop.
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
var socket = io.connect('/'); | |
////////////// | |
// Handlers // | |
////////////// | |
// fire when socket.io is connected to the back-end | |
socket.on('connect', function() { | |
getEntries(3); | |
}); | |
// fire when a "newEntry" event is broadcasted | |
socket.on('newEntry-' + currentLocation, function(data) { | |
getEntries(3); | |
}); | |
function getEntries(number) { | |
// do something client side | |
} | |
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
// create an entry | |
router.post('/', function(req, res, next) { | |
// our parameters | |
// TODO: attach client id and to entries | |
var payload = { | |
message: req.body.entryMessage, | |
position: { | |
currentState: parseInt(req.body.entryCurrentState), | |
currentChapter: parseInt(req.body.entryCurrentChapter) | |
}, | |
location: req.body.entryLocation || 'unknown', | |
user: req.body.entryUser || 'unknown', //TODO: our client need to send a unique client id | |
timestamp: new Date() | |
}; | |
// our model's method | |
Entry.create(payload, function(err, doc) { | |
if (err) { | |
next(helpers.generateError(req.originalUrl, 503, err)); | |
} | |
res.io.emit('newEntry-' + payload.location, doc); // our socket.io event broadcast | |
res.io.emit('updateEntries', doc); // a general event for our admin page | |
res.json(doc); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment