fix ios issue
This commit is contained in:
parent
49989b0e58
commit
c7154b1465
@ -14,6 +14,8 @@ public class Socket: NSObject {
|
|||||||
|
|
||||||
weak var delegate: SocketDelegate?
|
weak var delegate: SocketDelegate?
|
||||||
|
|
||||||
|
var dataBuffer = Data()
|
||||||
|
|
||||||
public init(id: String) {
|
public init(id: String) {
|
||||||
self.id = id
|
self.id = id
|
||||||
}
|
}
|
||||||
@ -35,7 +37,7 @@ public class Socket: NSObject {
|
|||||||
let connection = NWConnection(host: NWEndpoint.Host(host), port: NWEndpoint.Port(String(port))!, using: parameters)
|
let connection = NWConnection(host: NWEndpoint.Host(host), port: NWEndpoint.Port(String(port))!, using: parameters)
|
||||||
connection.stateUpdateHandler = self.stateDidChange(to:)
|
connection.stateUpdateHandler = self.stateDidChange(to:)
|
||||||
self.receive(on: connection)
|
self.receive(on: connection)
|
||||||
connection.start(queue: .main)
|
connection.start(queue: DispatchQueue(label: "Igloo"))
|
||||||
self.connection = connection
|
self.connection = connection
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,53 +48,64 @@ public class Socket: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func disconnect() {
|
public func disconnect() {
|
||||||
connection?.forceCancel()
|
connection?.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
func stateDidChange(to state: NWConnection.State) {
|
func stateDidChange(to state: NWConnection.State) {
|
||||||
switch state {
|
switch state {
|
||||||
case .setup:
|
case .setup:
|
||||||
print("connection setup")
|
print("connection setup")
|
||||||
break
|
|
||||||
|
|
||||||
case .waiting(let error):
|
case .waiting(let error):
|
||||||
print("connection waiting: \(error)")
|
print("connection waiting: \(error)")
|
||||||
break
|
|
||||||
|
|
||||||
case .preparing:
|
case .preparing:
|
||||||
self.delegate?.didChangeState(socket: self.id, state: .connecting)
|
self.delegate?.didChangeState(socket: self.id, state: .connecting)
|
||||||
break
|
|
||||||
|
|
||||||
case .ready:
|
case .ready:
|
||||||
self.delegate?.didChangeState(socket: self.id, state: .connected)
|
self.delegate?.didChangeState(socket: self.id, state: .connected)
|
||||||
break
|
|
||||||
|
|
||||||
case .failed(let error):
|
case .failed(let error):
|
||||||
print("connection failed: \(error)")
|
print("connection failed: \(error)")
|
||||||
break
|
self.delegate?.didChangeState(socket: self.id, state: .disconnected)
|
||||||
|
self.disconnect()
|
||||||
|
|
||||||
case .cancelled:
|
case .cancelled:
|
||||||
self.delegate?.didChangeState(socket: self.id, state: .disconnected)
|
self.delegate?.didChangeState(socket: self.id, state: .disconnected)
|
||||||
break
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
print("other")
|
print("other")
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func receive(on connection: NWConnection) {
|
func receive(on connection: NWConnection) {
|
||||||
connection.receive(minimumIncompleteLength: 1, maximumLength: 65536) { (data, contentContext, isComplete, error) in
|
connection.receive(minimumIncompleteLength: 1, maximumLength: 65536) { (data, contentContext, isComplete, error) in
|
||||||
|
if let error = error {
|
||||||
|
print("Receive error: \(error)")
|
||||||
|
self.delegate?.didChangeState(socket: self.id, state: .disconnected)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if let data = data, !data.isEmpty {
|
if let data = data, !data.isEmpty {
|
||||||
if let message = String(data: data, encoding: .utf8) {
|
self.dataBuffer.append(data) // Append the data to the buffer
|
||||||
|
|
||||||
|
// Process buffer if enough data is available (protocol-specific logic)
|
||||||
|
if let message = String(data: self.dataBuffer, encoding: .utf8) {
|
||||||
self.delegate?.didReceiveMessage(socket: self.id, message: message)
|
self.delegate?.didReceiveMessage(socket: self.id, message: message)
|
||||||
|
self.dataBuffer.removeAll() // Clear buffer after processing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connection.state == .ready) {
|
if connection.state == .ready {
|
||||||
self.receive(on: connection)
|
self.receive(on: connection)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isComplete {
|
||||||
|
print("Connection completed.")
|
||||||
|
self.delegate?.didChangeState(socket: self.id, state: .disconnected)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user