ssl - secure Websocket client in ruby -


how can make secure (tls) websocket client connection using faye-websocket in ruby?

i using faye/websocket gem in 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 |event|         p [:open]         ws.send('hello, world!')       end        ws.on :message |event|         p [:message, event.data]       end        ws.on :close |event|         p [:close, event.code, event.reason]         ws = nil       end     } 

you can use use iodine's websocket client if it's simple connection (you can add query parameters, cookies , headers request, if need them)... fair notice, author of iodine gem.

it should simple:

# load http extension includes websocket client , server require 'iodine/http' # long iodine.protocol isn't class, iodine perform tasks iodine.protocol = :timers  # use our 'on_open' callback. on_open_proc = proc.new      puts 'connection opened'      # `#write` defined in websocketclient      # proc runs within instance's context.      # it's defining method in subclass.      write 'hello world!' end # use our 'on_message(data)' callback. on_message_proc = proc.new {|data| puts data } # use our 'on_close' callback. # it's called if connection isn't automatically renewed. # in our case, unless server shuts down, won't called. on_close_proc = proc.new { puts "connection wasn't renewed..." } # use "polling" data. on_timer_proc = proc.new { write "the time #{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 cookie'} #,        # ssl_key: 'key_data', ssl_cert: 'cert_data'  # if running iodine within irb, use `exit`: exit   # if running iodine within existing server application, # have force start while script running: # iodine.force_start! 

the websocket echo server answers me on ssl... hope you.

edit answer edited since iodine inherited grhttp's codebase , in active development, whereas grhttp no longer in active development.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -