1
0

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.

mrubyでWebSocketを使ってみた

Last updated at Posted at 2017-01-28

Asmod4nさんのmruby-websocketsを動かしてみます。

環境

  • OSX ElCapitan

手順

依存ライブラリはぜんぶbrewで入れらるので入れます。

brew install libressl
brew install wslay
brew install libsodium

次に、mrubyのheadをcloneしてきます。

理由-----------

最初、mruby-cliで生成した雛形からビルドしようとしていたのだが、

error: implicit declaration of function 'mrb_int_mul_overflow'

というのがでた。
mrb_int_mul_overflowはmruby-websocketが依存しているmruby-sysrandomで使われているようなのだけど、この関数はmruby 1.2.0には存在しない

mruby-cliが取得してくるmrubyがリリース版(1.2.0)だったので、mrb_int_mul_overflowが存在しないようだった。

-------理由終わり

git clone https://github.com/mruby/mruby.git
cd mruby

次に、build_config.rbを編集します。

MRuby::Build.new do |conf|
  # load specific toolchain settings

  # Gets set by the VS command prompts.
  if ENV['VisualStudioVersion'] || ENV['VSINSTALLDIR']
    toolchain :visualcpp
  else
    toolchain :gcc
  end

  enable_debug

  conf.gem :github => 'Asmod4n/mruby-websockets' do |g|
    g.cc.include_paths << '/usr/local/opt/libressl/include'
    g.linker.library_paths << '/usr/local/opt/libressl/lib'
  end
  conf.gem :github => 'Asmod4n/mruby-poll'
  conf.gem :github => 'Asmod4n/mruby-tls' do |g|
    g.cc.include_paths << '/usr/local/opt/libressl/include'
    g.linker.library_paths << '/usr/local/opt/libressl/lib'
  end
  conf.gem :github => 'Asmod4n/mruby-wslay' do |g|
    g.cc.include_paths << '/usr/local/opt/wslay/include'
    g.cc.include_paths << '/usr/local/opt/libsodium/include'
    g.linker.library_paths << '/usr/local/opt/wslay/lib'
    g.linker.library_paths << '/usr/local/opt/libsodium/lib'
  end
end

ビルドします。

rake

bin/mirbができているはずなので、実行してみます。

./bin/mirb

mirbが起動したら、echo.websocket.orgを利用して、WebSocketで通信できるかどうか試してみます。

mirb - Embeddable Interactive Ruby Shell

> WebSocket
 => WebSocket
> client = WebSocket::Client.new(:ws, "echo.websocket.org", 80, "/")
 => #<WebSocket::Client:0x7fea1a81fdb0 @connection=#<WebSocket::WsConnection:0x7fea1a81fd20>>
> client.send "hallo"
 => #<WebSocket::Client:0x7fea1a81fdb0 @connection=#<WebSocket::WsConnection:0x7fea1a81fd20>>
> client.recv
 => #<struct Wslay::Event::OnMsgRecvArg rsv=0, opcode=:text_frame, msg="hallo", status_code=nil>
> client.close
 => [#<struct Wslay::Event::OnMsgRecvArg rsv=0, opcode=:connection_close, msg="\003\350", status_code=:normal_closure>]

いけましたね。

参考

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?