Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/reconnect #9

Merged
merged 2 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions RIBsTreeViewerClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0.4;
MARKETING_VERSION = 1.0.5;
PRODUCT_BUNDLE_IDENTIFIER = co.minipro.app.RIBsTreeViewerClient;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -352,7 +352,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0.4;
MARKETING_VERSION = 1.0.5;
PRODUCT_BUNDLE_IDENTIFIER = co.minipro.app.RIBsTreeViewerClient;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down
20 changes: 14 additions & 6 deletions RIBsTreeViewerClient/Sources/RIBsTreeViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public class RIBsTreeViewerImpl: RIBsTreeViewer {
let jsonData = try JSONSerialization.data(withJSONObject: $0)
let jsonString = String(bytes: jsonData, encoding: .utf8)!
self?.webSocket.send(text: jsonString)
} catch {
// TODO: Error Handling
} catch let error {
}
})

Expand Down Expand Up @@ -170,20 +169,25 @@ class WebSocketClient: NSObject {
var webSocketTask: URLSessionWebSocketTask!
var urlSession: URLSession!
let delegateQueue = OperationQueue()
let url: URL

init(url: URL) {
self.url = url
super.init()
urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: delegateQueue)
webSocketTask = urlSession.webSocketTask(with: url)
self.urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: delegateQueue)
self.webSocketTask = urlSession.webSocketTask(with: url)
}

func connect() {
if webSocketTask.state == .completed {
webSocketTask = urlSession.webSocketTask(with: url)
}
webSocketTask.resume()
listen()
}

func disconnect() {
webSocketTask.cancel()
webSocketTask.cancel(with: .goingAway, reason: nil)
}

func send(data: Data) {
Expand All @@ -205,7 +209,8 @@ class WebSocketClient: NSObject {
}

private func listen() {
webSocketTask.receive { result in
webSocketTask.receive { [weak self] result in
guard let `self` = self else { return }
switch result {
case .success(let message):
switch message {
Expand Down Expand Up @@ -235,3 +240,6 @@ extension WebSocketClient: URLSessionWebSocketDelegate {
self.delegate?.onDisconnedted(client: self)
}
}

#endif