KMWebsocketManager.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // KMWebsocketManager.swift
  3. // PDF Reader Pro DMG
  4. //
  5. // Created by wanjun on 2023/6/29.
  6. //
  7. import Cocoa
  8. enum KMWebsocketEvent : Int {
  9. case KMWebsocketEventPaySuccess = 0
  10. case KMWebsocketEventPayFail
  11. }
  12. class KMWebsocketManager: NSObject {
  13. static let defaultManager = KMWebsocketManager()
  14. var websocket: KMWebsocket?
  15. var content: [String : String] = [:]
  16. override init() {
  17. super.init()
  18. self.websocket = KMWebsocket()
  19. self.websocket?.delegate = self
  20. }
  21. //MARK: Private Methods
  22. //MARK: Public Methods
  23. func connect() -> Void {
  24. #if VERSION_DMG
  25. let endPoint: String = ""
  26. #endif
  27. self.websocket?.connect(endPoint: endPoint)
  28. }
  29. func disconnect() -> Void {
  30. self.websocket?.disconnect()
  31. }
  32. }
  33. extension KMWebsocketManager: KMWebsocketDelegate {
  34. func websocketDidConnect() {
  35. }
  36. func websocketDidDisconnect() {
  37. }
  38. func websocketDidError(error: Error) {
  39. self.disconnect()
  40. }
  41. func websocketDidReceive(content: String) {
  42. DispatchQueue.main.async {
  43. let data: Data = content.data(using: .utf8)!
  44. let content: [String : String] = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String : String]
  45. self.content = content
  46. let event: String = content["event"]!
  47. if event == "pay_success" {
  48. } else if event == "pay_fail" {
  49. }
  50. }
  51. }
  52. func webSocketDidReceiveData(data: Data) {
  53. }
  54. }