Skip to content

Commit

Permalink
Sets close and leave to public and invalidates timers on close to pro…
Browse files Browse the repository at this point in the history
…perly dealloc a socket
  • Loading branch information
davidstump committed Jun 14, 2016
1 parent 74a0be4 commit cab14cb
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Pod/Classes/Phoenix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,28 @@ public struct Phoenix {

- parameter callback: Function to run after close
*/
func close(callback: () -> ()) {
public func close(callback: (() -> ()) = {}) {
if let connection = self.conn {
connection.delegate = nil
connection.disconnect()
}
invalidateTimers()
callback()
}

/**
Invalidate open timers to allow socket to be deallocated when closed
*/
func invalidateTimers() {
heartbeatTimer.invalidate()
reconnectTimer.invalidate()
sendBufferTimer.invalidate()

heartbeatTimer = NSTimer()
reconnectTimer = NSTimer()
sendBufferTimer = NSTimer()
}

/**
Initializes a 30s timer to let Phoenix know this device is still alive
*/
Expand All @@ -283,7 +297,7 @@ public struct Phoenix {
/**
Reconnects to a closed socket connection
*/
func reconnect() {
public func reconnect() {
close() {
self.conn = WebSocket(url: NSURL(string: self.endPoint!)!)
if let connection = self.conn {
Expand Down Expand Up @@ -391,7 +405,7 @@ public struct Phoenix {
- parameter topic: String topic name
- parameter message: Phoenix.Message payload
*/
func leave(topic topic: String, message: Phoenix.Message) {
public func leave(topic topic: String, message: Phoenix.Message) {
let leavingMessage = Phoenix.Message(subject: "status", body: "leaving")
let payload = Phoenix.Payload(topic: topic, event: "leave", message: leavingMessage)
send(payload)
Expand Down

0 comments on commit cab14cb

Please sign in to comment.