17
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rustを学ぶためのWebsocket

Posted at

RustでWebsocketを使ってみる。
最終的にはRaspberryPiを使ってビデオチャット機器を作りたい.

調査

websocketモジュール

wsを使用する.

[dependence]
ws = "*"

echo サーバー/クライアント

server.rs

use ws::listen;

fn main(){

    println!("Runnig Serer");

    listen("127.0.0.1:3012", |out| {
    
        move |msg| {
           let response: String = format!("{} - {}", msg, "Im server.".to_string());
           
           println!("{}", response);
           
           out.send(response)
       }
       
    }).unwrap()

    
}

client.rs

use ws::{connect, CloseCode};


fn main(){
    
    connect("ws://127.0.0.1:3012", |out| {
        out.send("Hello WebSocket").unwrap();
    
        move |msg| {
            println!("Got message: {}", msg);
            out.close(CloseCode::Normal)
        }
    }).unwrap()


}

実行:

$ cargo run --bin server

Runnig Serer

$ cargo run --bin client

Got message: Hello WebSocket - Im server.

無事, クライアントから送ったメッセージがechoされている.
echo サーバーを実装できた。

今後

  • BroadCastを実装
  • WebRTCについて調査
  • 映像+音声データの送受信
  • ブラウザWebRTCで実現している機能を、ブラウザなしのRustで実装する.

以上

17
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?