From 997239e74ddb2ce3bd159222664c42f8978d5286 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 1 Jun 2017 18:11:31 -0400 Subject: printer socket io and stuff --- lib/awprint/index.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'lib/awprint/index.js') diff --git a/lib/awprint/index.js b/lib/awprint/index.js index f500545..dec43fd 100644 --- a/lib/awprint/index.js +++ b/lib/awprint/index.js @@ -2,6 +2,7 @@ var bodyParser = require('body-parser') var mongoose = require('mongoose') var path = require('path') mongoose.Promise = require('bluebird') +var socketIO = require('socket.io') function AWPrint (options) { if (!(this instanceof AWPrint)) @@ -16,6 +17,7 @@ function AWPrint (options) { var express = options.express var router = express.Router() var config = options.config + var io; var db, Print @@ -55,8 +57,8 @@ function AWPrint (options) { date: Date.now() } res.sendStatus(200) - new Print(data).save().then( (req) => { - // send a websocket message + new Print(data).save().then( (job) => { + io && io.emit('job', job) }).catch( (err) => { // idk }) @@ -64,7 +66,7 @@ function AWPrint (options) { router.post('/print', bodyParser.json({}), function (req, res) { res.sendStatus(200) - Print.update({ url: req.body.url }, { printed: true, }).then( (req) => { + Print.update({ _id: req.body._id }, { printed: true, }).then( (req) => { // send a websocket message? }).catch( (err) => { // idk @@ -72,6 +74,14 @@ function AWPrint (options) { }) this._router = router + + // defer until the app is mounted + setImmediate(function(){ + io = socketIO(options.app._server) + io.on('connection', function(socket){ + // ... + }) + }) } AWPrint.prototype.middleware = function () { -- cgit v1.2.3-70-g09d2