Files
deb-python-eventlet/doc/modules/websocket.html
2016-12-12 01:27:50 +03:00

224 lines
13 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>websocket Websocket Server &mdash; Eventlet 0.20.0 documentation</title>
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '0.20.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="top" title="Eventlet 0.20.0 documentation" href="../index.html" />
<link rel="up" title="Module Reference" href="../modules.html" />
<link rel="next" title="wsgi WSGI server" href="wsgi.html" />
<link rel="prev" title="timeout Universal Timeouts" href="timeout.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="wsgi.html" title="wsgi WSGI server"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="timeout.html" title="timeout Universal Timeouts"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Eventlet 0.20.0 documentation</a> &raquo;</li>
<li class="nav-item nav-item-1"><a href="../modules.html" accesskey="U">Module Reference</a> &raquo;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="websocket-websocket-server">
<h1><code class="xref py py-mod docutils literal"><span class="pre">websocket</span></code> &#8211; Websocket Server<a class="headerlink" href="#websocket-websocket-server" title="Permalink to this headline"></a></h1>
<p>This module provides a simple way to create a <a class="reference external" href="http://dev.w3.org/html5/websockets/">websocket</a> server. It works with a few
tweaks in the <a class="reference internal" href="wsgi.html#module-eventlet.wsgi" title="eventlet.wsgi"><code class="xref py py-mod docutils literal"><span class="pre">wsgi</span></code></a> module that allow websockets to
coexist with other WSGI applications.</p>
<p>To create a websocket server, simply decorate a handler method with
<code class="xref py py-class docutils literal"><span class="pre">WebSocketWSGI</span></code> and use it as a wsgi application:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">eventlet</span> <span class="kn">import</span> <span class="n">wsgi</span><span class="p">,</span> <span class="n">websocket</span>
<span class="kn">import</span> <span class="nn">eventlet</span>
<span class="nd">@websocket.WebSocketWSGI</span>
<span class="k">def</span> <span class="nf">hello_world</span><span class="p">(</span><span class="n">ws</span><span class="p">):</span>
<span class="n">ws</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="s2">&quot;hello world&quot;</span><span class="p">)</span>
<span class="n">wsgi</span><span class="o">.</span><span class="n">server</span><span class="p">(</span><span class="n">eventlet</span><span class="o">.</span><span class="n">listen</span><span class="p">((</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">8090</span><span class="p">)),</span> <span class="n">hello_world</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Please see graceful termination warning in <a class="reference internal" href="wsgi.html#eventlet.wsgi.server" title="eventlet.wsgi.server"><code class="xref py py-func docutils literal"><span class="pre">server()</span></code></a>
documentation</p>
</div>
<p>You can find a slightly more elaborate version of this code in the file
<code class="docutils literal"><span class="pre">examples/websocket.py</span></code>.</p>
<p>As of version 0.9.13, eventlet.websocket supports SSL websockets; all that&#8217;s necessary is to use an <a class="reference internal" href="wsgi.html#wsgi-ssl"><span>SSL wsgi server</span></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The web socket spec is still under development, and it will be necessary to change the way that this module works in response to spec changes.</p>
</div>
<span class="target" id="module-eventlet.websocket"></span><dl class="class">
<dt id="eventlet.websocket.WebSocketWSGI">
<em class="property">class </em><code class="descclassname">eventlet.websocket.</code><code class="descname">WebSocketWSGI</code><span class="sig-paren">(</span><em>handler</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.websocket.WebSocketWSGI" title="Permalink to this definition"></a></dt>
<dd><p>Wraps a websocket handler function in a WSGI application.</p>
<p>Use it like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@websocket.WebSocketWSGI</span>
<span class="k">def</span> <span class="nf">my_handler</span><span class="p">(</span><span class="n">ws</span><span class="p">):</span>
<span class="n">from_browser</span> <span class="o">=</span> <span class="n">ws</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="n">ws</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="s2">&quot;from server&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>The single argument to the function will be an instance of
<a class="reference internal" href="#eventlet.websocket.WebSocket" title="eventlet.websocket.WebSocket"><code class="xref py py-class docutils literal"><span class="pre">WebSocket</span></code></a>. To close the socket, simply return from the
function. Note that the server will log the websocket request at
the time of closure.</p>
</dd></dl>
<dl class="class">
<dt id="eventlet.websocket.WebSocket">
<em class="property">class </em><code class="descclassname">eventlet.websocket.</code><code class="descname">WebSocket</code><span class="sig-paren">(</span><em>sock</em>, <em>environ</em>, <em>version=76</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.websocket.WebSocket" title="Permalink to this definition"></a></dt>
<dd><p>A websocket object that handles the details of
serialization/deserialization to the socket.</p>
<p>The primary way to interact with a <a class="reference internal" href="#eventlet.websocket.WebSocket" title="eventlet.websocket.WebSocket"><code class="xref py py-class docutils literal"><span class="pre">WebSocket</span></code></a> object is to
call <a class="reference internal" href="#eventlet.websocket.WebSocket.send" title="eventlet.websocket.WebSocket.send"><code class="xref py py-meth docutils literal"><span class="pre">send()</span></code></a> and <a class="reference internal" href="#eventlet.websocket.WebSocket.wait" title="eventlet.websocket.WebSocket.wait"><code class="xref py py-meth docutils literal"><span class="pre">wait()</span></code></a> in order to pass messages back
and forth with the browser. Also available are the following
properties:</p>
<dl class="docutils">
<dt>path</dt>
<dd>The path value of the request. This is the same as the WSGI PATH_INFO variable,
but more convenient.</dd>
<dt>protocol</dt>
<dd>The value of the Websocket-Protocol header.</dd>
<dt>origin</dt>
<dd>The value of the &#8216;Origin&#8217; header.</dd>
<dt>environ</dt>
<dd>The full WSGI environment for this request.</dd>
</dl>
<dl class="method">
<dt id="eventlet.websocket.WebSocket.close">
<code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.websocket.WebSocket.close" title="Permalink to this definition"></a></dt>
<dd><p>Forcibly close the websocket; generally it is preferable to
return from the handler method.</p>
</dd></dl>
<dl class="method">
<dt id="eventlet.websocket.WebSocket.send">
<code class="descname">send</code><span class="sig-paren">(</span><em>message</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.websocket.WebSocket.send" title="Permalink to this definition"></a></dt>
<dd><p>Send a message to the browser.</p>
<p><em>message</em> should be convertable to a string; unicode objects should be
encodable as utf-8. Raises socket.error with errno of 32
(broken pipe) if the socket has already been closed by the client.</p>
</dd></dl>
<dl class="method">
<dt id="eventlet.websocket.WebSocket.wait">
<code class="descname">wait</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.websocket.WebSocket.wait" title="Permalink to this definition"></a></dt>
<dd><p>Waits for and deserializes messages.</p>
<p>Returns a single message; the oldest not yet processed. If the client
has already closed the connection, returns None. This is different
from normal socket behavior because the empty string is a valid
websocket message.</p>
</dd></dl>
</dd></dl>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="timeout.html"
title="previous chapter"><code class="docutils literal"><span class="pre">timeout</span></code> &#8211; Universal Timeouts</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="wsgi.html"
title="next chapter"><code class="docutils literal"><span class="pre">wsgi</span></code> &#8211; WSGI server</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/modules/websocket.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="wsgi.html" title="wsgi WSGI server"
>next</a> |</li>
<li class="right" >
<a href="timeout.html" title="timeout Universal Timeouts"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Eventlet 0.20.0 documentation</a> &raquo;</li>
<li class="nav-item nav-item-1"><a href="../modules.html" >Module Reference</a> &raquo;</li>
</ul>
</div>
<div class="footer" role="contentinfo">
&copy; Copyright 2005-2010, Eventlet Contributors.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.5.
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-42952223-1', 'eventlet.net');
ga('send', 'pageview');
</script>
</body>
</html>