From 4e67df95733e69f86b438423c7057f3573031503 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 2 Aug 2012 11:30:30 +0200 Subject: [PATCH] Added ssl support for web sockets. --- app/assets/javascripts/app/lib/websocket.js.coffee | 6 +++++- script/websocket-server.rb | 10 ++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/app/lib/websocket.js.coffee b/app/assets/javascripts/app/lib/websocket.js.coffee index 79226e912..32638380d 100644 --- a/app/assets/javascripts/app/lib/websocket.js.coffee +++ b/app/assets/javascripts/app/lib/websocket.js.coffee @@ -57,7 +57,11 @@ class _Singleton extends Spine.Controller ) return - @ws = new window.WebSocket( "ws://" + window.location.hostname + ":6042/" ) + protocol = 'ws://' + if window.location.protocol is 'https:' + protocol = 'wss://' + + @ws = new window.WebSocket( protocol + window.location.hostname + ":6042/" ) # Set event handlers. @ws.onopen = => diff --git a/script/websocket-server.rb b/script/websocket-server.rb index 31d09bc38..eddf04025 100644 --- a/script/websocket-server.rb +++ b/script/websocket-server.rb @@ -15,6 +15,7 @@ options = { :k => '/path/to/server.key', :c => '/path/to/server.crt', } +tls_options = {} OptionParser.new do |opts| opts.banner = "Usage: websocket-server.rb [options]" @@ -28,10 +29,10 @@ OptionParser.new do |opts| options[:s] = s end opts.on("-k", "--private-key [OPT]", "/path/to/server.key for secure connections") do |k| - options[:k] = k + tls_options[:private_key_file] = k end opts.on("-c", "--certificate [OPT]", "/path/to/server.crt for secure connections") do |c| - options[:c] = c + tls_options[:cert_chain_file] = c end end.parse! @@ -40,10 +41,7 @@ puts "Starting websocket server on #{ options[:b] }:#{ options[:p] } (secure:#{ @clients = {} EventMachine.run { - EventMachine::WebSocket.start( :host => options[:b], :port => options[:p], :secure => options[:s], :tls_options => { - :private_key_file => options[:p], - :cert_chain_file => options[:c], - }) do |ws| + EventMachine::WebSocket.start( :host => options[:b], :port => options[:p], :secure => options[:s], :tls_options => tls_options ) do |ws| # register client connection ws.onopen {