-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstalync-client.js
More file actions
124 lines (106 loc) · 3.73 KB
/
stalync-client.js
File metadata and controls
124 lines (106 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var basicDataType = [ "string", "number", "boolean" ];
class Client {
/**
* Put your stalync credentials for specific application
* the credentials located in stalync profile page.
* @param {string} developerID
* @param {string} applicationName
*/
constructor(developerID, applicationName) {
/** @private */
this.developerID = developerID;
/** @private */
this.applicationName = applicationName;
/** @private */
this.eventMap = {};
/** @private */
this.allEventCallback;
/**
* @private
* @type {WebSocket}
*/
this.websocketClient;
}
connect() {
let URL = `ws://ws.stalync.tech?developerID=${this.developerID}&applicationName=${this.applicationName}`;
if(typeof window === "undefined") var window = {};
if ('WebSocket' in window) {
this.websocketClient = new WebSocket(URL);
} else if ('MozWebSocket' in window) {
this.websocketClient = new MozWebSocket(URL);
} else if(typeof require === "function") {
let WS = require('websocket').w3cwebsocket;
this.websocketClient = new WS(URL);
} else {
this.websocketClient = new SockJS(URL);
}
/**
* @param {string} eventName
* @param {string=} value
*/
let callUserCallback = (eventName, value) => {
if(eventName == "*") {
if(this.allEventCallback) this.allEventCallback(value);
} else {
let currentCallback = this.eventMap[eventName];
if(currentCallback) currentCallback(eventName, value);
}
};
this.websocketClient.onclose = (_) => { callUserCallback("$disconnected") };
this.websocketClient.onerror = (_) => { callUserCallback("$error") };
this.websocketClient.onopen = (_) => { callUserCallback("$connected") };
this.websocketClient.onmessage = (event) => {
let payload = JSON.parse(event.data);
callUserCallback(payload.eventName, payload.message);
}
}
/**
* @param {string} eventName
* @param {string | number | boolean | Object} message
*/
emitEvent(eventName, message) {
if(typeof eventName !== "string") {
return console.error("Error event name expected to be a string in function emitEvent");
}
/** @type {string} */
let convertedMessage;
if(basicDataType.indexOf(typeof message) >= 0) {
convertedMessage = String(message);
} else if(!message) {
convertedMessage = "";
} else {
convertedMessage = JSON.stringify(message);
}
this.websocketClient.send(
JSON.stringify({
message: convertedMessage,
eventName,
})
);
}
/**
* @param {string} eventName
* @param {function(string)} callback
*/
onEvent(eventName, callback) {
if(typeof eventName !== "string") {
return console.error("Error event name expected to be a string in function onEvent");
}
if(typeof callback !== "function") {
return console.error("Error callback expected to be a function with one string parameter");
}
this.eventMap[eventName] = callback;
}
/**
* @param {function(string)} callback
*/
onMessage(callback) {
if(typeof callback !== "function") {
return console.error("Error callback expected to be a function with one string parameter");
}
this.allEventCallback = callback;
}
}
if(typeof module === "object") {
module.exports = { Client };
}