Skip to content

Commit 1c3705c

Browse files
committed
add pusher modules
1 parent 5c32862 commit 1c3705c

12 files changed

+98
-18
lines changed

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "A Javascript API to access kadcontrade",
55
"main": "target/ktapi-full.js",
66
"scripts": {
7-
"prepublish": "rollup -c"
7+
"prepublish": "rollup -c && rollup --config rollup.config.noPusher.js",
8+
"clean": "rm -r target/"
89
},
910
"repository": {
1011
"type": "git",
@@ -21,7 +22,8 @@
2122
"homepage": "https://github.com/125m125/ktapi-js#readme",
2223
"dependencies": {
2324
"d3-request": "^1.0.6",
24-
"jssha": "^2.3.1"
25+
"jssha": "^2.3.1",
26+
"pusher": "^1.5.1"
2527
},
2628
"devDependencies": {
2729
"rollup": "^0.50.0"

rollup.config.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
export default {
2-
input: 'src/index.js',
2+
input: 'src/full.js',
33
name: 'Kt',
44
output: {
55
file: 'target/ktapi-full.js',
6-
format: 'umd',
6+
format: 'umd'
77
},
8-
external: ['jsSHA','d3-request'],
8+
external: ['jsSHA','d3-request','pusher'],
99
globals: {
1010
'jsSHA': 'jsSHA',
11-
'd3-request': 'd3'
11+
'd3-request': 'd3',
12+
'pusher': 'Pusher'
1213
}
1314
}

rollup.config.noPusher.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default {
2+
input: 'src/noPusher.js',
3+
name: 'Kt',
4+
output: {
5+
file: 'target/ktapi-noPusher.js',
6+
format: 'umd',
7+
},
8+
external: ['jsSHA','d3-request','pusher'],
9+
globals: {
10+
'jsSHA': 'jsSHA',
11+
'd3-request': 'd3',
12+
'pusher': 'Pusher'
13+
}
14+
}

src/full.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export {default as KtApi} from './ktapi/index.js'
2+
export * from './authenticators/authenticators.js'
3+
import './pusher/index.js'

src/ktapi/core.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export default function (uid, tid, tkn, authenticator) {
66
"tid": tid,
77
"tkn": tkn
88
};
9-
9+
this.getUserID = function () {
10+
return user.uid;
11+
}
1012
this.getRequest = function (method, type, suburl, params, headers, auth) {
1113
var requestAuthenticator;
1214
if (auth) {

src/ktapi/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import './messages.js';
55
import './orderbook.js';
66
import './payouts.js';
77
import './permissions.js';
8-
import './pusherAuth.js';
98
import './trades.js';
109

1110
export default KtApi;

src/ktapi/pusherAuth.js

-10
This file was deleted.
File renamed without changes.

src/pusher/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './pusherAuth.js';
2+
import './pusherCore.js';
3+
import './pusherSubscribtions.js';

src/pusher/pusherAuth.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { default as KtApi } from '../ktapi/core.js';
2+
3+
KtApi.prototype.pusherAuthenticate = function (socket, channel, callback) {
4+
var params;
5+
params = {
6+
"user": this.getUserID(), // FIXME
7+
"channel_name": channel,
8+
"socketId": socket
9+
};
10+
this.performRequest("POST", "json", "pusher/authenticate?user={user}", params, null, true, callback);
11+
};

src/pusher/pusherCore.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { default as KtApi } from '../ktapi/core.js';
2+
import Pusher from 'pusher';
3+
import './pusherAuth';
4+
5+
KtApi.prototype.pusherSubscribe = function (channelname, callback) {
6+
if (!this.pusher) {
7+
var self = this;
8+
var superCallback = callback;
9+
this.pusher = new Pusher('25ba65999fadc5a6e290', {
10+
cluster: 'eu',
11+
encrypted: true,
12+
authorizer: function (channel, options) {
13+
return {
14+
authorize: function (socketId, callback) {
15+
self.pusherAuthenticate(socketId, channel.name, function (err, data) {
16+
if (err) {
17+
superCallback(err);
18+
return callback(err);
19+
}
20+
callback(false, JSON.parse(data.authdata));
21+
});
22+
}
23+
};
24+
}
25+
});
26+
}
27+
this.pusher.subscribe(channelname).bind('update', function(data) {
28+
callback(false, JSON.parse(data));
29+
});
30+
}

src/pusher/pusherSubscribtions.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { default as KtApi } from '../ktapi/core.js';
2+
import './pusherCore';
3+
4+
function subscribe(ktapi, name, selfCreated, callback) {
5+
ktapi.pusherSubscribe('private-' + ktapi.getUserID() + '_r' + name + (selfCreated ? '.selfCreated' : ''), callback);
6+
}
7+
8+
KtApi.prototype.subscribeItems = function (callback, selfCreated) {
9+
subscribe(this, 'Items', selfCreated, callback);
10+
}
11+
KtApi.prototype.subscribeOrders = function (callback, selfCreated) {
12+
subscribe(this, 'Orders', selfCreated, callback);
13+
}
14+
KtApi.prototype.subscribePayouts = function (callback, selfCreated) {
15+
subscribe(this, 'Payouts', selfCreated, callback);
16+
}
17+
KtApi.prototype.subscribeMessages = function (callback, selfCreated) {
18+
subscribe(this, 'Messages', selfCreated, callback);
19+
}
20+
KtApi.prototype.subscribeOrderbook = function (callback) {
21+
this.pusherSubscribe('order', callback);
22+
}
23+
KtApi.prototype.subscribeHistory = function (callback) {
24+
this.pusherSubscribe('history', callback);
25+
}

0 commit comments

Comments
 (0)