Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #71 from jariz/fix-connection
Browse files Browse the repository at this point in the history
Fix connection
  • Loading branch information
jariz authored Jan 19, 2018
2 parents 7524136 + 678bc82 commit 9c878eb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "0"
version = "2.0">
</Bucket>
1 change: 1 addition & 0 deletions Noti/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let file = FileDestination() // Adding file destination of log output
let console = ConsoleDestination() // log to Xcode Console
console.format = "$DHH:mm:ss$d $L $M" // clean console log
file.format = "$Ddd-MM-yyyy HH:mm:ss$d $L $F:$l - $M" // clean file log format
var url = try? FileManager.default.url(for: .libraryDirectory, // getting ~/Library/
in: .userDomainMask,
appropriateFor: nil,
Expand Down
27 changes: 20 additions & 7 deletions Noti/PushManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
center.delegate = self
self.initCrypt()

log.debug("Getting user info...")
log.debug("Init Noti")
getUserInfo {
self.connect()
}
Expand All @@ -48,15 +48,22 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
disconnect(attemptReconnect:false)
}

@objc internal func disconnectForTimeout() {
log.warning("Disconnected for timeout (nop not received)")
disconnect(attemptReconnect: true)
}

@objc internal func disconnect(attemptReconnect: Bool = true) {
log.warning("Trigged disconnect")
log.warning("Triggered disconnect attemptReconnect:\(attemptReconnect), isConnected:\(self.socket!.isConnected)")
//stops attempts to reconnect
if !attemptReconnect {
self.killed = true
}

//disconnect now!
self.socket!.disconnect(forceTimeout: 0)
if self.socket!.isConnected {
self.socket!.disconnect(forceTimeout: 0)
}
}

func initCrypt() {
Expand Down Expand Up @@ -88,6 +95,7 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate

var _callback:(() -> Void)? = nil
func getUserInfo(_ callback: (() -> Void)?) {
log.debug("Getting user info...")
//todo: this is kinda dirty ...
self._callback = callback

Expand All @@ -114,7 +122,6 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
} else if response.result.error != nil {
log.error("response error requesting user info")
if callback == nil {
self.killed = true
self.disconnect(attemptReconnect:false)
self.setState("Failed to log in.")
} else {
Expand Down Expand Up @@ -261,6 +268,7 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
}

func connect() {
log.debug("Connecting to Pushbullet")
socket!.delegate = self
socket!.connect()
}
Expand All @@ -276,11 +284,11 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
}


log.debug("PushManager", "Is connected")
log.debug("PushManager is connected")
}

func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
log.warning("PushManager", "Is disconnected: \(error?.localizedDescription)")
log.warning("PushManager is disconnected: \(error?.localizedDescription ?? "")")

if(!self.killed) {
log.info("Reconnecting in 5 sec");
Expand Down Expand Up @@ -313,15 +321,17 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
switch type {
case "nop":
self.nopTimer.invalidate() // Resetting nopTimer
self.nopTimer = Timer.scheduledTimer(timeInterval: 35.0, target: self, selector: #selector(PushManager.disconnect), userInfo: nil, repeats: false)
self.nopTimer = Timer.scheduledTimer(timeInterval: 35.0, target: self, selector: #selector(PushManager.disconnectForTimeout), userInfo: nil, repeats: false)
case "tickle":
if let subtype = message["subtype"].string {
if(subtype == "account") {
log.debug("TICKLE -> account")
getUserInfo(nil)
}
else if(subtype == "push"){
// When you receive a tickle message, it means that a resource of the type push has changed.
// Request only the latest push: In this case can be a file, a link or just a simple note
log.debug("TICKLE -> push")
Alamofire.request("https://api.pushbullet.com/v2/pushes?limit=1", headers: ["Access-Token": token])
.responseString { response in
if let result = response.result.value {
Expand All @@ -340,10 +350,12 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
if let pushType = push["type"].string {
switch(pushType) {
case "mirror":
log.debug("PUSH -> mirror")
center.deliver(createNotification(push))
break;
case "dismissal":
//loop through current user notifications, if identifier matches, remove it
log.debug("PUSH -> dismiss")
for noti in center.deliveredNotifications {
if noti.identifier == push["notification_id"].string {
center.removeDeliveredNotification(noti)
Expand All @@ -364,6 +376,7 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate

break;
case "sms_changed":
log.debug("PUSH -> sms_changed")
if push["notifications"].exists() {
for sms in push["notifications"].array! {
let notification = NSUserNotification()
Expand Down

0 comments on commit 9c878eb

Please sign in to comment.