/usr/share/doc/python-tornado-doc/html/websocket.html is in python-tornado-doc 4.4.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | <!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>tornado.websocket — Bidirectional communication to the browser — Tornado 4.4.3 documentation</title>
<link rel="shortcut icon" href="_static/favicon.ico"/>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="index" title="Index"
href="genindex.html"/>
<link rel="search" title="Search" href="search.html"/>
<link rel="top" title="Tornado 4.4.3 documentation" href="index.html"/>
<link rel="up" title="Web framework" href="webframework.html"/>
<link rel="next" title="HTTP servers and clients" href="http.html"/>
<link rel="prev" title="tornado.locale — Internationalization support" href="locale.html"/>
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> Tornado
</a>
<div class="version">
4.4.3
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="guide.html">User’s guide</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="webframework.html">Web framework</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="web.html"><code class="docutils literal"><span class="pre">tornado.web</span></code> — <code class="docutils literal"><span class="pre">RequestHandler</span></code> and <code class="docutils literal"><span class="pre">Application</span></code> classes</a></li>
<li class="toctree-l2"><a class="reference internal" href="template.html"><code class="docutils literal"><span class="pre">tornado.template</span></code> — Flexible output generation</a></li>
<li class="toctree-l2"><a class="reference internal" href="escape.html"><code class="docutils literal"><span class="pre">tornado.escape</span></code> — Escaping and string manipulation</a></li>
<li class="toctree-l2"><a class="reference internal" href="locale.html"><code class="docutils literal"><span class="pre">tornado.locale</span></code> — Internationalization support</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#"><code class="docutils literal"><span class="pre">tornado.websocket</span></code> — Bidirectional communication to the browser</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="http.html">HTTP servers and clients</a></li>
<li class="toctree-l1"><a class="reference internal" href="networking.html">Asynchronous networking</a></li>
<li class="toctree-l1"><a class="reference internal" href="coroutine.html">Coroutines and concurrency</a></li>
<li class="toctree-l1"><a class="reference internal" href="integration.html">Integration with other services</a></li>
<li class="toctree-l1"><a class="reference internal" href="utilities.html">Utilities</a></li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="releases.html">Release notes</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Tornado</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li><a href="webframework.html">Web framework</a> »</li>
<li><code class="docutils literal"><span class="pre">tornado.websocket</span></code> — Bidirectional communication to the browser</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/websocket.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="tornado-websocket-bidirectional-communication-to-the-browser">
<h1><code class="docutils literal"><span class="pre">tornado.websocket</span></code> — Bidirectional communication to the browser<a class="headerlink" href="#tornado-websocket-bidirectional-communication-to-the-browser" title="Permalink to this headline">¶</a></h1>
<span class="target" id="module-tornado.websocket"></span><p>Implementation of the WebSocket protocol.</p>
<p><a class="reference external" href="http://dev.w3.org/html5/websockets/">WebSockets</a> allow for bidirectional
communication between the browser and server.</p>
<p>WebSockets are supported in the current versions of all major browsers,
although older versions that do not support WebSockets are still in use
(refer to <a class="reference external" href="http://caniuse.com/websockets">http://caniuse.com/websockets</a> for details).</p>
<p>This module implements the final version of the WebSocket protocol as
defined in <a class="reference external" href="http://tools.ietf.org/html/rfc6455">RFC 6455</a>. Certain
browser versions (notably Safari 5.x) implemented an earlier draft of
the protocol (known as “draft 76”) and are not compatible with this module.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 4.0: </span>Removed support for the draft 76 protocol version.</p>
</div>
<dl class="class">
<dt id="tornado.websocket.WebSocketHandler">
<em class="property">class </em><code class="descclassname">tornado.websocket.</code><code class="descname">WebSocketHandler</code><span class="sig-paren">(</span><em>application</em>, <em>request</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>Subclass this class to create a basic WebSocket handler.</p>
<p>Override <a class="reference internal" href="#tornado.websocket.WebSocketHandler.on_message" title="tornado.websocket.WebSocketHandler.on_message"><code class="xref py py-obj docutils literal"><span class="pre">on_message</span></code></a> to handle incoming messages, and use
<a class="reference internal" href="#tornado.websocket.WebSocketHandler.write_message" title="tornado.websocket.WebSocketHandler.write_message"><code class="xref py py-obj docutils literal"><span class="pre">write_message</span></code></a> to send messages to the client. You can also
override <a class="reference internal" href="#tornado.websocket.WebSocketHandler.open" title="tornado.websocket.WebSocketHandler.open"><code class="xref py py-obj docutils literal"><span class="pre">open</span></code></a> and <a class="reference internal" href="#tornado.websocket.WebSocketHandler.on_close" title="tornado.websocket.WebSocketHandler.on_close"><code class="xref py py-obj docutils literal"><span class="pre">on_close</span></code></a> to handle opened and closed
connections.</p>
<p>See <a class="reference external" href="http://dev.w3.org/html5/websockets/">http://dev.w3.org/html5/websockets/</a> for details on the
JavaScript interface. The protocol is specified at
<a class="reference external" href="http://tools.ietf.org/html/rfc6455">http://tools.ietf.org/html/rfc6455</a>.</p>
<p>Here is an example WebSocket handler that echos back all received messages
back to the client:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">EchoWebSocket</span><span class="p">(</span><span class="n">tornado</span><span class="o">.</span><span class="n">websocket</span><span class="o">.</span><span class="n">WebSocketHandler</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">open</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"WebSocket opened"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">on_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">write_message</span><span class="p">(</span><span class="sa">u</span><span class="s2">"You said: "</span> <span class="o">+</span> <span class="n">message</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">on_close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"WebSocket closed"</span><span class="p">)</span>
</pre></div>
</div>
<p>WebSockets are not standard HTTP connections. The “handshake” is
HTTP, but after the handshake, the protocol is
message-based. Consequently, most of the Tornado HTTP facilities
are not available in handlers of this type. The only communication
methods available to you are <a class="reference internal" href="#tornado.websocket.WebSocketHandler.write_message" title="tornado.websocket.WebSocketHandler.write_message"><code class="xref py py-obj docutils literal"><span class="pre">write_message()</span></code></a>, <a class="reference internal" href="#tornado.websocket.WebSocketHandler.ping" title="tornado.websocket.WebSocketHandler.ping"><code class="xref py py-obj docutils literal"><span class="pre">ping()</span></code></a>, and
<a class="reference internal" href="#tornado.websocket.WebSocketHandler.close" title="tornado.websocket.WebSocketHandler.close"><code class="xref py py-obj docutils literal"><span class="pre">close()</span></code></a>. Likewise, your request handler class should implement
<a class="reference internal" href="#tornado.websocket.WebSocketHandler.open" title="tornado.websocket.WebSocketHandler.open"><code class="xref py py-obj docutils literal"><span class="pre">open()</span></code></a> method rather than <code class="docutils literal"><span class="pre">get()</span></code> or <code class="docutils literal"><span class="pre">post()</span></code>.</p>
<p>If you map the handler above to <code class="docutils literal"><span class="pre">/websocket</span></code> in your application, you can
invoke it in JavaScript with:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">ws</span> <span class="o">=</span> <span class="n">new</span> <span class="n">WebSocket</span><span class="p">(</span><span class="s2">"ws://localhost:8888/websocket"</span><span class="p">);</span>
<span class="n">ws</span><span class="o">.</span><span class="n">onopen</span> <span class="o">=</span> <span class="n">function</span><span class="p">()</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">"Hello, world"</span><span class="p">);</span>
<span class="p">};</span>
<span class="n">ws</span><span class="o">.</span><span class="n">onmessage</span> <span class="o">=</span> <span class="n">function</span> <span class="p">(</span><span class="n">evt</span><span class="p">)</span> <span class="p">{</span>
<span class="n">alert</span><span class="p">(</span><span class="n">evt</span><span class="o">.</span><span class="n">data</span><span class="p">);</span>
<span class="p">};</span>
</pre></div>
</div>
<p>This script pops up an alert box that says “You said: Hello, world”.</p>
<p>Web browsers allow any site to open a websocket connection to any other,
instead of using the same-origin policy that governs other network
access from javascript. This can be surprising and is a potential
security hole, so since Tornado 4.0 <a class="reference internal" href="#tornado.websocket.WebSocketHandler" title="tornado.websocket.WebSocketHandler"><code class="xref py py-obj docutils literal"><span class="pre">WebSocketHandler</span></code></a> requires
applications that wish to receive cross-origin websockets to opt in
by overriding the <a class="reference internal" href="#tornado.websocket.WebSocketHandler.check_origin" title="tornado.websocket.WebSocketHandler.check_origin"><code class="xref py py-obj docutils literal"><span class="pre">check_origin</span></code></a> method (see that
method’s docs for details). Failure to do so is the most likely
cause of 403 errors when making a websocket connection.</p>
<p>When using a secure websocket connection (<code class="docutils literal"><span class="pre">wss://</span></code>) with a self-signed
certificate, the connection from a browser may fail because it wants
to show the “accept this certificate” dialog but has nowhere to show it.
You must first visit a regular HTML page using the same certificate
to accept it before the websocket connection will succeed.</p>
</dd></dl>
<div class="section" id="event-handlers">
<h2>Event handlers<a class="headerlink" href="#event-handlers" title="Permalink to this headline">¶</a></h2>
<dl class="method">
<dt id="tornado.websocket.WebSocketHandler.open">
<code class="descclassname">WebSocketHandler.</code><code class="descname">open</code><span class="sig-paren">(</span><em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler.open"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler.open" title="Permalink to this definition">¶</a></dt>
<dd><p>Invoked when a new WebSocket is opened.</p>
<p>The arguments to <a class="reference internal" href="#tornado.websocket.WebSocketHandler.open" title="tornado.websocket.WebSocketHandler.open"><code class="xref py py-obj docutils literal"><span class="pre">open</span></code></a> are extracted from the <a class="reference internal" href="web.html#tornado.web.URLSpec" title="tornado.web.URLSpec"><code class="xref py py-obj docutils literal"><span class="pre">tornado.web.URLSpec</span></code></a>
regular expression, just like the arguments to
<a class="reference internal" href="web.html#tornado.web.RequestHandler.get" title="tornado.web.RequestHandler.get"><code class="xref py py-obj docutils literal"><span class="pre">tornado.web.RequestHandler.get</span></code></a>.</p>
</dd></dl>
<dl class="method">
<dt id="tornado.websocket.WebSocketHandler.on_message">
<code class="descclassname">WebSocketHandler.</code><code class="descname">on_message</code><span class="sig-paren">(</span><em>message</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler.on_message"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler.on_message" title="Permalink to this definition">¶</a></dt>
<dd><p>Handle incoming messages on the WebSocket</p>
<p>This method must be overridden.</p>
</dd></dl>
<dl class="method">
<dt id="tornado.websocket.WebSocketHandler.on_close">
<code class="descclassname">WebSocketHandler.</code><code class="descname">on_close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler.on_close"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler.on_close" title="Permalink to this definition">¶</a></dt>
<dd><p>Invoked when the WebSocket is closed.</p>
<p>If the connection was closed cleanly and a status code or reason
phrase was supplied, these values will be available as the attributes
<code class="docutils literal"><span class="pre">self.close_code</span></code> and <code class="docutils literal"><span class="pre">self.close_reason</span></code>.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 4.0: </span>Added <code class="docutils literal"><span class="pre">close_code</span></code> and <code class="docutils literal"><span class="pre">close_reason</span></code> attributes.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="tornado.websocket.WebSocketHandler.select_subprotocol">
<code class="descclassname">WebSocketHandler.</code><code class="descname">select_subprotocol</code><span class="sig-paren">(</span><em>subprotocols</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler.select_subprotocol"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler.select_subprotocol" title="Permalink to this definition">¶</a></dt>
<dd><p>Invoked when a new WebSocket requests specific subprotocols.</p>
<p><code class="docutils literal"><span class="pre">subprotocols</span></code> is a list of strings identifying the
subprotocols proposed by the client. This method may be
overridden to return one of those strings to select it, or
<code class="docutils literal"><span class="pre">None</span></code> to not select a subprotocol. Failure to select a
subprotocol does not automatically abort the connection,
although clients may close the connection if none of their
proposed subprotocols was selected.</p>
</dd></dl>
</div>
<div class="section" id="output">
<h2>Output<a class="headerlink" href="#output" title="Permalink to this headline">¶</a></h2>
<dl class="method">
<dt id="tornado.websocket.WebSocketHandler.write_message">
<code class="descclassname">WebSocketHandler.</code><code class="descname">write_message</code><span class="sig-paren">(</span><em>message</em>, <em>binary=False</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler.write_message"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler.write_message" title="Permalink to this definition">¶</a></dt>
<dd><p>Sends the given message to the client of this Web Socket.</p>
<p>The message may be either a string or a dict (which will be
encoded as json). If the <code class="docutils literal"><span class="pre">binary</span></code> argument is false, the
message will be sent as utf8; in binary mode any byte string
is allowed.</p>
<p>If the connection is already closed, raises <a class="reference internal" href="#tornado.websocket.WebSocketClosedError" title="tornado.websocket.WebSocketClosedError"><code class="xref py py-obj docutils literal"><span class="pre">WebSocketClosedError</span></code></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span><a class="reference internal" href="#tornado.websocket.WebSocketClosedError" title="tornado.websocket.WebSocketClosedError"><code class="xref py py-obj docutils literal"><span class="pre">WebSocketClosedError</span></code></a> was added (previously a closed connection
would raise an <a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#AttributeError" title="(in Python v3.5)"><code class="xref py py-obj docutils literal"><span class="pre">AttributeError</span></code></a>)</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 4.3: </span>Returns a <a class="reference internal" href="concurrent.html#tornado.concurrent.Future" title="tornado.concurrent.Future"><code class="xref py py-obj docutils literal"><span class="pre">Future</span></code></a> which can be used for flow control.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="tornado.websocket.WebSocketHandler.close">
<code class="descclassname">WebSocketHandler.</code><code class="descname">close</code><span class="sig-paren">(</span><em>code=None</em>, <em>reason=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler.close"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Closes this Web Socket.</p>
<p>Once the close handshake is successful the socket will be closed.</p>
<p><code class="docutils literal"><span class="pre">code</span></code> may be a numeric status code, taken from the values
defined in <a class="reference external" href="https://tools.ietf.org/html/rfc6455#section-7.4.1">RFC 6455 section 7.4.1</a>.
<code class="docutils literal"><span class="pre">reason</span></code> may be a textual message about why the connection is
closing. These values are made available to the client, but are
not otherwise interpreted by the websocket protocol.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 4.0: </span>Added the <code class="docutils literal"><span class="pre">code</span></code> and <code class="docutils literal"><span class="pre">reason</span></code> arguments.</p>
</div>
</dd></dl>
</div>
<div class="section" id="configuration">
<h2>Configuration<a class="headerlink" href="#configuration" title="Permalink to this headline">¶</a></h2>
<dl class="method">
<dt id="tornado.websocket.WebSocketHandler.check_origin">
<code class="descclassname">WebSocketHandler.</code><code class="descname">check_origin</code><span class="sig-paren">(</span><em>origin</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler.check_origin"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler.check_origin" title="Permalink to this definition">¶</a></dt>
<dd><p>Override to enable support for allowing alternate origins.</p>
<p>The <code class="docutils literal"><span class="pre">origin</span></code> argument is the value of the <code class="docutils literal"><span class="pre">Origin</span></code> HTTP
header, the url responsible for initiating this request. This
method is not called for clients that do not send this header;
such requests are always allowed (because all browsers that
implement WebSockets support this header, and non-browser
clients do not have the same cross-site security concerns).</p>
<p>Should return True to accept the request or False to reject it.
By default, rejects all requests with an origin on a host other
than this one.</p>
<p>This is a security protection against cross site scripting attacks on
browsers, since WebSockets are allowed to bypass the usual same-origin
policies and don’t use CORS headers.</p>
<p>To accept all cross-origin traffic (which was the default prior to
Tornado 4.0), simply override this method to always return true:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">check_origin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">origin</span><span class="p">):</span>
<span class="k">return</span> <span class="kc">True</span>
</pre></div>
</div>
<p>To allow connections from any subdomain of your site, you might
do something like:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">check_origin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">origin</span><span class="p">):</span>
<span class="n">parsed_origin</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="n">origin</span><span class="p">)</span>
<span class="k">return</span> <span class="n">parsed_origin</span><span class="o">.</span><span class="n">netloc</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">".mydomain.com"</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 4.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="tornado.websocket.WebSocketHandler.get_compression_options">
<code class="descclassname">WebSocketHandler.</code><code class="descname">get_compression_options</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler.get_compression_options"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler.get_compression_options" title="Permalink to this definition">¶</a></dt>
<dd><p>Override to return compression options for the connection.</p>
<p>If this method returns None (the default), compression will
be disabled. If it returns a dict (even an empty one), it
will be enabled. The contents of the dict may be used to
control the memory and CPU usage of the compression,
but no such options are currently implemented.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 4.1.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="tornado.websocket.WebSocketHandler.set_nodelay">
<code class="descclassname">WebSocketHandler.</code><code class="descname">set_nodelay</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler.set_nodelay"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler.set_nodelay" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the no-delay flag for this stream.</p>
<p>By default, small messages may be delayed and/or combined to minimize
the number of packets sent. This can sometimes cause 200-500ms delays
due to the interaction between Nagle’s algorithm and TCP delayed
ACKs. To reduce this delay (at the expense of possibly increasing
bandwidth usage), call <code class="docutils literal"><span class="pre">self.set_nodelay(True)</span></code> once the websocket
connection is established.</p>
<p>See <a class="reference internal" href="iostream.html#tornado.iostream.BaseIOStream.set_nodelay" title="tornado.iostream.BaseIOStream.set_nodelay"><code class="xref py py-obj docutils literal"><span class="pre">BaseIOStream.set_nodelay</span></code></a> for additional details.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.1.</span></p>
</div>
</dd></dl>
</div>
<div class="section" id="other">
<h2>Other<a class="headerlink" href="#other" title="Permalink to this headline">¶</a></h2>
<dl class="method">
<dt id="tornado.websocket.WebSocketHandler.ping">
<code class="descclassname">WebSocketHandler.</code><code class="descname">ping</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler.ping"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler.ping" title="Permalink to this definition">¶</a></dt>
<dd><p>Send ping frame to the remote end.</p>
</dd></dl>
<dl class="method">
<dt id="tornado.websocket.WebSocketHandler.on_pong">
<code class="descclassname">WebSocketHandler.</code><code class="descname">on_pong</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketHandler.on_pong"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketHandler.on_pong" title="Permalink to this definition">¶</a></dt>
<dd><p>Invoked when the response to a ping frame is received.</p>
</dd></dl>
<dl class="exception">
<dt id="tornado.websocket.WebSocketClosedError">
<em class="property">exception </em><code class="descclassname">tornado.websocket.</code><code class="descname">WebSocketClosedError</code><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketClosedError"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketClosedError" title="Permalink to this definition">¶</a></dt>
<dd><p>Raised by operations on a closed connection.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>
</div>
<div class="section" id="client-side-support">
<h2>Client-side support<a class="headerlink" href="#client-side-support" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="tornado.websocket.websocket_connect">
<code class="descclassname">tornado.websocket.</code><code class="descname">websocket_connect</code><span class="sig-paren">(</span><em>url</em>, <em>io_loop=None</em>, <em>callback=None</em>, <em>connect_timeout=None</em>, <em>on_message_callback=None</em>, <em>compression_options=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#websocket_connect"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.websocket_connect" title="Permalink to this definition">¶</a></dt>
<dd><p>Client-side websocket support.</p>
<p>Takes a url and returns a Future whose result is a
<a class="reference internal" href="#tornado.websocket.WebSocketClientConnection" title="tornado.websocket.WebSocketClientConnection"><code class="xref py py-obj docutils literal"><span class="pre">WebSocketClientConnection</span></code></a>.</p>
<p><code class="docutils literal"><span class="pre">compression_options</span></code> is interpreted in the same way as the
return value of <a class="reference internal" href="#tornado.websocket.WebSocketHandler.get_compression_options" title="tornado.websocket.WebSocketHandler.get_compression_options"><code class="xref py py-obj docutils literal"><span class="pre">WebSocketHandler.get_compression_options</span></code></a>.</p>
<p>The connection supports two styles of operation. In the coroutine
style, the application typically calls
<a class="reference internal" href="#tornado.websocket.WebSocketClientConnection.read_message" title="tornado.websocket.WebSocketClientConnection.read_message"><code class="xref py py-obj docutils literal"><span class="pre">read_message</span></code></a> in a loop:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">conn</span> <span class="o">=</span> <span class="k">yield</span> <span class="n">websocket_connect</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
<span class="n">msg</span> <span class="o">=</span> <span class="k">yield</span> <span class="n">conn</span><span class="o">.</span><span class="n">read_message</span><span class="p">()</span>
<span class="k">if</span> <span class="n">msg</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">break</span>
<span class="c1"># Do something with msg</span>
</pre></div>
</div>
<p>In the callback style, pass an <code class="docutils literal"><span class="pre">on_message_callback</span></code> to
<code class="docutils literal"><span class="pre">websocket_connect</span></code>. In both styles, a message of <code class="docutils literal"><span class="pre">None</span></code>
indicates that the connection has been closed.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Also accepts <code class="docutils literal"><span class="pre">HTTPRequest</span></code> objects in place of urls.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 4.1: </span>Added <code class="docutils literal"><span class="pre">compression_options</span></code> and <code class="docutils literal"><span class="pre">on_message_callback</span></code>.
The <code class="docutils literal"><span class="pre">io_loop</span></code> argument is deprecated.</p>
</div>
</dd></dl>
<dl class="class">
<dt id="tornado.websocket.WebSocketClientConnection">
<em class="property">class </em><code class="descclassname">tornado.websocket.</code><code class="descname">WebSocketClientConnection</code><span class="sig-paren">(</span><em>io_loop</em>, <em>request</em>, <em>on_message_callback=None</em>, <em>compression_options=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketClientConnection"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketClientConnection" title="Permalink to this definition">¶</a></dt>
<dd><p>WebSocket client connection.</p>
<p>This class should not be instantiated directly; use the
<a class="reference internal" href="#tornado.websocket.websocket_connect" title="tornado.websocket.websocket_connect"><code class="xref py py-obj docutils literal"><span class="pre">websocket_connect</span></code></a> function instead.</p>
<dl class="method">
<dt id="tornado.websocket.WebSocketClientConnection.close">
<code class="descname">close</code><span class="sig-paren">(</span><em>code=None</em>, <em>reason=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketClientConnection.close"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketClientConnection.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Closes the websocket connection.</p>
<p><code class="docutils literal"><span class="pre">code</span></code> and <code class="docutils literal"><span class="pre">reason</span></code> are documented under
<a class="reference internal" href="#tornado.websocket.WebSocketHandler.close" title="tornado.websocket.WebSocketHandler.close"><code class="xref py py-obj docutils literal"><span class="pre">WebSocketHandler.close</span></code></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 4.0: </span>Added the <code class="docutils literal"><span class="pre">code</span></code> and <code class="docutils literal"><span class="pre">reason</span></code> arguments.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="tornado.websocket.WebSocketClientConnection.write_message">
<code class="descname">write_message</code><span class="sig-paren">(</span><em>message</em>, <em>binary=False</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketClientConnection.write_message"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketClientConnection.write_message" title="Permalink to this definition">¶</a></dt>
<dd><p>Sends a message to the WebSocket server.</p>
</dd></dl>
<dl class="method">
<dt id="tornado.websocket.WebSocketClientConnection.read_message">
<code class="descname">read_message</code><span class="sig-paren">(</span><em>callback=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/tornado/websocket.html#WebSocketClientConnection.read_message"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#tornado.websocket.WebSocketClientConnection.read_message" title="Permalink to this definition">¶</a></dt>
<dd><p>Reads a message from the WebSocket server.</p>
<p>If on_message_callback was specified at WebSocket
initialization, this function will never return messages</p>
<p>Returns a future whose result is the message, or None
if the connection is closed. If a callback argument
is given it will be called with the future when it is
ready.</p>
</dd></dl>
</dd></dl>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="http.html" class="btn btn-neutral float-right" title="HTTP servers and clients" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="locale.html" class="btn btn-neutral" title="tornado.locale — Internationalization support" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2009-2017, The Tornado Authors.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'4.4.3',
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>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>
|