nuclearace / Socket.IO-Client-Swift

socket.io-client for Swift
Other
361 stars 53 forks source link

Socket On listeners donot fire but emit works fine #172

Open Xcode09 opened 5 years ago

Xcode09 commented 5 years ago

i am working on chat application and facing some issues when i send the message , it sent succuessfully but my listeners donot fire and message cannot updates i make a sperate class code is here please help open class SocketIOManager {

public static let default = SocketIOManager() private let manager: SocketManager private let socket: SocketIOClient

private init() { self.manager = SocketManager(socketURL: URL(string: "http://18.220.164.142:3000/")!, config: [.connectParams(["token": UserState.gettoken() ?? ""]), .compress,.log(true)]) self.socket = manager.defaultSocket }

//To check before any call is socket connected or not. func isSocketConnected() -> Bool{ if self.socket.status == .connected { return true } return false } //Connection with socket Handshaking func establishConnection(completionHandler: @escaping (Bool) -> Void) { socket.on(clientEvent: .connect) {data, ack in completionHandler(true) print("socket connected (data)") } socket.connect() } //Connection with socket Handshaking func establishConnection() { socket.connect()

} //Socket connection disconnect func closeConnection() { socket.disconnect() socket.off(clientEvent: .disconnect) }

//Socket Remove Listeners func removeListeners() { socket.off("message") socket.off("typing") socket.off("stopTyping") }

func getChatMessage(completionHandler: @escaping (GetChatConversation?,String?) -> Void) {

socket.on("message") { (dataArray, socketAck) -> Void in
  print(dataArray)

  if let data = dataArray.first as? Data{
    do{
      let objc = try JSONDecoder.init().decode(GetChatConversation.self, from: data)
      completionHandler(objc,nil)
    }
    catch let error{
      return completionHandler(nil,error.localizedDescription)
    }
  }else{
    completionHandler(nil,"Cannot cast Array of Any to Data")
  }
}

}

func getStartTypingResponse(completionHandler: @escaping (String) -> Void) { socket.on("typing") { (dataArray, socketAck) -> Void in let swiftyJsonVar = JSON(dataArray as Any) let messageDictionary = swiftyJsonVar.arrayValue[0]

  completionHandler(messageDictionary["sender"].stringValue)
}

}

func getStopTypingResponse(completionHandler: @escaping (String) -> Void) { socket.on("stopTyping") { (dataArray, socketAck) -> Void in let swiftyJsonVar = JSON(dataArray as Any) let messageDictionary = swiftyJsonVar.arrayValue[0]

  completionHandler(messageDictionary["sender"].stringValue)
}

}

func sendStartTypingMessage(conversationId: String) {

var typingDict = [String: Any]()
typingDict.updateValue(conversationId, forKey: "conversationId")

if let bearerToken = UserState.gettoken() {
  if bearerToken.count > 0 {
    typingDict.updateValue(bearerToken, forKey: "token")
  }
}
socket.emitWithAck("typing", typingDict).timingOut(after: 0.1, callback: { data in
  print("\(data)")
})  }

func sendStopTypingMessage(conversationId: String) {

var stopTypingDict = [String: Any]()
stopTypingDict.updateValue(conversationId, forKey: "conversationId")

if let bearerToken = UserState.gettoken() {
  if bearerToken.count > 0 {
    stopTypingDict.updateValue(bearerToken, forKey: "token")
  }
}
socket.emitWithAck("stopTyping", stopTypingDict).timingOut(after: 0.1, callback: { data in
  print("\(data)")
})

}

func sendMessage(message: String, conversationId: String, completionHandler: @escaping (Bool) -> Void) {

var conversationDictionary = [String: Any]()
conversationDictionary.updateValue(message, forKey: "message")
conversationDictionary.updateValue(conversationId, forKey: "conversationId")
conversationDictionary.updateValue(Date().millisecondsSince1970, forKey: "timestamp")

if let bearerToken = UserState.gettoken() {
  if bearerToken.count > 0 {
    conversationDictionary.updateValue(bearerToken, forKey: "token")
  }
}

socket.emitWithAck("message", conversationDictionary).timingOut(after: 0.1, callback: { data in
  print("\(data)")

})

} }