secure Websocket client in ruby

房东的猫 提交于 2019-12-08 11:12:42

问题


How can I make a secure (TLS) websocket client connection using Faye-websocket in Ruby?

I am using faye/websocket gem in my script.

    require 'faye/websocket'
    require 'eventmachine'

    EM.run {
      ws = Faye::WebSocket::Client.new('wss://aws.com/gateway',:ssl => {
    :private_key_file => 'path/to/ssl.key',
    :cert_chain_file  => 'path/to/ssl.crt'
  }, :headers => { 'Authorization' => 'Basic bXl1c2VyOm15cGFzc3dvcmQ='})

      ws.on :open do |event|
        p [:open]
        ws.send('Hello, world!')
      end

      ws.on :message do |event|
        p [:message, event.data]
      end

      ws.on :close do |event|
        p [:close, event.code, event.reason]
        ws = nil
      end
    }

回答1:


EDIT 2018:

This answer is outdated.

The iodine server was re-written as a C extension and neither the Websocket client nor the SSL/TLS layer have been implemented in Ruby just yet (SSL/TLS tunneling is currently the recommended way to achieve encryption).


You can use probably use Iodine's websocket client if it's a simple connection (you can add query parameters, cookies and headers to the request, if you need them)... Fair notice, I am the author of the Iodine gem.

It should be simple:

# load the Http extension which includes a websocket client and server
require 'iodine/http'
# As long as Iodine.protocol isn't a Class, Iodine will only perform tasks
Iodine.protocol = :timers

# We will use this as our 'on_open' callback.
on_open_proc = Proc.new do
     puts 'Connection opened'
     # `#write` is defined in the WebsocketClient
     # This Proc runs within the instance's context.
     # It's like defining a method in a subclass.
     write 'Hello World!'
end
# We will use this as our 'on_message(data)' callback.
on_message_proc = Proc.new {|data| puts data }
# We will use this as our 'on_close' callback.
# It's only called if the connection isn't automatically renewed.
# In our case, unless the server shuts down, it won't be called.
on_close_proc = Proc.new { puts "Connection wasn't renewed..." }
# We will use this for "polling" data.
on_timer_proc = Proc.new { write "The time is #{Time.now}" }

# test client:
Iodine::Http.ws_connect 'wss://echo.websocket.org',
       on_message: on_message_proc,
       on_open: on_open_proc,
       on_close: on_close_proc,
       every: 5, send: on_timer_proc,
       renew: 5,
       cookies: {'my_cookie' => 'value of my cookie'} #,
       # ssl_key: 'key_data', ssl_cert: 'cert_data'

# If you are running Iodine within irb, use `exit`:
exit 

# If you are running Iodine within an existing server application,
# you will have to force it to start while your script is running:
# Iodine.force_start!

The websocket echo server answers me on SSL... So I hope this will help you.

EDIT This answer was edited since Iodine inherited GRHttp's codebase and is in active development, whereas GRHttp is no longer in active development.



来源:https://stackoverflow.com/questions/29801789/secure-websocket-client-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!