Skip to content

Commit

Permalink
Had success in getting response from push server, add notification so…
Browse files Browse the repository at this point in the history
…cket delegate
  • Loading branch information
Will-Tyler committed Aug 8, 2018
1 parent 3ae878c commit 9bed68b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
4 changes: 4 additions & 0 deletions GroupMac.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
4CDC813E20DAF9EC004D8DD0 /* MessagesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDC813D20DAF9EC004D8DD0 /* MessagesViewController.swift */; };
4CDC814220DB23CB004D8DD0 /* ConvosViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDC814120DB23CB004D8DD0 /* ConvosViewController.swift */; };
5D786FECBEFA441532910443 /* Pods_GroupMac.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 415CC64559BA1B574C68A601 /* Pods_GroupMac.framework */; };
9732A1827C76D1E7B72ADF77 /* NotificationSocketDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9732AC292DD2BE4DFA789A74 /* NotificationSocketDelegate.swift */; };
9732A2C9BA6651AEF167EB76 /* AttachmentContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9732A1EC1A8162F327B833DE /* AttachmentContent.swift */; };
9732A897C2C8F699E66989C0 /* APIResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9732ABDEEE4AA9F6324C7EFB /* APIResponse.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -114,6 +115,7 @@
831C8A87BAFE21A2FF9877CB /* Pods-GroupMac.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GroupMac.release.xcconfig"; path = "Pods/Target Support Files/Pods-GroupMac/Pods-GroupMac.release.xcconfig"; sourceTree = "<group>"; };
9732A1EC1A8162F327B833DE /* AttachmentContent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttachmentContent.swift; sourceTree = "<group>"; };
9732ABDEEE4AA9F6324C7EFB /* APIResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIResponse.swift; sourceTree = "<group>"; };
9732AC292DD2BE4DFA789A74 /* NotificationSocketDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationSocketDelegate.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -242,6 +244,7 @@
4C78FDB920D82E21006385B2 /* token.txt */,
9732ABDEEE4AA9F6324C7EFB /* APIResponse.swift */,
9732A1EC1A8162F327B833DE /* AttachmentContent.swift */,
9732AC292DD2BE4DFA789A74 /* NotificationSocketDelegate.swift */,
);
path = GroupMe;
sourceTree = "<group>";
Expand Down Expand Up @@ -479,6 +482,7 @@
4C78FDB720D82C8D006385B2 /* GroupMe.swift in Sources */,
9732A897C2C8F699E66989C0 /* APIResponse.swift in Sources */,
9732A2C9BA6651AEF167EB76 /* AttachmentContent.swift in Sources */,
9732A1827C76D1E7B72ADF77 /* NotificationSocketDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
3 changes: 2 additions & 1 deletion GroupMac/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import Cocoa


class AppDelegate: NSObject, NSApplicationDelegate {
final class AppDelegate: NSObject, NSApplicationDelegate {

static let me = GroupMe.me
private let socket = GroupMe.notificationSocket;

let windowController: NSWindowController = {
let window = NSWindow()
Expand Down
31 changes: 29 additions & 2 deletions GroupMe/GroupMe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,44 @@ class GroupMe {
return task
}

private static let notificationSocket: SRWebSocket = {
static let notificationSocket: SRWebSocket = {
print("Creating notification socket...")
defer {
print("Finished setting up socket...")
}

let url = URL(string: "https://push.groupme.com/faye")!
let request: URLRequest = {
var req = URLRequest(url: url)
let body = [
[
"channel": "/meta/handshake",
"version": "1.0",
"supportedConnectionTypes": ["long-polling"],
"id": "1"
]
]

req.httpMethod = HTTP.RequestMethod.post.rawValue
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
req.httpMethod = HTTP.RequestMethod.post.rawValue
req.httpBody = try! JSONSerialization.data(withJSONObject: body)

return req
}()

URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if let data = data, !data.isEmpty {
print(String(data: data, encoding: .utf8)!)
}
else {
print("Received nil or empty data...")
}
}.resume()

let socket = SRWebSocket(urlRequest: request)!
let delegate = NotificationSocketDelegate()

socket.delegate = delegate

return socket
}()
Expand Down
16 changes: 16 additions & 0 deletions GroupMe/NotificationSocketDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Created by Will Tyler on 8/5/18.
// Copyright (c) 2018 Will Tyler. All rights reserved.
//

import Foundation
import SocketRocket


final class NotificationSocketDelegate: NSObject, SRWebSocketDelegate {

func webSocket(_ webSocket: SRWebSocket!, didReceiveMessage message: Any!) {
print("Notification socket received message...", message, separator: "\n")
}

}

0 comments on commit 9bed68b

Please sign in to comment.