Skip to content

Commit

Permalink
Merge pull request #5 from zvkemp/connect_opts
Browse files Browse the repository at this point in the history
support per-request connection options or proc
  • Loading branch information
zvkemp authored Oct 4, 2017
2 parents 8f887e7 + 61b7b06 commit 293117d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
26 changes: 22 additions & 4 deletions lib/phoenix/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
module Phoenix
class Socket
include MonitorMixin
attr_reader :path, :socket, :inbox, :topic, :join_options
attr_accessor :verbose
attr_reader :path, :socket, :inbox, :topic
attr_accessor :verbose, :join_options_proc, :connect_options_proc

def initialize(topic, join_options: {}, path: 'ws://localhost:4000/socket/websocket')
def initialize(topic, join_options: {}, connect_options: {}, path: 'ws://localhost:4000/socket/websocket')
@path = path
@topic = topic
@join_options = join_options
@connect_options = connect_options
@inbox = Phoenix::Inbox.new(ttl: 15)
super() # MonitorMixin
@inbox_cond = new_cond
Expand Down Expand Up @@ -50,6 +51,16 @@ def request_reply(event:, payload: {}, timeout: 5) # timeout in seconds
end
end

def join_options
return @join_options unless join_options_proc
join_options_proc.call(@join_options)
end

def connect_options
return @connect_options unless connect_options_proc
connect_options_proc.call(@connect_options)
end

private

attr_reader :inbox_cond, :thread_ready
Expand Down Expand Up @@ -120,6 +131,13 @@ def handle_open(event)
end
end

def build_path
uri = URI.parse(path)
existing_query = CGI.parse(uri.query || '')
uri.query = URI.encode_www_form(existing_query.merge(connect_options))
uri.to_s
end

def spawn_thread
return if @spawned || connection_alive?
log 'spawning...'
Expand All @@ -129,7 +147,7 @@ def spawn_thread
EM.run do
synchronize do
log 'em.run.sync'
@socket = Faye::WebSocket::Client.new(path)
@socket = Faye::WebSocket::Client.new(build_path)
socket.on :open do |event|
handle_open(event)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/phoenix/socket/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Rb
module Phoenix
module Socket
VERSION = "0.2.0"
VERSION = "0.3.0"
end
end
end

0 comments on commit 293117d

Please sign in to comment.