概要
mrubyだけでwebsocket通信ができるのか?という技術的興味で調べてみました。現状はクライアントサイドのみの実装がある?mruby-websockets
を使えば、mrubyのwebsocket通信ができそうです。(ただし、クライアントのみの実装)
ちなみに、環境はOS X El Capitan
です。
Dockerは既にインストールされているものとします。
サーバーを作成
rubyでwebsocketを試してみる を参考にサーバーサイドを起動。
Dockerfile
FROM ubuntu:latest
MAINTAINER Yoshihiko Yamanaka
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN sed -i.bak -e "s%http://archive.ubuntu.com/ubuntu/%http://ftp.jaist.ac.jp/pub/Linux/ubuntu/%g" /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y --force-yes build-essential git zlib1g-dev libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt-dev ruby2.0 ruby2.0-dev rake bison curl
RUN apt-get install -y --force-yes vim
# install czmq
WORKDIR /root
RUN apt-get install -y --force-yes git-all libtool pkg-config autotools-dev autoconf automake cmake uuid-dev libpcre3-dev valgrind
RUN apt-get install -y asciidoc
## build sodium
WORKDIR /root
RUN git clone https://github.com/jedisct1/libsodium /root/libsodium
WORKDIR /root/libsodium
RUN ./autogen.sh
RUN ./configure --prefix=/usr
RUN make && make check
RUN make install
## build libzmq
WORKDIR /root
RUN git clone git://github.com/zeromq/libzmq.git
WORKDIR /root/libzmq
RUN ./autogen.sh
RUN ./configure --prefix=/usr
RUN make
RUN make install
## build czmq
WORKDIR /root
RUN git clone https://github.com/zeromq/czmq
WORKDIR /root/czmq
RUN ./autogen.sh
RUN ./configure --prefix=/usr
RUN make check
RUN make install
RUN ldconfig
# install cunit
WORKDIR /root
RUN apt-get install -y --force-yes libcunit1 libcunit1-dev
# install nettle
WORKDIR /root
RUN curl -L https://ftp.gnu.org/gnu/nettle/nettle-3.2.tar.gz > nettle-3.2.tar.gz
RUN tar xzvf nettle-3.2.tar.gz
WORKDIR /root/nettle-3.2
RUN ./configure --prefix=/usr
RUN make
RUN make install
# install wslay
RUN apt-get install -y --force-yes python-sphinx
RUN git clone https://github.com/tatsuhiro-t/wslay /root/wslay
WORKDIR /root/wslay
RUN autoreconf -i
RUN automake
RUN autoconf
RUN ./configure --prefix=/usr
RUN make
# install libressl-portable
WORKDIR /root
RUN git clone https://github.com/libressl-portable/portable
WORKDIR /root/portable
RUN ./autogen.sh
RUN ./configure --prefix=/usr
RUN make && make check
RUN make install
# build mruby
RUN git clone https://github.com/mruby/mruby /root/mruby
WORKDIR /root/mruby
ADD ./build_config.rb ./build_config.rb
RUN make
WORKDIR /root/mruby/bin
build_config.rb
Dcokerfileと同じディレクトリに配置
build_config.rb
MRuby::Build.new do |conf|
toolchain :gcc
enable_debug
conf.gembox 'default'
conf.gem :github => "Asmod4n/mruby-websockets"
# C compiler settings
conf.cc do |cc|
cc.flags << [ENV['CFLAGS'] || %w(-fPIC -DHAVE_ARPA_INET_H)]
cc.include_paths << %(/root/wslay/lib)
end
end
Docker build & run
Dockerfileと同じディレクトリで
$ docker build -t websocket .
$ docker run -it websocket
クライアント作成
mruby-websockets
のExampleの通りにファイルを作成
test.rb
client = WebSocket::Client.new(:ws, "echo.websocket.org", 80, "/")
# ^^^^^^^^^^^^^^^^^^ ^^ ^^^
# ここは上で起動しているサーバー名と合わせる
client.send "hallo"
client.recv
client.close
クライアント実行
root@xxxxxx:~/mruby/bin# ./mruby test.rb
※ rubyでwebsocketを試してみるのhtmlをブラウザで開いておくとクライアントで送信したデータhello
が送信されてくるので、動いているかわかりやすい。
今後の課題
サーバーサイドを作成して、mrubyだけでwebsocket通信ができる環境を作れるといいなぁ。