-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultipeerJSON.swift
181 lines (160 loc) · 3.97 KB
/
MultipeerJSON.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// MultipeerJSON.swift
// Pods
//
// Created by Brian Semiglia on 7/3/17.
//
//
import Foundation
import MultipeerConnectivity
import RxSwift
import RxSwiftExt
public class MultipeerJSON:
NSObject,
MCNearbyServiceAdvertiserDelegate,
MCSessionDelegate {
public enum Action {
case idle
case connecting
case connected
case disconnected
case received(data: Data)
}
private let cleanup = DisposeBag()
public let output = BehaviorSubject(value: Action.idle)
private let input = BehaviorSubject<[AnyHashable: Any]>(value: [:])
var session: MCSession?
let mine: MCPeerID
let advertiser: MCNearbyServiceAdvertiser
public override init() {
mine = MCPeerID(
displayName: UIDevice.current.name
)
advertiser = MCNearbyServiceAdvertiser(
peer: mine,
discoveryInfo: nil,
serviceType: "Cycle-Monitor"
)
super.init()
advertiser.delegate = self
advertiser.startAdvertisingPeer()
}
public func rendered(_ input: Observable<[AnyHashable: Any]>) -> Observable<Action> {
input.subscribe { [weak self] in
if let element = $0.element {
self?.input.on(.next(element))
}
}
.disposed(by: cleanup)
self.input.pausableBuffered(output.isConnected, limit: nil).subscribe(
onNext: { [weak self] new in
self?.render(new)
}
)
.disposed(by: cleanup)
return output
}
func render(_ input: [AnyHashable: Any]) {
if let connected = session?.connectedPeers {
try? session?.send(
JSONSerialization.data(
withJSONObject: input,
options: JSONSerialization.WritingOptions(rawValue: 0)
),
toPeers: connected,
with: .reliable
)
}
}
public func advertiser(
_ advertiser: MCNearbyServiceAdvertiser,
didReceiveInvitationFromPeer peerID: MCPeerID,
withContext context: Data?,
invitationHandler: @escaping (Bool, MCSession?) -> Swift.Void
) {
if session == nil {
advertiser.stopAdvertisingPeer()
let session = MCSession(
peer: mine,
securityIdentity: nil,
encryptionPreference: .required
)
session.delegate = self
invitationHandler(
true,
session
)
self.session = session
}
}
public func advertiser(
_ advertiser: MCNearbyServiceAdvertiser,
didNotStartAdvertisingPeer error: Error
) {
}
public func session(
_ session: MCSession,
peer peerID: MCPeerID,
didChange state: MCSessionState
) {
switch state {
case .connected:
output.on(.next(.connected))
case .connecting:
output.on(.next(.connecting))
case .notConnected:
output.on(.next(.disconnected))
self.session = nil
advertiser.startAdvertisingPeer()
}
}
public func session(
_ session: MCSession,
didReceive data: Data,
fromPeer peerID: MCPeerID
) {
if let json = data.JSON, json["action"].flatMap({ $0 as? String }).map({ $0 == "disconnect" }) == true {
session.disconnect()
} else {
output.on(
.next(
.received(
data: data
)
)
)
}
}
public func session(
_ session: MCSession,
didReceive stream: InputStream,
withName streamName: String,
fromPeer peerID: MCPeerID
) {
}
public func session(
_ session: MCSession,
didStartReceivingResourceWithName resourceName: String,
fromPeer peerID: MCPeerID,
with progress: Progress
) {
}
public func session(
_ session: MCSession,
didFinishReceivingResourceWithName resourceName: String,
fromPeer peerID: MCPeerID,
at localURL: URL?,
withError error: Error?
) {
}
}
extension Observable where E == MultipeerJSON.Action {
var isConnected: Observable<Bool> { return
map {
switch $0 {
case .connected, .received: return true
case .idle, .connecting, .disconnected: return false
}
}
}
}