UNPKG

expresser

Version:

A ready-to-use platform for Node.js web apps, built on top of Express.

323 lines (176 loc) 12.9 kB
<!DOCTYPE html> <html> <head> <title>sockets.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" /> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div class="container"> <div class="page"> <div class="header"> <h1>sockets.coffee</h1> <div class="toc"> <h3>Table of Contents</h3> <ol> <li> <a class="source" href="index.html"> index.coffee </a> </li> <li> <a class="source" href="app.html"> app.coffee </a> </li> <li> <a class="source" href="cron.html"> cron.coffee </a> </li> <li> <a class="source" href="database.html"> database.coffee </a> </li> <li> <a class="source" href="downloader.html"> downloader.coffee </a> </li> <li> <a class="source" href="events.html"> events.coffee </a> </li> <li> <a class="source" href="firewall.html"> firewall.coffee </a> </li> <li> <a class="source" href="imaging.html"> imaging.coffee </a> </li> <li> <a class="source" href="logger.html"> logger.coffee </a> </li> <li> <a class="source" href="mailer.html"> mailer.coffee </a> </li> <li> <a class="source" href="settings.html"> settings.coffee </a> </li> <li> <a class="source" href="sockets.html"> sockets.coffee </a> </li> <li> <a class="source" href="utils.html"> utils.coffee </a> </li> </ol> </div> </div> <h2 id="expresser-sockets">EXPRESSER SOCKETS</h2> <p>Handles sockets communication using the module Socket.IO. ATTENTION! The Sockets module is started automatically by the App module. If you wish to disable it, set <code>Settings.sockets.enabled</code> to false. <!-- @see Settings.sockets --></p> <div class='highlight'><pre><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sockets</span></span> lodash = <span class="hljs-built_in">require</span> <span class="hljs-string">"lodash"</span> logger = <span class="hljs-built_in">require</span> <span class="hljs-string">"./logger.coffee"</span> settings = <span class="hljs-built_in">require</span> <span class="hljs-string">"./settings.coffee"</span></pre></div> <p>@property [Array] Holds a list of current event listeners.</p> <div class='highlight'><pre> <span class="hljs-attribute">currentListeners</span>: <span class="hljs-literal">null</span></pre></div> <p>@property [Socket.IO Object] Exposes Socket.IO object to external modules.</p> <div class='highlight'><pre> <span class="hljs-attribute">io</span>: <span class="hljs-literal">null</span></pre></div> <h2 id="init">INIT</h2> <p>Bind the Socket.IO object to the Express app. This will also set the counter to increase / decrease when users connects or disconnects from the app. @param [Object] options Sockets init options. @option options [Object] server The Express server object to bind to.</p> <div class='highlight'><pre> <span class="hljs-attribute">init</span>: <span class="hljs-function"><span class="hljs-params">(options)</span> =&gt;</span> options = {<span class="hljs-attribute">server</span>: options} <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> options.server? <span class="hljs-property">@currentListeners</span> = [] <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> options.server? logger.error <span class="hljs-string">"Sockets.init"</span>, <span class="hljs-string">"App server is invalid. Abort!"</span> <span class="hljs-keyword">return</span> <span class="hljs-property">@io</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">"socket.io"</span>) options.server</pre></div> <p>Listen to user connection count updates.</p> <div class='highlight'><pre> <span class="hljs-property">@io</span>.sockets.<span class="hljs-literal">on</span> <span class="hljs-string">"connection"</span>, <span class="hljs-function"><span class="hljs-params">(socket)</span> =&gt;</span> <span class="hljs-property">@io</span>.emit <span class="hljs-string">"connection-count"</span>, <span class="hljs-property">@getConnectionCount</span>() socket.<span class="hljs-literal">on</span> <span class="hljs-string">"disconnect"</span>, <span class="hljs-property">@onDisconnect</span></pre></div> <p>Bind all current event listeners.</p> <div class='highlight'><pre> <span class="hljs-keyword">for</span> listener <span class="hljs-keyword">in</span> <span class="hljs-property">@currentListeners</span> socket.<span class="hljs-literal">on</span> listener.key, listener.callback <span class="hljs-keyword">if</span> listener?</pre></div> <h2 id="events">EVENTS</h2> <p>Emit the specified key and data to clients. @param [String] key The event key. @param [Object] data The JSON data to be sent out to clients.</p> <div class='highlight'><pre> <span class="hljs-attribute">emit</span>: <span class="hljs-function"><span class="hljs-params">(key, data)</span> =&gt;</span> <span class="hljs-keyword">if</span> <span class="hljs-property">@io</span>? <span class="hljs-property">@io</span>.emit key, data logger.debug <span class="hljs-string">"Sockets.emit"</span>, key, JSON.stringify(data).length + <span class="hljs-string">" bytes"</span> <span class="hljs-keyword">else</span> logger.debug <span class="hljs-string">"Sockets.emit"</span>, key, <span class="hljs-string">"Sockets not initiated yet, abort!"</span></pre></div> <p>Listen to a specific event. If <code>onlyNewClients</code> is true then it won’t listen to that particular event from currently connected clients. @param [String] key The event key. @param [Method] callback The callback to be called when key is triggered. @param [Boolean] onlyNewClients Optional, if true, listen to event only from new clients.</p> <div class='highlight'><pre> <span class="hljs-attribute">listenTo</span>: <span class="hljs-function"><span class="hljs-params">(key, callback, onlyNewClients)</span> =&gt;</span> <span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> callback? onlyNewClients = <span class="hljs-literal">false</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> onlyNewClients? <span class="hljs-property">@currentListeners</span>.push {<span class="hljs-attribute">key</span>: key, <span class="hljs-attribute">callback</span>: callback} <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> onlyNewClients <span class="hljs-keyword">for</span> key, socket <span class="hljs-keyword">of</span> <span class="hljs-property">@io</span>.sockets.connected socket.<span class="hljs-literal">on</span> key, callback logger.debug <span class="hljs-string">"Sockets.listenTo"</span>, key</pre></div> <p>Stops listening to the specified event key. @param [String] key The event key. @param [Object] callback The callback to stop triggering.</p> <div class='highlight'><pre> <span class="hljs-attribute">stopListening</span>: <span class="hljs-function"><span class="hljs-params">(key, callback)</span> =&gt;</span> <span class="hljs-keyword">for</span> socketKey, socket <span class="hljs-keyword">of</span> <span class="hljs-property">@io</span>.sockets.connected <span class="hljs-keyword">if</span> callback? socket.removeListener key, callback <span class="hljs-keyword">else</span> socket.removeAllListeners key</pre></div> <p>Remove binding from the currentListeners collection.</p> <div class='highlight'><pre> <span class="hljs-keyword">for</span> listener <span class="hljs-keyword">in</span> <span class="hljs-property">@currentListeners</span> <span class="hljs-keyword">if</span> listener.key <span class="hljs-keyword">is</span> key <span class="hljs-keyword">and</span> (listener.callback <span class="hljs-keyword">is</span> callback <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> callback?) listener = <span class="hljs-literal">null</span> logger.debug <span class="hljs-string">"Sockets.stopListening"</span>, key</pre></div> <p>Remove invalid and expired event listeners.</p> <div class='highlight'><pre> <span class="hljs-attribute">compact</span>:<span class="hljs-function"> =&gt;</span> <span class="hljs-property">@currentListeners</span> = lodash.compact <span class="hljs-property">@currentListeners</span></pre></div> <h2 id="helpers">HELPERS</h2> <p>Get how many users are currenly connected to the app.</p> <div class='highlight'><pre> <span class="hljs-attribute">getConnectionCount</span>:<span class="hljs-function"> =&gt;</span> <span class="hljs-keyword">return</span> Object.keys(<span class="hljs-property">@io</span>.sockets.connected).length</pre></div> <p>When user disconnects, emit an event with the new connection count to all clients.</p> <div class='highlight'><pre> <span class="hljs-attribute">onDisconnect</span>:<span class="hljs-function"> =&gt;</span> count = <span class="hljs-property">@getConnectionCount</span>() logger.debug <span class="hljs-string">"Sockets.onDisconnect"</span>, <span class="hljs-string">"New count: <span class="hljs-subst">#{count}</span>."</span></pre></div> <h2 id="singleton-implementation">Singleton implementation</h2> <div class='highlight'><pre>Sockets.<span class="hljs-function"><span class="hljs-title">getInstance</span> = -&gt;</span> <span class="hljs-property">@instance</span> = <span class="hljs-keyword">new</span> Sockets() <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> <span class="hljs-property">@instance</span>? <span class="hljs-keyword">return</span> <span class="hljs-property">@instance</span> <span class="hljs-built_in">module</span>.<span class="hljs-built_in">exports</span> = <span class="hljs-built_in">exports</span> = Sockets.getInstance()</pre></div> <div class="fleur">h</div> </div> </div> </body> </html>