Skip to content

Commit 4fc6e6f

Browse files
author
yuki tamazawa
committed
[ci skip] Refactor
1 parent 9767757 commit 4fc6e6f

3 files changed

Lines changed: 100 additions & 114 deletions

File tree

RIBsTreeViewerClient.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
C555E16B21EF16790060A7B0 /* RIBsTreeViewer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C555E16A21EF16790060A7B0 /* RIBsTreeViewer.swift */; };
1212
C555E18821EF244C0060A7B0 /* RIBs.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C555E18421EF20F10060A7B0 /* RIBs.framework */; platformFilter = ios; };
1313
C555E18921EF245A0060A7B0 /* RxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C555E18621EF20FF0060A7B0 /* RxSwift.framework */; platformFilter = ios; };
14-
C5AC41E72396A0AD00F6A344 /* WebSocketClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5AC41E62396A0AD00F6A344 /* WebSocketClient.swift */; };
1514
/* End PBXBuildFile section */
1615

1716
/* Begin PBXFileReference section */
@@ -23,7 +22,6 @@
2322
C555E18621EF20FF0060A7B0 /* RxSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxSwift.framework; path = Carthage/Build/iOS/RxSwift.framework; sourceTree = "<group>"; };
2423
C555E18A21EF27150060A7B0 /* SocketIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SocketIO.framework; path = Carthage/Build/iOS/SocketIO.framework; sourceTree = "<group>"; };
2524
C555E18C21EF271F0060A7B0 /* Starscream.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Starscream.framework; path = Carthage/Build/iOS/Starscream.framework; sourceTree = "<group>"; };
26-
C5AC41E62396A0AD00F6A344 /* WebSocketClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebSocketClient.swift; sourceTree = "<group>"; };
2725
/* End PBXFileReference section */
2826

2927
/* Begin PBXFrameworksBuildPhase section */
@@ -70,7 +68,6 @@
7068
isa = PBXGroup;
7169
children = (
7270
C555E16A21EF16790060A7B0 /* RIBsTreeViewer.swift */,
73-
C5AC41E62396A0AD00F6A344 /* WebSocketClient.swift */,
7471
);
7572
path = Sources;
7673
sourceTree = "<group>";
@@ -168,7 +165,6 @@
168165
isa = PBXSourcesBuildPhase;
169166
buildActionMask = 2147483647;
170167
files = (
171-
C5AC41E72396A0AD00F6A344 /* WebSocketClient.swift in Sources */,
172168
C555E16B21EF16790060A7B0 /* RIBsTreeViewer.swift in Sources */,
173169
);
174170
runOnlyForDeploymentPostprocessing = 0;

RIBsTreeViewerClient/Sources/RIBsTreeViewer.swift

Lines changed: 100 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,29 @@ import Foundation
1010
import RxSwift
1111
import RIBs
1212

13+
protocol RIBsTreeViewer {
14+
func start()
15+
}
16+
1317
public enum RIBsTreeViewerOptions: String {
1418
case webSocketURL
1519
}
1620

