Skip to content

Commit b1d7ff3

Browse files
HansonHanson
authored andcommitted
feat: 接入websorcke
1 parent 6199cde commit b1d7ff3

File tree

10 files changed

+477
-3
lines changed

10 files changed

+477
-3
lines changed

BatChat.xcodeproj/project.pbxproj

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
0ED6730BCBD43BFC676E2C61 /* Pods_Domain.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FC9603DB5850B55C17AD8631 /* Pods_Domain.framework */; };
1111
5D80721D7E22338D9F85FBBF /* Pods_BatChat.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 40AF006FE9370853776FDD91 /* Pods_BatChat.framework */; };
1212
6CD963C84735413EEEB3782D /* Pods_NetworkPlatform.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 99E4191A2721DB54B7981141 /* Pods_NetworkPlatform.framework */; };
13+
C002E36E265E501E00E291C6 /* BCSessionViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C002E36D265E501E00E291C6 /* BCSessionViewModel.swift */; };
14+
C002E373265E540000E291C6 /* Observable+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = C002E371265E53C800E291C6 /* Observable+Ext.swift */; };
15+
C002E374265E540000E291C6 /* ErrorTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = C002E36F265E52AC00E291C6 /* ErrorTracker.swift */; };
16+
C002E37A265E564700E291C6 /* SocketService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C002E379265E564700E291C6 /* SocketService.swift */; };
17+
C002E37C265E58CF00E291C6 /* ChatSocketService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C002E37B265E58CF00E291C6 /* ChatSocketService.swift */; };
1318
C007689525E4A83B0098DF89 /* ScreenMetric.swift in Sources */ = {isa = PBXBuildFile; fileRef = C007688325E4A83A0098DF89 /* ScreenMetric.swift */; };
1419
C007689625E4A83B0098DF89 /* UIImage+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C007688425E4A83A0098DF89 /* UIImage+Additions.swift */; };
1520
C007689725E4A83B0098DF89 /* UIView+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C007688525E4A83A0098DF89 /* UIView+Additions.swift */; };
@@ -225,6 +230,11 @@
225230
486C20FD8F9D9C3A2EEB3A97 /* Pods-BatChat.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-BatChat.debug.xcconfig"; path = "Target Support Files/Pods-BatChat/Pods-BatChat.debug.xcconfig"; sourceTree = "<group>"; };
226231
825453482B5EAF099FBA771F /* Pods-NetworkPlatform.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NetworkPlatform.debug.xcconfig"; path = "Target Support Files/Pods-NetworkPlatform/Pods-NetworkPlatform.debug.xcconfig"; sourceTree = "<group>"; };
227232
99E4191A2721DB54B7981141 /* Pods_NetworkPlatform.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_NetworkPlatform.framework; sourceTree = BUILT_PRODUCTS_DIR; };
233+
C002E36D265E501E00E291C6 /* BCSessionViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BCSessionViewModel.swift; sourceTree = "<group>"; };
234+
C002E36F265E52AC00E291C6 /* ErrorTracker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorTracker.swift; sourceTree = "<group>"; };
235+
C002E371265E53C800E291C6 /* Observable+Ext.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Observable+Ext.swift"; sourceTree = "<group>"; };
236+
C002E379265E564700E291C6 /* SocketService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketService.swift; sourceTree = "<group>"; };
237+
C002E37B265E58CF00E291C6 /* ChatSocketService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatSocketService.swift; sourceTree = "<group>"; };
228238
C007688325E4A83A0098DF89 /* ScreenMetric.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScreenMetric.swift; sourceTree = "<group>"; };
229239
C007688425E4A83A0098DF89 /* UIImage+Additions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Additions.swift"; sourceTree = "<group>"; };
230240
C007688525E4A83A0098DF89 /* UIView+Additions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Additions.swift"; sourceTree = "<group>"; };
@@ -598,10 +608,11 @@
598608
C07B2E3625CD2F3700C2AB01 /* Session */ = {
599609
isa = PBXGroup;
600610
children = (
611+
C07B2E3725CD2F6400C2AB01 /* BCSessionViewController.swift */,
612+
C002E36D265E501E00E291C6 /* BCSessionViewModel.swift */,
601613
C00768AF25E4B80B0098DF89 /* Chat Items */,
602614
C0A4D2F225E2091B00013B31 /* ViewModels */,
603615
C0A4D22325E2065600013B31 /* Base */,
604-
C07B2E3725CD2F6400C2AB01 /* BCSessionViewController.swift */,
605616
);
606617
path = Session;
607618
sourceTree = "<group>";
@@ -866,6 +877,8 @@
866877
C0ED3D0F25CAA7A600505906 /* Protocol */,
867878
C0C79EF6265CEEFB0025C69A /* BCHUD.swift */,
868879
C0C79EF8265CEF430025C69A /* Toast.swift */,
880+
C002E36F265E52AC00E291C6 /* ErrorTracker.swift */,
881+
C002E371265E53C800E291C6 /* Observable+Ext.swift */,
869882
);
870883
path = Utility;
871884
sourceTree = "<group>";
@@ -949,6 +962,8 @@
949962
C0ED3DC125CAAFA700505906 /* Network.swift */,
950963
C0ED3DC225CAAFA700505906 /* GlobalConfiguration.swift */,
951964
C0ED3DC325CAAFA700505906 /* AppEnvironment.swift */,
965+
C002E379265E564700E291C6 /* SocketService.swift */,
966+
C002E37B265E58CF00E291C6 /* ChatSocketService.swift */,
952967
);
953968
path = Network;
954969
sourceTree = "<group>";
@@ -1293,6 +1308,7 @@
12931308
C0ED3D6B25CAAABB00505906 /* UILabel+Extension.swift in Sources */,
12941309
C0A4D2EA25E2066B00013B31 /* PhotosInputCell.swift in Sources */,
12951310
C068FC5925E742C300A2D7AD /* EmojiChatInputItem.swift in Sources */,
1311+
C002E374265E540000E291C6 /* ErrorTracker.swift in Sources */,
12961312
C0ED3D2225CAA86E00505906 /* BaseViewController.swift in Sources */,
12971313
C0E2B89025E63F0200F7B092 /* ChatInputBar.swift in Sources */,
12981314
C0A4D2E325E2066B00013B31 /* PhotosInputWithPlaceholdersDataProvider.swift in Sources */,
@@ -1344,6 +1360,7 @@
13441360
C012779325EE155E00DE8ADD /* PhotoMessagePresenter.swift in Sources */,
13451361
C0A4D2EB25E2066B00013B31 /* PhotosInputDataProvider.swift in Sources */,
13461362
C0ED3D7425CAAABB00505906 /* UIView+AddClickedEvent.swift in Sources */,
1363+
C002E373265E540000E291C6 /* Observable+Ext.swift in Sources */,
13471364
C0ED3D7725CAAABB00505906 /* NSArray+Extsion.swift in Sources */,
13481365
C0C79EF3265CD6B90025C69A /* Router.swift in Sources */,
13491366
C00768A525E4A83B0098DF89 /* CGPoint+Additions.swift in Sources */,
@@ -1365,6 +1382,7 @@
13651382
C00768C125E4B8EC0098DF89 /* TextMessageCollectionViewCellDefaultStyle.swift in Sources */,
13661383
C0C79EF9265CEF430025C69A /* Toast.swift in Sources */,
13671384
C012779125EE155E00DE8ADD /* PhotoMessagePresenterBuilder.swift in Sources */,
1385+
C002E36E265E501E00E291C6 /* BCSessionViewModel.swift in Sources */,
13681386
C007689F25E4A83B0098DF89 /* Cache.swift in Sources */,
13691387
C00768D125E4B9490098DF89 /* BaseMessageModel.swift in Sources */,
13701388
C0A4D2DF25E2066B00013B31 /* DeviceImagePicker.swift in Sources */,
@@ -1404,6 +1422,7 @@
14041422
C0ED3DE725CAB18300505906 /* _CleanJSONDecodingStorage.swift in Sources */,
14051423
C0ED3DEA25CAB18300505906 /* _CleanJSONDecoder+Unbox.swift in Sources */,
14061424
C0ED3DC425CAAFA700505906 /* Network.swift in Sources */,
1425+
C002E37C265E58CF00E291C6 /* ChatSocketService.swift in Sources */,
14071426
C0ED3DE925CAB18300505906 /* CleanDecoder.swift in Sources */,
14081427
C0ED3DF225CAB18300505906 /* AnyEncodable.swift in Sources */,
14091428
C0ED3DFD25CAB3D800505906 /* UseCaseProvider.swift in Sources */,
@@ -1419,6 +1438,7 @@
14191438
C0ED3DE825CAB18300505906 /* DecodingStrategy.swift in Sources */,
14201439
C0ED3DBD25CAAF4100505906 /* UsersNetwork.swift in Sources */,
14211440
C0ED3DE225CAB18300505906 /* _CleanJSONDecoder+SingleValueDecodingContainer.swift in Sources */,
1441+
C002E37A265E564700E291C6 /* SocketService.swift in Sources */,
14221442
C0ED3DEF25CAB18300505906 /* Defaultable.swift in Sources */,
14231443
);
14241444
runOnlyForDeploymentPostprocessing = 0;

