Skip to content

Commit

Permalink
Merge pull request pubnub#51 from nickveys/ruby-stack-overflow
Browse files Browse the repository at this point in the history
Ruby gem stack overflow
  • Loading branch information
stephenlb committed Nov 10, 2011
2 parents a3bc9be + d4f52a0 commit 3d004eb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
1 change: 1 addition & 0 deletions ruby/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkg
60 changes: 28 additions & 32 deletions ruby/lib/pubnub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ def subscribe(args)
## Capture User Input
channel = args['channel']
callback = args['callback']
timetoken = args['timetoken'] ? args['timetoken'] : 0

## Fail if missing channel
if !channel
Expand All @@ -114,39 +113,36 @@ def subscribe(args)
return false
end

## Begin Recusive Subscribe
begin
## Wait for Message
response = self._request([
'subscribe',
@subscribe_key,
channel,
'0',
timetoken.to_s
])

messages = response[0]
args['timetoken'] = response[1]

## If it was a timeout
if !messages.length
return self.subscribe(args)
end

## Run user Callback and Reconnect if user permits.
messages.each do |message|
if !callback.call(message)
return
## Begin Subscribe
loop do
begin
timetoken = args['timetoken'] ? args['timetoken'] : 0

## Wait for Message
response = self._request([
'subscribe',
@subscribe_key,
channel,
'0',
timetoken.to_s
])

messages = response[0]
args['timetoken'] = response[1]

## If it was a timeout
next if !messages.length

## Run user Callback and Reconnect if user permits.
messages.each do |message|
if !callback.call(message)
return
end
end
rescue Timeout::Error
rescue
sleep(1)
end

## Keep Listening.
return self.subscribe(args)
rescue Timeout::Error
return self.subscribe(args)
rescue
sleep(1)
return self.subscribe(args)
end
end

Expand Down

0 comments on commit 3d004eb

Please sign in to comment.