1721
@available(iOS 13.0, *)
18-
public class RIBsTreeViewer {
22+
public class RIBsTreeViewerImpl {
1923

2024
private let router: Routing
21-
private let webSocket: WebSocketTaskConnection
25+
private let webSocket: WebSocketClient
2226
private let disposeBag = DisposeBag()
2327

2428
public init(router: Routing, option: [RIBsTreeViewerOptions: String]? = nil) {
2529
let url = option?[.webSocketURL]
2630
self.router = router
2731

2832
if let url = url {
29-
self.webSocket = WebSocketTaskConnection.init(url: URL(string: url)!)
33+
self.webSocket = WebSocketClient.init(url: URL(string: url)!)
3034
} else {
31-
self.webSocket = WebSocketTaskConnection.init(url: URL(string: "wc://0.0.0.0:8080")!)
35+
self.webSocket = WebSocketClient.init(url: URL(string: "wc://0.0.0.0:8080")!)
3236
}
3337
self.webSocket.delegate = self
3438
self.webSocket.connect()
@@ -76,7 +80,10 @@ public class RIBsTreeViewer {
7680
return nil
7781
}
7882
}
83+
}
7984

85+
@available(iOS 13.0, *)
86+
extension RIBsTreeViewerImpl {
8087
private func captureView(from targetRouter: String) -> Data? {
8188
guard let router = findRouter(target: targetRouter, router: router) as? ViewableRouting,
8289
let view = router.viewControllable.uiviewController.view,
@@ -99,18 +106,14 @@ public class RIBsTreeViewer {
99106
}
100107

101108
@available(iOS 13.0, *)
102-
extension RIBsTreeViewer: WebSocketConnectionDelegate {
103-
104-
func onConnected(connection: WebSocketConnection) {
109+
extension RIBsTreeViewerImpl: WebSocketClientDelegate {
110+
func onConnected(client: WebSocketClient) {
105111
}
106112

107-
func onDisconnected(connection: WebSocketConnection, error: Error?) {
113+
func onDisconnedted(client: WebSocketClient) {
108114
}
109115

110-
func onError(connection: WebSocketConnection, error: Error) {
111-
}
112-
113-
func onMessage(connection: WebSocketConnection, text: String) {
116+
func onMessage(client: WebSocketClient, text: String) {
114117
// text == routerName
115118
DispatchQueue.main.async {
116119
if let data = self.captureView(from: text) {
@@ -119,7 +122,91 @@ extension RIBsTreeViewer: WebSocketConnectionDelegate {
119122
}
120123
}
121124

122-
func onMessage(connection: WebSocketConnection, data: Data) {
125+
func onMessage(client: WebSocketClient, data: Data) {
126+
}
127+
128+
func onError(client: WebSocketClient, error: Error) {
129+
}
130+
}
131+
132+
protocol WebSocketClientDelegate: class {
133+
@available(iOS 13.0, *)
134+
func onConnected(client: WebSocketClient)
135+
@available(iOS 13.0, *)
136+
func onDisconnedted(client: WebSocketClient)
137+
@available(iOS 13.0, *)
138+
func onMessage(client: WebSocketClient, text: String)
139+
@available(iOS 13.0, *)
140+
func onMessage(client: WebSocketClient, data: Data)
141+
@available(iOS 13.0, *)
142+
func onError(client: WebSocketClient, error: Error)
143+
}
144+
145+
@available(iOS 13.0, *)
146+
class WebSocketClient: NSObject {
147+
148+
weak var delegate: WebSocketClientDelegate?
149+
var webSocketTask: URLSessionWebSocketTask!
150+
var urlSession: URLSession!
151+
let delegateQueue = OperationQueue()
152+
153+
init(url: URL) {
154+
super.init()
155+
urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: delegateQueue)
156+
webSocketTask = urlSession.webSocketTask(with: url)
157+
}
158+
159+
func connect() {
160+
webSocketTask.resume()
123161
}
124162

163+
func disconnect() {
164+
webSocketTask.cancel()
165+
}
166+
167+
func send(data: Data) {
168+
webSocketTask.send(.data(data)) { error in
169+
guard let error = error else {
170+
return
171+
}
172+
self.delegate?.onError(client: self, error: error)
173+
}
174+
}
175+
176+
func send(text: String) {
177+
webSocketTask.send(.string(text)) { error in
178+
guard let error = error else {
179+
return
180+
}
181+
self.delegate?.onError(client: self, error: error)
182+
}
183+
}
184+
185+
private func listen() {
186+
webSocketTask.receive { result in
187+
switch result {
188+
case .success(let message):
189+
switch message {
190+
case .data(let data):
191+
self.delegate?.onMessage(client: self, data: data)
192+
case .string(let text):
193+
self.delegate?.onMessage(client: self, text: text)
194+
}
195+
case .failure(let error):
196+
self.delegate?.onError(client: self, error: error)
197+
}
198+
}
199+
}
200+
201+
}
202+
203+
@available(iOS 13.0, *)
204+
extension WebSocketClient: URLSessionWebSocketDelegate {
205+
func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol protocol: String?) {
206+
self.delegate?.onConnected(client: self)
207+
}
208+
209+
func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didCloseWith closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?) {
210+
self.delegate?.onDisconnedted(client: self)
211+
}
125212
}

RIBsTreeViewerClient/Sources/WebSocketClient.swift

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)