|
6 | 6 | //
|
7 | 7 |
|
8 | 8 | import Foundation
|
| 9 | +import Starscream |
9 | 10 |
|
10 |
| -public class StompClient { |
| 11 | +// |
| 12 | +public typealias MessageHandler = (_ frame : Frame) -> Any |
| 13 | + |
| 14 | +public class StompClient : WebSocketDelegate { |
| 15 | + |
| 16 | + private var endpoint: URL |
| 17 | + private var handler : MessageHandler = {_ in } |
| 18 | + private var underlyWebsocket : WebSocket |
| 19 | + |
| 20 | + public var wsConnected = false |
| 21 | + |
| 22 | + // status of stomp client |
| 23 | + // CONNECTING -> CONNECTED -> DISCONNECTED |
| 24 | + public var status = StompStatus.STARTING |
| 25 | + |
| 26 | + // STOMP Protocol version |
| 27 | + // initially UNKNOWN was set, after handshake, the really version will be set. |
| 28 | + private var version = StompVersions.UNKNOWN |
| 29 | + |
| 30 | + // STOMP Heart beat value. 0 means not send. |
| 31 | + private var heartbeat = 0 |
| 32 | + |
| 33 | + // On Connected Handling callback hook. |
| 34 | + private var onConnected : (_ client: StompClient) -> Any = {_ in } |
| 35 | + |
| 36 | + // intilize the underlying websocket object. |
| 37 | + // at this point, the websocket had not connected to server yet. |
| 38 | + public init(endpoint url : String) { |
| 39 | + self.endpoint = URL(string: url)! |
| 40 | + var request = URLRequest(url: endpoint) |
| 41 | + |
| 42 | + request.timeoutInterval = 5 |
| 43 | + underlyWebsocket = WebSocket(request: request) |
| 44 | + underlyWebsocket.delegate = self |
| 45 | + } |
| 46 | + |
| 47 | + // |
| 48 | + public func startConnect () { |
| 49 | + underlyWebsocket.connect() |
| 50 | + } |
| 51 | + |
| 52 | + // |
| 53 | + public func subscribe(to topic : String, handleby handler: @escaping MessageHandler) -> StompClient { |
| 54 | + let data = ("SUBSCRIBE\nid:sub-0\n" |
| 55 | + + "destination:" + topic + "\n" |
| 56 | + + "ack:client\n\n\0" |
| 57 | + ).data(using: .utf8)! |
| 58 | + |
| 59 | + underlyWebsocket.write(data: data) |
| 60 | + |
| 61 | + self.handler = handler |
| 62 | + return self |
| 63 | + } |
| 64 | + |
| 65 | + // |
| 66 | + public func disconnect() { |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + // |
| 71 | + public func send(_ msg: String) { |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + // |
| 76 | + public func didReceive(event: WebSocketEvent, client: WebSocket) { |
| 77 | + switch event { |
| 78 | + case .connected(let headers): |
| 79 | + wsConnected = true |
| 80 | + print("websocket is connected: \(headers)") |
| 81 | + let command = Frame.connectFrame(versions: "1.2,1.1") |
| 82 | + |
| 83 | + let data = command.toData()! |
| 84 | + |
| 85 | + underlyWebsocket.write(data: data) |
| 86 | + |
| 87 | + case .disconnected(let reason, let code): |
| 88 | + wsConnected = false |
| 89 | + print("websocket is disconnected: \(reason) with code: \(code)") |
| 90 | + |
| 91 | + case .text(let string): |
| 92 | + print("Received text: \(string)") |
| 93 | + // handle stomp frame |
| 94 | + handleFrame(text: string) |
| 95 | + |
| 96 | + case .binary(let data): |
| 97 | + // Exception: a stomp client expects no binary data |
| 98 | + print("Received data: \(data.count)") |
| 99 | + |
| 100 | + case .ping(_): |
| 101 | + break |
| 102 | + |
| 103 | + case .pong(_): |
| 104 | + break |
| 105 | + |
| 106 | + case .viabilityChanged(_): |
| 107 | + break |
| 108 | + |
| 109 | + case .reconnectSuggested(_): |
| 110 | + break |
| 111 | + |
| 112 | + case .cancelled: |
| 113 | + wsConnected = false |
| 114 | + |
| 115 | + case .error(let error): |
| 116 | + wsConnected = false |
| 117 | + handleError(error ) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Handle Frame text from server, update client status or call message handler |
| 122 | + // according to the content of the frame. |
| 123 | + func handleFrame(text: String) { |
| 124 | + // check |
| 125 | + |
| 126 | + let parser = FrameParser(as: .VER1_2) |
| 127 | + parser.parse(text: text) |
| 128 | + |
| 129 | + let frame = parser.resultFrame |
| 130 | + |
| 131 | + if (frame == nil) { |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + // if CONNECTED |
| 136 | + if (frame!.isConnected) { |
| 137 | + status = .CONNECTED |
| 138 | + // retrieve heart beat |
| 139 | + // retrieve protocol version |
| 140 | + |
| 141 | + // call back to onConnected function |
| 142 | + |
| 143 | + } else if ( frame!.isMessage) { |
| 144 | + // if MESSAGE |
| 145 | + |
| 146 | + } else if ( frame!.isReceipt) { |
| 147 | + // if RECEIPT |
| 148 | + |
| 149 | + } else if ( frame!.isError) { |
| 150 | + // if ERROR |
| 151 | + |
| 152 | + } else { |
| 153 | + // exception |
| 154 | + } |
| 155 | + } |
11 | 156 |
|
12 |
| - public init() { |
| 157 | + // |
| 158 | + func handleError(_ error : Error?) { |
13 | 159 |
|
14 | 160 | }
|
15 | 161 | }
|
0 commit comments