webgme-engine
Version:
WebGME server and Client API without a GUI
218 lines (182 loc) • 10.2 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: server/middleware/websocket-router/WebsocketRouter.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: server/middleware/websocket-router/WebsocketRouter.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*globals requireJS, module*/
'use strict';
const CONSTANTS = requireJS('common/storage/constants');
function __connectUserToRouter(router, socketId, handles) {
router._handles[socketId] = handles;
}
/** Class for individual users connected via websocket to the websocket router. */
class WebsocketRouterUser {
/**
* Create a WebsocketRouterUser (normally called automatically as a response to a connect event).
* @param {object} socket - The socket that just joined the router specific room (connected).
* @param {object} router - The router where the user was connecting.
*/
constructor(socket, router) {
this._socket = socket;
this._id = socket.id;
this.userId = socket.userId;
this._router = router;
const handleObject = {};
handleObject[CONSTANTS.WEBSOCKET_ROUTER_MESSAGE_TYPES.MESSAGE] = (payload, callback) => {
this._msgHandle(payload, callback);
};
handleObject[CONSTANTS.WEBSOCKET_ROUTER_MESSAGE_TYPES.DISCONNECT] = (payload, callback) => {
this._discHandle(payload, callback);
};
__connectUserToRouter(this._router, this._id, handleObject);
}
/**
* Sends a message to the connected users.
* @param {object} payload - The content object of the message (has to be stringifiable!).
*/
send(payload) {
this._socket.emit('websocketRouterMessage', {
routerId: this._router.getRouterId(),
messageType: CONSTANTS.WEBSOCKET_ROUTER_MESSAGE_TYPES.MESSAGE,
payload: payload,
});
}
/**
* Forcefully disconnects the connected users (notification will be sent).
* @param {Error} error - The error that describes the reason for disconnect.
*/
disconnect(err) {
this._socket.emit('websocketRouterMessage', {
routerId: this._router.getRouterId(),
messageType: CONSTANTS.WEBSOCKET_ROUTER_MESSAGE_TYPES.DISCONNECT,
payload: err.message,
});
}
/**
* Sets the message handle of the user.
* @param {function} handleFn - The function that will be called once the user send a message.
*/
onMessage(handleFn) {
this._msgHandle = handleFn;
}
/**
* Sets the disconnect handle of the user.
* @param {function} handleFn - The function that will be called once the user disconnects from the service.
*/
onDisconnect(handleFn) {
this._discHandle = handleFn;
}
}
/** Class for websocket funcionalities associated with routers. */
class WebsocketRouter {
/**
* Create a WebsocketRouter.
* @param {object} websocket - The websocket server that the router will be attahced to.
* @param {string} routerId - The id of the router 'usually its name'.
* Had to be known by the client user to be able to sucessfully connect.
*/
constructor(websocket, routerId) {
this._id = CONSTANTS.WEBSOCKET_ROUTER_ROOM_ID_PREFIX + routerId;
this._routerId = routerId;
this._sockets = {};
this._handles = {};
this._onConnectHandle = (user, callback) => {callback(null);};
const handleObject = {};
handleObject[CONSTANTS.WEBSOCKET_ROUTER_MESSAGE_TYPES.CONNECT] = (socket, callback) => {
this._sockets[socket.id] = socket;
socket.join(this._id);
const user = new WebsocketRouterUser(socket, this);
this._onConnectHandle(user, callback);
};
handleObject[CONSTANTS.WEBSOCKET_ROUTER_MESSAGE_TYPES.DISCONNECT] = (socketId, payload, callback) => {
// user initiated disconnect
this._handles[socketId][CONSTANTS.WEBSOCKET_ROUTER_MESSAGE_TYPES.DISCONNECT](payload, callback);
this._sockets[socketId].leave(this._id);
delete this._handles[socketId];
delete this._sockets[socketId];
};
handleObject[CONSTANTS.WEBSOCKET_ROUTER_MESSAGE_TYPES.MESSAGE] = (socketId, payload, callback) => {
this._handles[socketId][CONSTANTS.WEBSOCKET_ROUTER_MESSAGE_TYPES.MESSAGE](payload, callback);
};
this._ws = websocket.handleWebsocketRouterMessages(routerId, handleObject);
}
/**
* Sets the event handling function of user connect.
* @param {function} handleFn - The function that needs to be called when a user connects.
* The function should handle two arguments. The first will be a WebsocketRouterUser object,
* while the second is a callback (highlighting with an error object if we accept the connection).
*/
onConnect(handleFn) {
this._onConnectHandle = handleFn;
}
/**
* Broadcasts a message to all connected users.
* @param {object} payload - The content object of the message (has to be stringifiable!).
*/
send(payload) {
this._ws.to(this._id).emit('websocketRouterMessage', {
routerId: this._routerId,
messageType: CONSTANTS.WEBSOCKET_ROUTER_MESSAGE_TYPES.MESSAGE,
payload: payload,
});
}
/**
* Forcefully disconnects all connected users (they got a notification).
* @param {Error} error - The error that describes the reason for disconnect.
*/
disconnect(error) {
this._ws.to(this._id).emit('websocketRouterMessage', {
routerId: this._routerId,
messageType: CONSTANTS.WEBSOCKET_ROUTER_MESSAGE_TYPES.DISCONNECT,
payload: error.message,
});
Object.keys(this._sockets).forEach(socketId => {
this._sockets[socketId].leave(this._id);
});
this._sockets = {};
this._handles = {};
}
/**
* Returns the websocket room id.
* @return {string} The full id of the websocket room that is used
* to separate users of this router.
*/
getRoomId() {
return this._id;
}
/**
* Returns the websocket router id.
* @return {string} The router id of the router.
*/
getRouterId() {
return this._routerId;
}
}
module.exports = WebsocketRouter;</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="Server_GMEAuth.html">Server:GMEAuth</a></li><li><a href="Server_SafeStorage.html">Server:SafeStorage</a></li><li><a href="Server_UserProject.html">Server:UserProject</a></li><li><a href="module-Core.html">Core</a></li><li><a href="module-Storage.html">Storage</a></li><li><a href="module-crosscuts.html">crosscuts</a></li><li><a href="module-serialization.html">serialization</a></li></ul><h3>Externals</h3><ul><li><a href="external-Promise.html">Promise</a></li></ul><h3>Classes</h3><ul><li><a href="AddOnBase.html">AddOnBase</a></li><li><a href="AddOnUpdateResult.html">AddOnUpdateResult</a></li><li><a href="Artifact.html">Artifact</a></li><li><a href="BlobClient.html">BlobClient</a></li><li><a href="BlobMetadata.html">BlobMetadata</a></li><li><a href="BlobRunPluginClient.html">BlobRunPluginClient</a></li><li><a href="Client.html">Client</a></li><li><a href="Core.html">Core</a></li><li><a href="ExecutorClient.html">ExecutorClient</a></li><li><a href="GMENode.html">GMENode</a></li><li><a href="GmeLogger.html">GmeLogger</a></li><li><a href="InterPluginResult.html">InterPluginResult</a></li><li><a href="JobInfo.html">JobInfo</a></li><li><a href="OutputInfo.html">OutputInfo</a></li><li><a href="PluginBase.html">PluginBase</a></li><li><a href="PluginConfig.html">PluginConfig</a></li><li><a href="PluginMessage.html">PluginMessage</a></li><li><a href="PluginNodeDescription.html">PluginNodeDescription</a></li><li><a href="PluginResult.html">PluginResult</a></li><li><a href="Project.html">Project</a></li><li><a href="ProjectInterface.html">ProjectInterface</a></li><li><a href="Server_GMEAuth-GMEAuth.html">GMEAuth</a></li><li><a href="Server_SafeStorage-SafeStorage.html">SafeStorage</a></li><li><a href="Server_UserProject-UserProject.html">UserProject</a></li><li><a href="WebsocketRouter.html">WebsocketRouter</a></li><li><a href="WebsocketRouterUser.html">WebsocketRouterUser</a></li></ul><h3>Events</h3><ul><li><a href="Client.html#event:BRANCH_CHANGED">BRANCH_CHANGED</a></li><li><a href="Client.html#event:BRANCH_CLOSED">BRANCH_CLOSED</a></li><li><a href="Client.html#event:BRANCH_OPENED">BRANCH_OPENED</a></li><li><a href="Client.html#event:BRANCH_STATUS_CHANGED">BRANCH_STATUS_CHANGED</a></li><li><a href="Client.html#event:CONNECTED_USERS_CHANGED">CONNECTED_USERS_CHANGED</a></li><li><a href="Client.html#event:NETWORK_STATUS_CHANGED">NETWORK_STATUS_CHANGED</a></li><li><a href="Client.html#event:NOTIFICATION">NOTIFICATION</a></li><li><a href="Client.html#event:PLUGIN_FINISHED">PLUGIN_FINISHED</a></li><li><a href="Client.html#event:PLUGIN_INITIATED">PLUGIN_INITIATED</a></li><li><a href="Client.html#event:PLUGIN_NOTIFICATION">PLUGIN_NOTIFICATION</a></li><li><a href="Client.html#event:PROJECT_CLOSED">PROJECT_CLOSED</a></li><li><a href="Client.html#event:PROJECT_OPENED">PROJECT_OPENED</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Jun 21 2024 09:43:40 GMT-0400 (Eastern Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>