BatChat/Scenes/Session/BCSessionViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class BCSessionViewController: BCChatViewController {
4444
class BCChatViewController: BaseChatViewController {
4545
var shouldUseAlternativePresenter: Bool = false
4646

47+
let viewModel = BCSessionViewModel()
4748
var inputTextView: ChatInputBar?
4849
let messagesSelector = BaseMessagesSelector()
4950
lazy var messageSender: ChatMessageSender = {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// BCSessionViewModel.swift
3+
// BatChat
4+
//
5+
// Created by Hanson on 2021/5/26.
6+
//
7+
8+
import UIKit
9+
import NetworkPlatform
10+
11+
final class BCSessionViewModel: ViewModelType {
12+
fileprivate lazy var disposeBag = DisposeBag()
13+
private var useCase: UserUseCase = netwokData.makeUserUseCase()
14+
15+
}
16+
17+
18+
extension BCSessionViewModel {
19+
public enum JumpType {
20+
case tabbar // 登陆跳转主页
21+
case chooseSex // 选着性别
22+
}
23+
24+
struct Input {
25+
let sendMessage: Driver<MessageModelProtocol>
26+
let jump: Driver<JumpType>
27+
}
28+
29+
struct Output {
30+
var messageList: Observable<MessageModelProtocol>
31+
let error: ErrorTracker
32+
}
33+
34+
func transform(input: BCSessionViewModel.Input) -> BCSessionViewModel.Output {
35+
let messageList = Observable<MessageModelProtocol>.empty()
36+
let errorTracker = ErrorTracker()
37+
let countdown = BehaviorRelay<Bool>(value: false)
38+
39+
40+
return Output(messageList: messageList,
41+
error: errorTracker)
42+
}
43+
}

BatChat/Scenes/Session/ViewModels/ChatDataSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ChatDataSource: ChatDataSourceProtocol {
7777
self.slidingWindow.insertItem(message, position: .bottom)
7878
self.delegate?.chatDataSourceDidUpdate(self)
7979
}
80-
80+
8181
func addRandomIncomingMessage() {
8282
let message = ChatMessageFactory.makeRandomMessage("\(self.nextMessageId)", isIncoming: true)
8383
self.nextMessageId += 1

BatChat/Utility/ErrorTracker.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Created by sergdort on 03/02/2017.
3+
// Copyright (c) 2017 sergdort. All rights reserved.
4+
//
5+
6+
7+
import Foundation
8+
import RxSwift
9+
import RxCocoa
10+
11+
final class ErrorTracker: SharedSequenceConvertibleType {
12+
typealias SharingStrategy = DriverSharingStrategy
13+
private let _subject = PublishSubject<Error>()
14+
15+
func trackError<O: ObservableConvertibleType>(from source: O) -> Observable<O.Element> {
16+
return source.asObservable().do(onError: onError)
17+
}
18+
19+
func asSharedSequence() -> SharedSequence<SharingStrategy, Error> {
20+
return _subject.asObservable().asDriverOnErrorJustComplete()
21+
}
22+
23+
func asObservable() -> Observable<Error> {
24+
return _subject.asObservable()
25+
}
26+
27+
private func onError(_ error: Error) {
28+
_subject.onNext(error)
29+
}
30+
31+
deinit {
32+
_subject.onCompleted()
33+
}
34+
}
35+
36+
extension ObservableConvertibleType {
37+
func trackError(_ errorTracker: ErrorTracker) -> Observable<Element> {
38+
return errorTracker.trackError(from: self)
39+
}
40+
}

BatChat/Utility/Observable+Ext.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Foundation
2+
import RxSwift
3+
import RxCocoa
4+
5+
extension ObservableType where Element == Bool {
6+
/// Boolean not operator
7+
public func not() -> Observable<Bool> {
8+
return self.map(!)
9+
}
10+
11+
}
12+
13+
extension SharedSequenceConvertibleType {
14+
func mapToVoid() -> SharedSequence<SharingStrategy, Void> {
15+
return map { _ in }
16+
}
17+
}
18+
19+
extension ObservableType {
20+
21+
func catchErrorJustComplete() -> Observable<Element> {
22+
return catchError { _ in
23+
return Observable.empty()
24+
}
25+
}
26+
27+
func asDriverOnErrorJustComplete() -> Driver<Element> {
28+
return asDriver { error in
29+
return Driver.empty()
30+
}
31+
}
32+
33+
func mapToVoid() -> Observable<Void> {
34+
return map { _ in }
35+
}
36+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//
2+
// ChatSocketService.swift
3+
// NetworkPlatform
4+
//
5+
// Created by Hanson on 2021/5/26.
6+
//
7+
8+
import Domain
9+
import RxSwift
10+
import SocketRocket
11+
12+
class ChatSocketService: SocketService {
13+
static let shared = ChatSocketService()
14+
let messageQueue = PublishSubject<ChatSocketService>()
15+
private var isGroup = false
16+
private var hasEnter: Bool = false // to be MOVED
17+
18+
override init() {
19+
super.init()
20+
name = "进入聊天"
21+
}
22+
23+
// MARK: - 向后台请求socket地址
24+
25+
public func beginConnet(_ data: ScoreChatGetTokenData, isGroup: Bool) {
26+
guard let addrData = data.lotteryAddress?.first else { return }
27+
self.isGroup = isGroup
28+
if isGroup {
29+
address = "\(addrData.protocol)://\(addrData.host):\(addrData.port)?groupToken=\(data.token)"
30+
} else {
31+
address = "\(addrData.protocol)://\(addrData.host):\(addrData.port)?token=\(data.token)"
32+
}
33+
34+
// 开始连接
35+
start()
36+
}
37+
38+
override func onConnect() {
39+
print(name + "连接成功")
40+
super.onConnect()
41+
onResponse(res: ["connet": true])
42+
}
43+
44+
override func onDisconnect() {
45+
super.onConnect()
46+
print(name + "断开连接")
47+
onResponse(res: ["connet": true])
48+
}
49+
50+
override func stop() {
51+
super.stop()
52+
hasEnter = false
53+
}
54+
55+
// 获取地址失败、重新发送网络获取
56+
@objc private func reCreatSocket() {
57+
guard !isExist() || address == nil else { return }
58+
59+
let delayTime = 5 // 每隔5s重试
60+
perform(#selector(super.onResume),
61+
with: nil,
62+
afterDelay: TimeInterval(delayTime))
63+
}
64+
}
65+
66+
// MARK: - 推送数据处理
67+
68+
extension ChatSocketService {
69+
override func webSocket(_ webSocket: SRWebSocket!, didReceiveMessage message: Any!) {
70+
guard let msgString = message as? String,
71+
let json = msgString.toJSON() else {
72+
return
73+
}
74+
var dict = json.toJSONDict()
75+
if let data = json.dictionary?["data"]?.rawString() {
76+
dict = data.toJSONDict()
77+
onResponse(res: dict as NSDictionary)
78+
}
79+
}
80+
81+
func onResponse(res: NSDictionary) {
82+
/// 发送消息
83+
messageQueue.onNext(ChatSocketService())
84+
85+
/// 收取消息
86+
messageQueue.subscribe { aa in
87+
print(aa.element ?? "ll")
88+
}.dispose()
89+
}
90+
}
91+
92+
93+
/// 数据模型
94+
class ScoreChatGetTokenData: ECodable {
95+
var lotteryAddress: [FMScoreChatGetTokenModel]?
96+
var token: String = ""
97+
}
98+
99+
/// sock数据模型
100+
class FMScoreChatGetTokenModel: ECodable {
101+
var host: String = ""
102+
var port: Int = 0
103+
var `protocol`: String = ""
104+
}

0 commit comments

Comments
 (0)