-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathViewController.swift
173 lines (142 loc) · 5.65 KB
/
ViewController.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
//
// ViewController.swift
// HubSamplePhone
//
// Created by Pawel Kadluczka on 2/11/18.
// Copyright © 2018 Pawel Kadluczka. All rights reserved.
//
import UIKit
import SignalRClient
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// Update the Url accordingly
private let serverUrl = "http://192.168.86.250:5000/chat" // /chat or /chatLongPolling or /chatWebSockets
private let dispatchQueue = DispatchQueue(label: "hubsamplephone.queue.dispatcheueuq")
private var chatHubConnection: HubConnection?
private var chatHubConnectionDelegate: HubConnectionDelegate?
private var name = ""
private var messages: [String] = []
private var reconnectAlert: UIAlertController?
@IBOutlet weak var sendButton: UIButton!
@IBOutlet weak var chatTableView: UITableView!
@IBOutlet weak var msgTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.chatTableView.delegate = self
self.chatTableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
let alert = UIAlertController(title: "Enter your Name", message:"", preferredStyle: UIAlertController.Style.alert)
alert.addTextField() { textField in textField.placeholder = "Name"}
let OKAction = UIAlertAction(title: "OK", style: .default) { action in
self.name = alert.textFields?.first?.text ?? "John Doe"
self.chatHubConnectionDelegate = ChatHubConnectionDelegate(controller: self)
self.chatHubConnection = HubConnectionBuilder(url: URL(string: self.serverUrl)!)
.withLogging(minLogLevel: .debug)
.withAutoReconnect()
.withHubConnectionDelegate(delegate: self.chatHubConnectionDelegate!)
.build()
self.chatHubConnection!.on(method: "NewMessage", callback: {(user: String, message: String) in
self.appendMessage(message: "\(user): \(message)")
})
self.chatHubConnection!.start()
}
alert.addAction(OKAction)
self.present(alert, animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
chatHubConnection?.stop()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func btnSend(_ sender: Any) {
let message = msgTextField.text
if message != "" {
chatHubConnection?.invoke(method: "Broadcast", name, message) { error in
if let e = error {
self.appendMessage(message: "Error: \(e)")
}
}
msgTextField.text = ""
}
}
private func appendMessage(message: String) {
self.dispatchQueue.sync {
self.messages.append(message)
}
self.chatTableView.beginUpdates()
self.chatTableView.insertRows(at: [IndexPath(row: messages.count - 1, section: 0)], with: .automatic)
self.chatTableView.endUpdates()
self.chatTableView.scrollToRow(at: IndexPath(item: messages.count-1, section: 0), at: .bottom, animated: true)
}
fileprivate func connectionDidOpen() {
toggleUI(isEnabled: true)
}
fileprivate func connectionDidFailToOpen(error: Error) {
blockUI(message: "Connection failed to start.", error: error)
}
fileprivate func connectionDidClose(error: Error?) {
if let alert = reconnectAlert {
alert.dismiss(animated: true, completion: nil)
}
blockUI(message: "Connection is closed.", error: error)
}
fileprivate func connectionWillReconnect(error: Error?) {
guard reconnectAlert == nil else {
print("Alert already present. This is unexpected.")
return
}
reconnectAlert = UIAlertController(title: "Reconnecting...", message: "Please wait", preferredStyle: .alert)
self.present(reconnectAlert!, animated: true, completion: nil)
}
fileprivate func connectionDidReconnect() {
reconnectAlert?.dismiss(animated: true, completion: nil)
reconnectAlert = nil
}
func blockUI(message: String, error: Error?) {
var message = message
if let e = error {
message.append(" Error: \(e)")
}
appendMessage(message: message)
toggleUI(isEnabled: false)
}
func toggleUI(isEnabled: Bool) {
sendButton.isEnabled = isEnabled
msgTextField.isEnabled = isEnabled
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count = -1
dispatchQueue.sync {
count = self.messages.count
}
return count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextCell", for: indexPath)
let row = indexPath.row
cell.textLabel?.text = messages[row]
return cell
}
}
class ChatHubConnectionDelegate: HubConnectionDelegate {
weak var controller: ViewController?
init(controller: ViewController) {
self.controller = controller
}
func connectionDidOpen(hubConnection: HubConnection) {
controller?.connectionDidOpen()
}
func connectionDidFailToOpen(error: Error) {
controller?.connectionDidFailToOpen(error: error)
}
func connectionDidClose(error: Error?) {
controller?.connectionDidClose(error: error)
}
func connectionWillReconnect(error: Error) {
controller?.connectionWillReconnect(error: error)
}
func connectionDidReconnect() {
controller?.connectionDidReconnect()
}
}