forked from butorenjin/easyrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyrtc_util.js
179 lines (155 loc) · 5.28 KB
/
easyrtc_util.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* global module, require */
/**
* Utility functions specific to EasyRTC.
*
* @module easyrtc_util
* @author Priologic Software, [email protected]
* @copyright Copyright 2016 Priologic Software. All rights reserved.
* @license BSD v2, see LICENSE file in module root folder.
*/
var util = require("util");
var _ = require("underscore"); // General utility functions external module
var g = require("./general_util"); // General utility functions local module
var e = require("./easyrtc_private_obj"); // EasyRTC private object
/**
* Object to hold EasyRTC Utility methods and classes.
*
* @class
*/
var eu = module.exports;
/**
* Disconnects socket. Failure results in a debug level log message.
*
* @param {Object} socket Socket.io connection object.
*/
eu.socketDisconnect = function(socket) {
try {
socket.disconnect();
} catch(err) {
eu.log("debug", "Socket disconnection command failed. Socket may already be disconnected.");
}
};
/**
* Custom Error Object for EasyRTC Server Errors.
*
* @extends Error
* @param {string} msg Text message describing the error.
* @returns {Error}
*/
eu.ServerError = function(msg) {
eu.ServerError.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ServerError, g.AbstractError);
eu.ServerError.prototype.name = "Server Error";
eu.ServerError.prototype.errorLevel = "error";
/**
* Custom Error Object for EasyRTC Application Errors.
*
* @extends Error
* @param {string} msg Text message describing the error.
* @returns {Error}
*/
eu.ApplicationError = function(msg) {
eu.ApplicationError.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ApplicationError, g.AbstractError);
eu.ApplicationError.prototype.name = "Application Error";
eu.ApplicationError.prototype.errorLevel = "error";
/**
* Custom Error Object for Connection Errors.
*
* @extends Error
* @param {string} msg Text message describing the error.
* @returns {Error}
*/
eu.ConnectionError = function(msg) {
eu.ConnectionError.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ConnectionError, g.AbstractError);
eu.ConnectionError.prototype.name = "Connection Error";
eu.ConnectionError.prototype.errorLevel = "error";
/**
* Custom Error Object for EasyRTC Server Warnings.
*
* @extends Error
* @param {string} msg Text message describing the error.
* @returns {Error}
*/
eu.ServerWarning = function(msg) {
eu.ServerWarning.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ServerWarning, g.AbstractError);
eu.ServerWarning.prototype.name = "Server Warning";
eu.ServerWarning.prototype.errorLevel = "warning";
/**
* Custom Error Object for EasyRTC Application Warnings.
*
* @extends Error
* @param {string} msg Text message describing the error.
* @returns {Error}
*/
eu.ApplicationWarning = function(msg) {
eu.ApplicationWarning.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ApplicationWarning, g.AbstractError);
eu.ApplicationWarning.prototype.name = "Application Warning";
eu.ApplicationWarning.prototype.errorLevel = "warning";
/**
* Custom Error Object for Connection Warnings.
*
* @extends Error
* @param {string} msg Text message describing the error.
* @returns {Error}
*/
eu.ConnectionWarning = function(msg) {
eu.ConnectionWarning.super_.call(this, msg, this.constructor);
};
util.inherits(eu.ConnectionWarning, g.AbstractError);
eu.ConnectionWarning.prototype.name = "Connection Warning";
eu.ConnectionWarning.prototype.errorLevel = "warning";
/**
* Determines if an Error object is an instance of ApplicationError, ConnectionError, or ServerError. If it is, it will return true.
*
* @param {Error} err
* @return {Boolean}
*/
eu.isError = function(err) {
if (err && ((err instanceof eu.ConnectionError)||(err instanceof eu.ApplicationError)||(err instanceof eu.ServerError)||(err instanceof Error))) {
return true;
} else {
return false;
}
};
/**
* Determines if an Error object is an instance of ApplicationWarning, ConnectionWarning, or ServerWarning. If it is, it will return true.
*
* @param {Error} err
* @return {Boolean}
*/
eu.isWarning = function(err) {
if (err && ((err instanceof eu.ConnectionWarning)||(err instanceof eu.ApplicationWarning)||(err instanceof eu.ServerWarning))) {
return true;
} else {
return false;
}
};
/**
* Returns a random available easyrtcid.
*
* @return {String} Available easyrtcid. A unique identifier for an EasyRTC connection.
*/
eu.getAvailableEasyrtcid = function() {
var newEasyrtcid = "";
var easyrtcidExists = false;
do {
newEasyrtcid = g.randomString();
easyrtcidExists = false;
for (var key in e.app) {
if (e.app[key].connection[newEasyrtcid]) {
easyrtcidExists = true;
break;
}
}
} while (easyrtcidExists);
return newEasyrtcid;
};