mirror of
https://github.com/actix/actix-website
synced 2024-12-04 20:51:54 +01:00
180 lines
15 KiB
HTML
180 lines
15 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `ws` mod in crate `actix_web`."><meta name="keywords" content="rust, rustlang, rust-lang, ws"><title>actix_web::ws - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><p class='location'>Module ws</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../index.html'>actix_web</a></p><script>window.sidebarCurrent = {name: 'ws', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/actix_web/ws/mod.rs.html#1-476' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>actix_web</a>::<wbr><a class="mod" href=''>ws</a></span></h1><div class='docblock'><p><code>WebSocket</code> support for Actix</p>
|
||
<p>To setup a <code>WebSocket</code>, first do web socket handshake then on success
|
||
convert <code>Payload</code> into a <code>WsStream</code> stream and then use <code>WsWriter</code> to
|
||
communicate with the peer.</p>
|
||
<h2 id="example" class="section-header"><a href="#example">Example</a></h2>
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">actix_web</span>::{<span class="ident">ws</span>, <span class="ident">HttpRequest</span>, <span class="ident">HttpResponse</span>};
|
||
|
||
<span class="comment">// do websocket handshake and start actor</span>
|
||
<span class="kw">fn</span> <span class="ident">ws_index</span>(<span class="ident">req</span>: <span class="kw-2">&</span><span class="ident">HttpRequest</span>) <span class="op">-></span> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">HttpResponse</span><span class="op">></span> {
|
||
<span class="ident">ws</span>::<span class="ident">start</span>(<span class="ident">req</span>, <span class="ident">Ws</span>)
|
||
}
|
||
|
||
<span class="kw">struct</span> <span class="ident">Ws</span>;
|
||
|
||
<span class="kw">impl</span> <span class="ident">Actor</span> <span class="kw">for</span> <span class="ident">Ws</span> {
|
||
<span class="kw">type</span> <span class="ident">Context</span> <span class="op">=</span> <span class="ident">ws</span>::<span class="ident">WebsocketContext</span><span class="op"><</span><span class="self">Self</span><span class="op">></span>;
|
||
}
|
||
|
||
<span class="comment">// Handler for ws::Message messages</span>
|
||
<span class="kw">impl</span> <span class="ident">StreamHandler</span><span class="op"><</span><span class="ident">ws</span>::<span class="ident">Message</span>, <span class="ident">ws</span>::<span class="ident">ProtocolError</span><span class="op">></span> <span class="kw">for</span> <span class="ident">Ws</span> {
|
||
<span class="kw">fn</span> <span class="ident">handle</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="self">self</span>, <span class="ident">msg</span>: <span class="ident">ws</span>::<span class="ident">Message</span>, <span class="ident">ctx</span>: <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="self">Self</span>::<span class="ident">Context</span>) {
|
||
<span class="kw">match</span> <span class="ident">msg</span> {
|
||
<span class="ident">ws</span>::<span class="ident">Message</span>::<span class="ident">Ping</span>(<span class="ident">msg</span>) <span class="op">=></span> <span class="ident">ctx</span>.<span class="ident">pong</span>(<span class="kw-2">&</span><span class="ident">msg</span>),
|
||
<span class="ident">ws</span>::<span class="ident">Message</span>::<span class="ident">Text</span>(<span class="ident">text</span>) <span class="op">=></span> <span class="ident">ctx</span>.<span class="ident">text</span>(<span class="ident">text</span>),
|
||
<span class="ident">ws</span>::<span class="ident">Message</span>::<span class="ident">Binary</span>(<span class="ident">bin</span>) <span class="op">=></span> <span class="ident">ctx</span>.<span class="ident">binary</span>(<span class="ident">bin</span>),
|
||
<span class="kw">_</span> <span class="op">=></span> (),
|
||
}
|
||
}
|
||
}</pre>
|
||
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class="struct" href="struct.Client.html"
|
||
title='struct actix_web::ws::Client'>Client</a></td>
|
||
<td class='docblock-short'>
|
||
<p><code>WebSocket</code> client</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="struct" href="struct.ClientHandshake.html"
|
||
title='struct actix_web::ws::ClientHandshake'>ClientHandshake</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Future that implementes client websocket handshake process.</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="struct" href="struct.ClientReader.html"
|
||
title='struct actix_web::ws::ClientReader'>ClientReader</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Websocket reader client</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="struct" href="struct.ClientWriter.html"
|
||
title='struct actix_web::ws::ClientWriter'>ClientWriter</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Websocket writer client</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="struct" href="struct.CloseReason.html"
|
||
title='struct actix_web::ws::CloseReason'>CloseReason</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Reason for closing the connection</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="struct" href="struct.Frame.html"
|
||
title='struct actix_web::ws::Frame'>Frame</a></td>
|
||
<td class='docblock-short'>
|
||
<p>A struct representing a <code>WebSocket</code> frame.</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="struct" href="struct.FramedMessage.html"
|
||
title='struct actix_web::ws::FramedMessage'>FramedMessage</a></td>
|
||
<td class='docblock-short'>
|
||
<p><code>WebSocket</code> message with framing.</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="struct" href="struct.WebsocketContext.html"
|
||
title='struct actix_web::ws::WebsocketContext'>WebsocketContext</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Execution context for <code>WebSockets</code> actors</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="struct" href="struct.WsStream.html"
|
||
title='struct actix_web::ws::WsStream'>WsStream</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Maps <code>Payload</code> stream into stream of <code>ws::Message</code> items</p>
|
||
|
||
</td>
|
||
</tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class="enum" href="enum.ClientError.html"
|
||
title='enum actix_web::ws::ClientError'>ClientError</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Websocket client error</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="enum" href="enum.CloseCode.html"
|
||
title='enum actix_web::ws::CloseCode'>CloseCode</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Status code used to indicate why an endpoint is closing the <code>WebSocket</code>
|
||
connection.</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="enum" href="enum.HandshakeError.html"
|
||
title='enum actix_web::ws::HandshakeError'>HandshakeError</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Websocket handshake errors</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="enum" href="enum.Message.html"
|
||
title='enum actix_web::ws::Message'>Message</a></td>
|
||
<td class='docblock-short'>
|
||
<p><code>WebSocket</code> Message</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="enum" href="enum.OpCode.html"
|
||
title='enum actix_web::ws::OpCode'>OpCode</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Operation codes as part of rfc6455.</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="enum" href="enum.ProtocolError.html"
|
||
title='enum actix_web::ws::ProtocolError'>ProtocolError</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Websocket protocol errors</p>
|
||
|
||
</td>
|
||
</tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class="trait" href="trait.WsWriter.html"
|
||
title='trait actix_web::ws::WsWriter'>WsWriter</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Common writing methods for a websocket.</p>
|
||
|
||
</td>
|
||
</tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class="fn" href="fn.handshake.html"
|
||
title='fn actix_web::ws::handshake'>handshake</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Prepare <code>WebSocket</code> handshake response.</p>
|
||
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class="fn" href="fn.start.html"
|
||
title='fn actix_web::ws::start'>start</a></td>
|
||
<td class='docblock-short'>
|
||
<p>Do websocket handshake and start actor</p>
|
||
|
||
</td>
|
||
</tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "actix_web";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html> |