UNPKG

3.77 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 csModStringify
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 publish(channel, data, privateKey) {
62 /**
63 * Publishes a message to all subscribers
64 * @function csModPublish
65 * @param {string} channel - the channel to publish to
66 * @param {object} data - the object to publish
67 * @param {string} privateKey - optional private key for private channels
68 * @param {this} this - this inheratance
69 */
70 csModPublish(channel, data, privateKey, this);
71 }
72
73 info(channel, data, opts) {
74 /**
75 * List all clients
76 * @function csModInfo
77 * @param {string} channel - the channel to look at
78 * @param {object} data - additional information for request
79 * @param {object} opts - additional options for subscriptions
80 * @param {this} this - this inheratance
81 */
82 csModInfo(channel, data, opts, this);
83 }
84
85 history(channel, limit, opts) {
86 /**
87 * List all clients
88 * @function csModHistory
89 * @param {string} channel - the channel to pull history from
90 * @param {number} limit - the ammount of items to pull from history
91 * @param {object} opts - options such as privateKeys
92 * @param {this} this - this inheratance
93 */
94 csModHistory(channel, limit, opts, this);
95 }
96
97 subscribe(channel, callback, opts) {
98 /**
99 * Subscribe to a channel
100 * @function csModSubscribe
101 * @param {string} channel - the channel to subscribe to
102 * @callback {function} callback - new messages are returned here via msg
103 * @param {object} opts - additional options for subscriptions
104 * @param {this} this - this inheratance
105 */
106 csModSubscribe(channel, callback, opts, this);
107 }
108
109 deny(channel, client, secret) {
110 /**
111 * Deny a client access to a channel
112 * @function csModDeny
113 * @param {string} channel - the channel in which to deny the client from
114 * @param {string} client - the client to deny
115 * @param {string} secret - the secret key associated with this channel
116 */
117 return csModDeny(channel, client, secret, this);
118 }
119
120 grant(channel, client, secret) {
121 /**
122 * Grant a client access to a channel
123 * @function csModGrant
124 * @param {string} channel - the channel in which to grant the client access to
125 * @param {string} client - the client to grant access
126 * @param {string} secret - the secret key associated with this channel
127 */
128 return cdModGrant(channel, client, secret, this);
129 }
130}