Skip to content

Commit

Permalink
Added Spotify.disconnect() method, fixed bug where heartbeat would st…
Browse files Browse the repository at this point in the history
…ill trigger after disconnection
  • Loading branch information
fuzeman committed Jul 2, 2014
1 parent ad1341c commit a586c10
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
5 changes: 4 additions & 1 deletion spotify/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def connect(self):
log.info('Connecting...')
self._resolve_ap()

def disconnect(self):
self.components.connection.disconnect()

# Resolve AP
def _resolve_ap(self):
params = {
Expand Down Expand Up @@ -131,7 +134,7 @@ def on_user_info(self, message):

if catalogue != 'premium':
self.emit('error', 'Please upgrade to premium (catalogue: %s)' % repr(catalogue))
self.components.connection.disconnect()
self.disconnect()
return

self.emit('login')
Expand Down
2 changes: 1 addition & 1 deletion spotify/commands/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def on_error(self, message):
log.warn('Error returned from command handler, disconnecting...')

# Trigger disconnection
self.sp.components.connection.disconnect()
self.sp.disconnect()

# Fire 'error'
self.emit('error', message)
23 changes: 10 additions & 13 deletions spotify/components/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from spotify.core.request import Request

from pyemitter import Emitter
from threading import Lock, Thread
from threading import Lock, Timer
from ws4py.client.threadedclient import WebSocketClient
import json
import logging
Expand All @@ -22,7 +22,7 @@ def __init__(self, sp):
self.connected = False
self.disconnecting = False

self.heartbeat_thread = None
self.heartbeat_timer = None

self.seq = 0
self.requests = {}
Expand All @@ -33,7 +33,9 @@ def reset(self):
self.connected = False
self.disconnecting = False

self.heartbeat_thread = None
if self.heartbeat_timer:
self.heartbeat_timer.cancel()
self.heartbeat_timer = None

self.seq = 0
self.requests = {}
Expand Down Expand Up @@ -109,9 +111,12 @@ def on_close(self, code, reason=None):

if self.disconnecting:
log.debug('Client requested disconnect, ignoring "close" event')
self.reset()
return

self.disconnect()
self.reset()

self.emit('close', code=code, reason=reason)

def send(self, name, *args):
Expand Down Expand Up @@ -163,14 +168,6 @@ def send_heartbeat(self):
.pipe('error', self)\
.send()

def heartbeat(self):
while self.connected:
time.sleep(self.heartbeat_interval)

self.send_heartbeat()

log.debug('heartbeat thread finished (disconnected)')

def on_connect(self, message):
log.debug('SpotifyConnection "connect" event: %s', message)

Expand All @@ -183,8 +180,8 @@ def on_connect(self, message):
self.connected = True

# Start heartbeats ('sp/echo')
self.heartbeat_thread = Thread(target=self.heartbeat)
self.heartbeat_thread.start()
self.heartbeat_timer = Timer(self.heartbeat_interval, self.send_heartbeat)
self.heartbeat_timer.start()

self.emit('connect')

Expand Down

0 comments on commit a586c10

Please sign in to comment.