Permalink
Browse files

Merge branch '6.x' into gh-pages

  • Loading branch information...
2 parents 71b6656 + 1b25e0f commit 808c62a8b7e57af68007d36acb34d05292766213 @shimaore shimaore committed Mar 21, 2017
View
@@ -0,0 +1,391 @@
+<!DOCTYPE html>
+
+<html>
+<head>
+ <title>Express-side API to bind with Socket.IO</title>
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8">
+ <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
+ <link rel="stylesheet" media="all" href="docco.css" />
+</head>
+<body>
+ <div id="container">
+ <div id="background"></div>
+
+ <ul id="jump_to">
+ <li>
+ <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
+ <a class="small" href="javascript:void(0);">+</a>
+ <div id="jump_wrapper">
+ <div id="jump_page_wrapper">
+ <div id="jump_page">
+
+
+ <a class="source" href="io-session.coffee.html">
+ io-session.coffee.md
+ </a>
+
+
+ <a class="source" href="route.coffee.html">
+ route.coffee.md
+ </a>
+
+
+ <a class="source" href="seemify.coffee.html">
+ seemify.coffee.md
+ </a>
+
+
+ <a class="source" href="settings.coffee.html">
+ settings.coffee.md
+ </a>
+
+
+ <a class="source" href="zappa.coffee.html">
+ zappa.coffee.md
+ </a>
+
+ </div>
+ </div>
+ </li>
+ </ul>
+
+ <ul class="sections">
+
+
+
+ <li id="section-1">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-1">&#182;</a>
+ </div>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre>pkg = <span class="hljs-built_in">require</span> <span class="hljs-string">'../package.json'</span>
+debug = (<span class="hljs-built_in">require</span> <span class="hljs-string">'debug'</span>) <span class="hljs-string">"<span class="hljs-subst">#{pkg.name}</span>:io-session"</span>
+uuid = <span class="hljs-built_in">require</span> <span class="hljs-string">'node-uuid'</span></pre></div></div>
+
+ </li>
+
+
+ <li id="section-2">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-2">&#182;</a>
+ </div>
+ <h1 id="express-side-api-to-bind-with-socket-io">Express-side API to bind with Socket.IO</h1>
+
+ </div>
+
+ </li>
+
+
+ <li id="section-3">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-3">&#182;</a>
+ </div>
+ <p>API used by the client (e.g. <code>zappajs-client</code>) to create an Express-side key that will be used to bind Express and Socket.io sessions.</p>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre>@bind_express = <span class="hljs-function"><span class="hljs-params">({context})</span> -&gt;</span>
+ {app} = context
+
+ zappa_prefix = app.settings.zappa_prefix
+ context.get zappa_prefix+<span class="hljs-string">'/socket/:channel_name/:socket_id'</span>, <span class="hljs-function">-&gt;</span>
+ <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> context.session_store?
+ debug <span class="hljs-string">'Missing session-store.'</span>
+ @res.status <span class="hljs-number">500</span>
+ @json error:<span class="hljs-string">'No session-store.'</span>
+ <span class="hljs-keyword">return</span>
+
+ <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> @session?
+ debug <span class="hljs-string">'Missing session.'</span>
+ @res.status <span class="hljs-number">400</span>
+ @json error:<span class="hljs-string">'No session'</span>
+ <span class="hljs-keyword">return</span>
+
+ channel_name = @params.channel_name
+ socket_id = @params.socket_id</pre></div></div>
+
+ </li>
+
+
+ <li id="section-4">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-4">&#182;</a>
+ </div>
+ <p>Use memoized socket data if available.</p>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre> @session.__socket ?= {}
+
+ <span class="hljs-keyword">if</span> @session.__socket[channel_name]?
+ @json
+ key: @session.__socket[channel_name].key
+ <span class="hljs-keyword">return</span></pre></div></div>
+
+ </li>
+
+
+ <li id="section-5">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-5">&#182;</a>
+ </div>
+ <p>Create a new socket session document
+The <code>key</code> is used to hide the actual <code>@session.id</code> from the
+client while allowing it to provide us with a pointer to the
+session document using the key.</p>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre> key = uuid.v4() <span class="hljs-comment"># used for socket 'authentication'</span></pre></div></div>
+
+ </li>
+
+
+ <li id="section-6">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-6">&#182;</a>
+ </div>
+ <p>Update the store.</p>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre> data =
+ id: @session.id <span class="hljs-comment"># local Express Session ID</span>
+ cookie: {}
+ context.session_store.set key, data, <span class="hljs-function"><span class="hljs-params">(err)</span> =&gt;</span>
+ <span class="hljs-keyword">if</span> err
+ @res.status <span class="hljs-number">500</span>
+ @json error: err.toString()
+ <span class="hljs-keyword">return</span></pre></div></div>
+
+ </li>
+
+
+ <li id="section-7">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-7">&#182;</a>
+ </div>
+ <p>Save the key and socket.id in the local Express session store.</p>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre> @session.__socket[channel_name] =
+ id: socket_id
+ key: key</pre></div></div>
+
+ </li>
+
+
+ <li id="section-8">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-8">&#182;</a>
+ </div>
+ <p>Let the client know which key it should use on the Socket.IO side.</p>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre> @json
+ key: key
+ <span class="hljs-keyword">return</span></pre></div></div>
+
+ </li>
+
+
+ <li id="section-9">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-9">&#182;</a>
+ </div>
+ <h1 id="retrieve-the-session">Retrieve the session</h1>
+
+ </div>
+
+ </li>
+
+
+ <li id="section-10">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-10">&#182;</a>
+ </div>
+ <p>Socket.io-side retrieval of the bound session.</p>
+<h2 id="bind-with-express">Bind with Express</h2>
+
+ </div>
+
+ </li>
+
+
+ <li id="section-11">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-11">&#182;</a>
+ </div>
+ <p>The special event <code>__zappa_key</code> is used by the client to notify us of the key provided by Express.</p>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre>@bind_io = <span class="hljs-function"><span class="hljs-params">({context})</span> -&gt;</span>
+
+ context.<span class="hljs-literal">on</span> <span class="hljs-string">'__zappa_key'</span>, <span class="hljs-function"><span class="hljs-params">({key},ack)</span> -&gt;</span>
+
+ <span class="hljs-keyword">unless</span> ack?
+ debug <span class="hljs-string">'Client did not request `ack` for __zappa_key'</span>
+ <span class="hljs-keyword">return</span>
+
+ <span class="hljs-keyword">unless</span> context.session_store?
+ debug <span class="hljs-string">'Missing session-store.'</span>
+ ack error:<span class="hljs-string">'Missing session-store.'</span>
+ <span class="hljs-keyword">unless</span> key?
+ debug <span class="hljs-string">'Missing key.'</span>
+ ack error:<span class="hljs-string">'Missing key.'</span>
+ <span class="hljs-keyword">return</span></pre></div></div>
+
+ </li>
+
+
+ <li id="section-12">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-12">&#182;</a>
+ </div>
+ <p>Retrieve the data record associated with the key.</p>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre> context.session_store.get key, <span class="hljs-function"><span class="hljs-params">(err,data)</span> =&gt;</span>
+ <span class="hljs-keyword">if</span> err?
+ debug <span class="hljs-string">'session_store.get #{key}: #{err}'</span>
+ ack error:err.toString()
+ <span class="hljs-keyword">return</span>
+ <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> data?
+ debug <span class="hljs-string">'session_store.get #{key}: Missing data'</span>
+ ack error:<span class="hljs-string">'Missing data'</span>
+ <span class="hljs-keyword">return</span></pre></div></div>
+
+ </li>
+
+
+ <li id="section-13">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-13">&#182;</a>
+ </div>
+ <p>Bind the session.id so that the handlers can access the session.</p>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre> @client.__session_id = data.id
+ ack {key}</pre></div></div>
+
+ </li>
+
+
+ <li id="section-14">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-14">&#182;</a>
+ </div>
+ <h1 id="middleware-for-sockets">Middleware for sockets</h1>
+
+ </div>
+
+ </li>
+
+
+ <li id="section-15">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-15">&#182;</a>
+ </div>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre>@bind_middleware = <span class="hljs-function"><span class="hljs-params">({context})</span> -&gt;</span>
+<span class="hljs-function">
+ <span class="hljs-title">get_session</span> = <span class="hljs-params">(ctx,next)</span> -&gt;</span>
+ session_id = ctx.client.__session_id
+ <span class="hljs-keyword">unless</span> context.session_store? <span class="hljs-keyword">and</span> session_id?
+ debug <span class="hljs-string">'Session Store is not ready, `@session` will not be available.'</span>
+ next()
+ <span class="hljs-keyword">return</span>
+
+ req =
+ sessionID: session_id
+ sessionStore: context.session_store</pre></div></div>
+
+ </li>
+
+
+ <li id="section-16">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-16">&#182;</a>
+ </div>
+ <p>Retrieve the session data stored by Express</p>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre> context.session_store.get session_id, <span class="hljs-function"><span class="hljs-params">(error,data)</span> -&gt;</span>
+ <span class="hljs-keyword">if</span> error
+ debug <span class="hljs-string">"get_session() <span class="hljs-subst">#{error}</span>"</span>
+ next()
+ <span class="hljs-keyword">return</span></pre></div></div>
+
+ </li>
+
+
+ <li id="section-17">
+ <div class="annotation">
+
+ <div class="pilwrap ">
+ <a class="pilcrow" href="#section-17">&#182;</a>
+ </div>
+ <p>Set <code>@req.session</code> just like Express-session does, and add a <code>@session</code> shortcut just like Zappa does.</p>
+
+ </div>
+
+ <div class="content"><div class='highlight'><pre> ctx.session = ctx.req.session = <span class="hljs-keyword">new</span> context.session.Session req, data
+ next()
+ <span class="hljs-keyword">return</span>
+
+ context.io_use (ctx,res,next) -&gt;
+ get_session ctx, <span class="hljs-function">-&gt;</span>
+ v = next()
+ <span class="hljs-keyword">if</span> v?.<span class="hljs-keyword">then</span>?
+ v.<span class="hljs-keyword">then</span> -&gt; ctx.session?.save()
+ <span class="hljs-keyword">else</span>
+ ctx.session?.save()</pre></div></div>
+
+ </li>
+
+ </ul>
+ </div>
+</body>
+</html>
Oops, something went wrong.

0 comments on commit 808c62a

Please sign in to comment.