UNPKG

4.19 kBJavaScriptView Raw
1'use strict';
2import {catsnakeConfig} from './config';
3
4import {
5 csModClientid,
6 csModStringify,
7 csModPublish,
8 csModInfo,
9 csModSubscribe,
10 csModGrant,
11 csModDeny
12} from './modules/core/index';
13
14import {
15 csModHistory
16} from './modules/persistance/index';
17
18/**
19 * Creates a new CatSnake client.
20 * @class
21 */
22class CatSnake {
23 /**
24 * @constructs CatSnake
25 * @param {string} address - the address of the catsnake server
26 * @param {object} options - options such as common name and others
27 */
28 constructor(address, options) {
29 this.socket = new WebSocket(address);
30 this.socket.binaryType = 'arraybuffer';
31
32 this.connected = false;
33
34 // Genrate a unique clientid
35 this.client = csModClientid();
36
37 this.commonName = (options.commonName) ?
38 options.commonName : config.defaultName;
39
40 // Fired when the connection is made to the server
41 this.socket.onopen = event => {
42 this.connected = true;
43
44 // Make sure we tell the server we're leaving.
45 window.onbeforeunload = () => {
46 this.socket.close();
47 };
48 };
49 }
50
51 stringify(data, callback) {
52 /**
53 * Tries to return a stringified object.
54 * @function stringify (internal)
55 * @param {object} data - the object to attempt to stringify
56 * @callback {string} - Returns a stringified object
57 */
58 return csModStringify(data, callback);
59 }
60
61 /**
62 * Publishes a message to all subscribers
63 * @function publish
64 * @param {string} channel - the channel to publish to
65 * @param {object} data - the object to publish
66 * @param {string} privateKey - optional private key for private channels
67 */
68 publish(channel, data, privateKey) {
69 csModPublish(channel, data, privateKey, this);
70 }
71
72 /**
73 * List channels, get client info.
74 * @function info
75 * @param {string} channel - the channel to look at
76 * @param {object} data - additional information for request
77 * @param {object} opts - additional options for subscriptions
78 * @param {string} opts.privateKey - private key used for getting info from private channels
79 */
80 info(channel, data, opts) {
81 csModInfo(channel, data, opts, this);
82 }
83
84 /**
85 * Get message history from a channel.
86 * @function history
87 * @param {string} channel - the channel to pull history from
88 * @param {number} limit - the ammount of items to pull from history
89 * @param {object} opts - options such as privateKeys
90 * @param {string} opts.privateKey - private key used for getting history from private channels
91 */
92 history(channel, limit, opts) {
93 csModHistory(channel, limit, opts, this);
94 }
95
96 /**
97 * Subscribe to a channel
98 * @function subscribe
99 * @param {string} channel - the channel to subscribe to
100 * @param {function} callback - new messages are returned here via msg
101 * @param {object} callback.msg - a new payload published to this channel
102 * @param {object} opts - additional options for subscriptions
103 * @param {string} opts.privateKey - private key used for subscribing to private channels
104 * @param {string} opts.noself - subscribe for everything but ignore your own payloads
105 * @param {string} opts.accessToken - used as a key to modify private channels. Not to be confused with privateKey
106 * @param {string} opts.private - make this channel private, clients can only connect if granted access
107 */
108 subscribe(channel, callback, opts) {
109 csModSubscribe(channel, callback, opts, this);
110 }
111
112 /**
113 * Deny a client access to a channel
114 * @function deny
115 * @param {string} channel - the channel in which to deny the client from
116 * @param {string} client - the client to deny
117 * @param {string} secret - the secret key associated with this channel
118 */
119 deny(channel, client, secret) {
120 return csModDeny(channel, client, secret, this);
121 }
122
123 /**
124 * Grant a client access to a channel
125 * @function grant
126 * @param {string} channel - the channel in which to grant the client access to
127 * @param {string} client - the client to grant access
128 * @param {string} secret - the secret key associated with this channel
129 */
130 grant(channel, client, secret) {
131 return cdModGrant(channel, client, secret, this);
132 }
133}