ruby中的安全Websocket客户端
如何使用Ruby中的Faye websocket建立安全的TLS websocket客户端连接 我正在脚本中使用faye/websocket gemruby中的安全Websocket客户端,ruby,ssl,websocket,faye,Ruby,Ssl,Websocket,Faye,如何使用Ruby中的Faye websocket建立安全的TLS websocket客户端连接 我正在脚本中使用faye/websocket gem require 'faye/websocket' require 'eventmachine' EM.run { ws = Faye::WebSocket::Client.new('wss://aws.com/gateway',:ssl => { :private_key_file => '
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
}
2018年编辑:
这个答案已经过时了
Idio服务器被重新编写为C扩展,Websocket客户端和SSL/TLS层都没有在Ruby中实现,但SSL/TLS隧道是目前推荐的实现加密的方法
如果它是一个简单的连接,您可以使用,如果您需要,您可以向请求添加查询参数、cookie和头。。。公平地说,我是《碘宝石》的作者
应该很简单:
# 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!
websocket echo服务器在SSL上回答我。。。所以我希望这会对你有所帮助
编辑此答案已编辑,因为它继承了GRHttp的代码库并处于活动开发中,而GRHttp不再处于活动开发中。您要求的是wss,所以您得到了吗?没有,write now i Get:-在抛出“std::runtime_error”实例后调用terminate什么:加密在此事件计算机上不可用此应用程序已请求运行时以异常方式终止它。请联系应用程序的支持团队以获取更多信息。我有一个工作的websocket服务器,它只允许安全连接。