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
const host = 'localhost';
const port = 6379;
const port = 3333;
const username = 'LambOfGod';
const password = '';

View File

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

View File

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

View File

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