websocket-rs
About Shiguredo's open source software
We will not respond to PRs or issues that have not been discussed on Discord. Also, Discord is only available in Japanese.
Please read https://github.com/shiguredo/oss before use.
時雨堂のオープンソースソフトウェアについて
利用前に https://github.com/shiguredo/oss をお読みください。
概要
Rust で実装された Sans I/O な WebSocket ライブラリです。
特徴
- Sans I/O
- HTTP/1.1 対応
使い方
クライアント (WebSocket 接続)
use ;
use TcpStream;
use ;
// 乱数ソースの実装
;
// TCP ソケット接続
let mut socket = connect?;
// WebSocket 接続オプション
let options = new;
// WebSocket 接続作成・開始
let mut ws = new;
ws.connect?;
// HTTP Upgrade リクエスト送信
while let Some = ws.poll_output
// ハンドシェイクレスポンス受信
let mut buf = ;
loop
サーバー (WebSocket 接続受付)
use ;
// WebSocketServerConnection の初期化
let options = new;
let mut ws = new;
// 受信データをフィード
// ws.feed_recv_buf(&received_data)?;
// ハンドシェイクの自動受諾
// if ws.state() == ConnectionState::Connecting {
// ws.accept_handshake_auto()?;
// }
// イベント処理
// while let Some(event) = ws.poll_event() { ... }
// 出力処理
// while let Some(output) = ws.poll_output() { ... }
メッセージ送信 (クライアント)
use CloseCode;
// テキストメッセージ送信
ws.send_text.unwrap;
// バイナリメッセージ送信
ws.send_binary.unwrap;
// Ping 送信
ws.send_ping.unwrap;
// 接続を閉じる
ws.close.unwrap;
メッセージ送信 (サーバー)
use CloseCode;
// テキストメッセージ送信
ws.send_text.unwrap;
// バイナリメッセージ送信
ws.send_binary.unwrap;
// Ping 送信
ws.send_ping.unwrap;
// 接続を閉じる
ws.close.unwrap;
フレームの直接操作 (低レベル API)
use ;
// フレーム作成
let frame = text;
let masking_key = ;
let encoded = frame.encode;
// フレームデコード
let mut decoder = new;
decoder.feed;
while let Some = decoder.decode.unwrap
WebSocket
このライブラリが対応している WebSocket の仕組みです。
フレーム
- テキストフレーム / バイナリフレーム
- 制御フレーム (Ping, Pong, Close)
- フラグメンテーション (継続フレーム)
- マスキング (クライアント→サーバー)
ハンドシェイク
- HTTP/1.1 Upgrade リクエスト/レスポンス
- Sec-WebSocket-Key / Sec-WebSocket-Accept の検証
- サブプロトコルネゴシエーション (Sec-WebSocket-Protocol)
- 拡張ネゴシエーション (Sec-WebSocket-Extensions)
拡張
- permessage-deflate (RFC 7692)
- server_no_context_takeover
- client_no_context_takeover
- server_max_window_bits
- client_max_window_bits
接続管理
- 自動 Ping/Pong 応答
- 定期的な Ping 送信 (設定可能)
- Close ハンドシェイク
- 状態管理 (Connecting, Connected, Closing, Closed)
セキュリティ
- マスキングキーの検証
- フレームサイズ制限
- UTF-8 検証 (テキストメッセージ)
制限 (DoS 対策)
デフォルト値:
- 最大フレームペイロードサイズ: 64MB
- 最大メッセージサイズ: 64MB
ClientConnectionOptions / ServerConnectionOptions で各制限値をカスタマイズ可能です。
サンプル
サンプルは Tokio と Rustls を利用しています。引数のライブラリには noargs を利用しています。
websocket_client
WS/WSS クライアントの例です。
オプション:
<URL>: 接続先 URL (ws:// または wss://)--insecure: 自己署名証明書を許可 (WSS のみ)
機能:
- WS/WSS リクエスト送信
- エコーメッセージ受信
- permessage-deflate 対応
- rustls-platform-verifier による TLS 検証
websocket_server
WS/WSS エコーサーバーの例です。
オプション:
-p, --port <PORT>: リッスンポート (デフォルト: 8080, TLS 有効時: 8443)--tls: WSS 有効化--cert <PATH>: 証明書ファイル (PEM 形式)--key <PATH>: 秘密鍵ファイル (PEM 形式)
機能:
- WS/WSS 接続受付
- テキスト/バイナリメッセージのエコーバック
- 自動 Pong 応答
websocket_reverse_proxy
WS/WSS リバースプロキシの例です。
# WS -> WS
# WS -> WSS
# WSS -> WSS (TLS 終端)
オプション:
-p, --port <PORT>: リッスンポート (デフォルト: 8080, TLS 有効時: 8443)-u, --upstream <URL>: 転送先 URL (ws:// または wss://)--tls: フロントエンドで WSS 有効化--cert <PATH>: 証明書ファイル (PEM 形式)--key <PATH>: 秘密鍵ファイル (PEM 形式)--insecure: アップストリームの自己署名証明書を許可--debug: デバッグログを有効化
機能:
- WS/WSS 接続の双方向プロキシ
- テキスト/バイナリ/Ping/Close メッセージの中継
- permessage-deflate 対応
規格書
このライブラリが準拠している RFC 一覧です。
- RFC 6455 - The WebSocket Protocol
- RFC 7692 - Compression Extensions for WebSocket
ライセンス
Apache License 2.0
Copyright 2026-2026, Shiguredo Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.