Fix events stuff with awful hacks
This commit is contained in:
parent
b48a4e92e1
commit
ab49c181e7
8 changed files with 60 additions and 52 deletions
|
@ -129,6 +129,7 @@ app.use(auth);
|
|||
|
||||
app.use(route.get('/user', user.get));
|
||||
app.use(route.get('/user/token', user.token));
|
||||
app.use(route.get('/token', user.token));
|
||||
app.use(route.get('/user/transaction', user.transaction));
|
||||
app.use(route.post('/user/settings', user.settings));
|
||||
app.use(route.get('/file', file.list));
|
||||
|
|
|
@ -15,7 +15,7 @@ const debug = debugname('hostr-api:file');
|
|||
|
||||
const redisUrl = process.env.REDIS_URL || process.env.REDISTOGO_URL || 'redis://localhost:6379';
|
||||
|
||||
const fileHost = process.env.FILE_HOST || 'https://localhost:4040';
|
||||
const fileHost = process.env.FILE_HOST || 'http://localhost:4040';
|
||||
|
||||
const storePath = process.env.STORE_PATH || path.join(process.env.HOME, '.hostr', 'uploads');
|
||||
|
||||
|
@ -105,7 +105,7 @@ export function* post(next) {
|
|||
|
||||
percentComplete = Math.floor(receivedSize * 100 / expectedSize);
|
||||
if (percentComplete > lastPercent && lastTick < Date.now() - 1000) {
|
||||
const progressEvent = `{type: 'file-progress', data: {id: ${fileId}, complete: ${percentComplete}}}`;
|
||||
const progressEvent = `{"type": "file-progress", "data": {"id": "${fileId}", "complete": ${percentComplete}}}`;
|
||||
this.redis.publish('/file/' + fileId, progressEvent);
|
||||
this.redis.publish('/user/' + this.user.id, progressEvent);
|
||||
lastTick = Date.now();
|
||||
|
@ -116,10 +116,10 @@ export function* post(next) {
|
|||
});
|
||||
|
||||
// Fire an event to let the frontend map the GUID it sent to the real ID. Allows immediate linking to the file
|
||||
let acceptedEvent = `{type: 'file-accepted', data: {id: ${fileId}, guid: ${tempGuid}, href: ${fileHost}/${fileId}}}`;
|
||||
let acceptedEvent = `{"type": "file-accepted", "data": {"id": "${fileId}", "guid": "${tempGuid}", "href": "${fileHost}/${fileId}"}}`;
|
||||
this.redis.publish('/user/' + this.user.id, acceptedEvent);
|
||||
// Fire final upload progress event so users know it's now processing
|
||||
const completeEvent = `{type: 'file-progress', data: {id: ${fileId}, complete: 100}}`;
|
||||
const completeEvent = `{"type": "file-progress", "data": {"id": "${fileId}", "complete": 100}}`;
|
||||
this.redis.publish('/file/' + fileId, completeEvent);
|
||||
this.redis.publish('/user/' + this.user.id, completeEvent);
|
||||
|
||||
|
@ -160,7 +160,7 @@ export function* post(next) {
|
|||
yield Files.updateOne({_id: fileId}, {$set: dbFile});
|
||||
|
||||
// Fire upload complete event
|
||||
const addedEvent = `{type: 'file-added', data: ${formattedFile}}`;
|
||||
const addedEvent = `{"type": "file-added", "data": ${JSON.stringify(formattedFile)}}`;
|
||||
this.redis.publish('/file/' + fileId, addedEvent);
|
||||
this.redis.publish('/user/' + this.user.id, addedEvent);
|
||||
this.status = 201;
|
||||
|
@ -246,14 +246,14 @@ export function* del(id) {
|
|||
|
||||
export function* events() {
|
||||
const pubsub = redis.connect(redisUrl);
|
||||
pubsub.on('ready', function() {
|
||||
pubsub.on('ready', () => {
|
||||
pubsub.subscribe(this.path);
|
||||
}.bind(this));
|
||||
});
|
||||
|
||||
pubsub.on('message', function(channel, message) {
|
||||
pubsub.on('message', (channel, message) => {
|
||||
this.websocket.send(message);
|
||||
}.bind(this));
|
||||
this.on('close', function() {
|
||||
});
|
||||
this.websocket.on('close', function() {
|
||||
pubsub.quit();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import co from 'co';
|
|||
import passwords from 'passwords';
|
||||
|
||||
import debugname from 'debug';
|
||||
const debug = debugname('hostr-api:file');
|
||||
const debug = debugname('hostr-api:user');
|
||||
|
||||
const redisUrl = process.env.REDIS_URL || process.env.REDISTOGO_URL || 'redis://localhost:6379';
|
||||
|
||||
|
@ -57,10 +57,10 @@ export function* settings() {
|
|||
|
||||
export function* events() {
|
||||
const pubsub = redis.connect(redisUrl);
|
||||
pubsub.on('message', function(channel, message) {
|
||||
pubsub.on('message', (channel, message) => {
|
||||
this.websocket.send(message);
|
||||
}.bind(this));
|
||||
pubsub.on('ready', function () {
|
||||
});
|
||||
pubsub.on('ready', () => {
|
||||
this.websocket.on('message', co.wrap(function* (message) {
|
||||
let json;
|
||||
try{
|
||||
|
@ -76,9 +76,9 @@ export function* events() {
|
|||
} else {
|
||||
this.websocket.send('Invalid authentication token.');
|
||||
}
|
||||
}));
|
||||
}.bind(this));
|
||||
this.on('close', function() {
|
||||
}.bind(this)));
|
||||
});
|
||||
this.websocket.on('close', () => {
|
||||
debug('Socket closed');
|
||||
pubsub.quit();
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue