rust websocket

サーバーアプリ

extern crate ws;

use ws::{listen, Sender, Handler, Result, Message, CloseCode, Handshake};
use ws::Message::Text;
use ws::Message::Binary;
use ws::util::Token;

struct Server {
   out: Sender,
}

impl Handler for Server {
  fn on_open(&mut self, _shake: Handshake) -> Result<()> {
	  println!("on_open");
	  Ok(())
  }
  fn on_message(&mut self, msg: Message) -> Result<()> {
	  println!("{:?}", msg);
	  Ok(())
  }
}

fn main() {
  listen("[IP]:[PORT]", |out| Server { out: out }).unwrap();
}
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>hello node.js!</title>
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script>
	$(document).ready(function(){
	console.log("hello");
	var sock = new WebSocket('ws://:[IP]:[PORT]');

	sock.addEventListener('open', function(e){
		console.log('Socket open');
	});

	sock.addEventListener('message', function(e){
		console.log(e.data);
	});

	$(':button[name=buttonMsg]').click(function(){
		var msg = $(':text[name=message]').val();
		console.log(msg);
		sock.send(msg);
	});
	});
    </script>
  </head>
  <body>
    <input type="text" name="message" value="">
    <input type="button" name="buttonMsg" value="send message">
    <input type="button" name="buttonDiscon" value="disconnect">
  </body>
</html>

このクライアントをraspiに置いておいて、apacheとかで見られるようにしておけばOK

f:id:katakanan:20181129000745p:plain
OK