Replace redis with socket.io connection to orbit-server

This commit is contained in:
haad 2016-02-22 15:26:02 +02:00
parent 144e1f94fd
commit 15fc4cf4f2
4 changed files with 13 additions and 24 deletions

View File

@ -7,7 +7,7 @@ const Timer = require('./Timer');
// Redis // Redis
const host = 'localhost'; const host = 'localhost';
const port = 6379; const port = 3333;
const username = 'LambOfGod'; const username = 'LambOfGod';
const password = ''; const password = '';

View File

@ -12,7 +12,7 @@
"asyncawait": "^1.0.1", "asyncawait": "^1.0.1",
"lodash": "^4.3.0", "lodash": "^4.3.0",
"orbit-common": "^0.1.0", "orbit-common": "^0.1.0",
"redis": "^2.4.2" "socket.io-client": "^1.4.5"
}, },
"devDependencies": { "devDependencies": {
"mocha": "^2.3.4" "mocha": "^2.3.4"

View File

@ -11,7 +11,7 @@ const HashCacheOps = require('./HashCacheOps');
const ItemTypes = require('./ItemTypes'); const ItemTypes = require('./ItemTypes');
const MetaInfo = require('./MetaInfo'); const MetaInfo = require('./MetaInfo');
const Post = require('./Post'); const Post = require('./Post');
const PubSub = require('./Pubsub'); const PubSub = require('./PubSub');
const List = require('./list/OrbitList'); const List = require('./list/OrbitList');
const DataStore = require('./DataStore'); const DataStore = require('./DataStore');

View File

@ -1,42 +1,31 @@
'use strict'; 'use strict';
const redis = require("redis"); const io = require('socket.io-client');
const List = require('./list/OrbitList'); const List = require('./list/OrbitList');
class Pubsub { class Pubsub {
constructor(ipfs, host, port, username, password) { constructor(ipfs, host, port, username, password) {
this.ipfs = ipfs; this.ipfs = ipfs;
this._subscriptions = {}; this._subscriptions = {};
this.client1 = redis.createClient({ host: host, port: port }); this._socket = io(`http://${host}:${port}`);
this.client2 = redis.createClient({ host: host, port: port }); this._socket.on('connect', (socket) => console.log('Connected to', `http://${host}:${port}`));
this.client1.on("message", this._handleMessage.bind(this)); this._socket.on('message', this._handleMessage.bind(this));
// this.client1.on('connect', () => console.log('redis connected'));
// this.client1.on("subscribe", (channel, count) => console.log(`subscribed to ${channel}`));
} }
subscribe(hash, password, callback) { subscribe(hash, password, callback) {
if(!this._subscriptions[hash] || this._subscriptions[hash].password !== password) { if(!this._subscriptions[hash]) {
this._subscriptions[hash] = { this._subscriptions[hash] = { head: null, callback: callback };
password: password, this._socket.emit('subscribe', { channel: hash });
head: null,
callback: callback
};
this.client1.subscribe(hash);
} }
} }
unsubscribe(hash) { unsubscribe(hash) {
this._socket.emit('unsubscribe', { channel: hash });
delete this._subscriptions[hash]; delete this._subscriptions[hash];
this.client1.unsubscribe();
this.client2.unsubscribe();
} }
publish(hash, message) { publish(hash, message) {
this.client2.publish(hash, message); this._socket.send({ channel: hash, message: message });
}
latest(hash) {
return { head: this._subscriptions[hash] ? this._subscriptions[hash].head : null };
} }
_handleMessage(hash, message) { _handleMessage(hash, message) {