You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

415 lines
14 KiB

5 years ago
  1. ![starscream](https://raw.githubusercontent.com/daltoniam/starscream/assets/starscream.jpg)
  2. Starscream is a conforming WebSocket ([RFC 6455](http://tools.ietf.org/html/rfc6455)) client library in Swift.
  3. Its Objective-C counterpart can be found here: [Jetfire](https://github.com/acmacalister/jetfire)
  4. ## Features
  5. - Conforms to all of the base [Autobahn test suite](http://autobahn.ws/testsuite/).
  6. - Nonblocking. Everything happens in the background, thanks to GCD.
  7. - TLS/WSS support.
  8. - Compression Extensions support ([RFC 7692](https://tools.ietf.org/html/rfc7692))
  9. - Simple concise codebase at just a few hundred LOC.
  10. ## Example
  11. First thing is to import the framework. See the Installation instructions on how to add the framework to your project.
  12. ```swift
  13. import Starscream
  14. ```
  15. Once imported, you can open a connection to your WebSocket server. Note that `socket` is probably best as a property, so it doesn't get deallocated right after being setup.
  16. ```swift
  17. socket = WebSocket(url: URL(string: "ws://localhost:8080/")!)
  18. socket.delegate = self
  19. socket.connect()
  20. ```
  21. After you are connected, there are some delegate methods that we need to implement.
  22. ### websocketDidConnect
  23. websocketDidConnect is called as soon as the client connects to the server.
  24. ```swift
  25. func websocketDidConnect(socket: WebSocketClient) {
  26. print("websocket is connected")
  27. }
  28. ```
  29. ### websocketDidDisconnect
  30. websocketDidDisconnect is called as soon as the client is disconnected from the server.
  31. ```swift
  32. func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
  33. print("websocket is disconnected: \(error?.localizedDescription)")
  34. }
  35. ```
  36. ### websocketDidReceiveMessage
  37. websocketDidReceiveMessage is called when the client gets a text frame from the connection.
  38. ```swift
  39. func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
  40. print("got some text: \(text)")
  41. }
  42. ```
  43. ### websocketDidReceiveData
  44. websocketDidReceiveData is called when the client gets a binary frame from the connection.
  45. ```swift
  46. func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
  47. print("got some data: \(data.count)")
  48. }
  49. ```
  50. ### Optional: websocketDidReceivePong *(required protocol: WebSocketPongDelegate)*
  51. websocketDidReceivePong is called when the client gets a pong response from the connection. You need to implement the WebSocketPongDelegate protocol and set an additional delegate, eg: ` socket.pongDelegate = self`
  52. ```swift
  53. func websocketDidReceivePong(socket: WebSocketClient, data: Data?) {
  54. print("Got pong! Maybe some data: \(data?.count)")
  55. }
  56. ```
  57. Or you can use closures.
  58. ```swift
  59. socket = WebSocket(url: URL(string: "ws://localhost:8080/")!)
  60. //websocketDidConnect
  61. socket.onConnect = {
  62. print("websocket is connected")
  63. }
  64. //websocketDidDisconnect
  65. socket.onDisconnect = { (error: Error?) in
  66. print("websocket is disconnected: \(error?.localizedDescription)")
  67. }
  68. //websocketDidReceiveMessage
  69. socket.onText = { (text: String) in
  70. print("got some text: \(text)")
  71. }
  72. //websocketDidReceiveData
  73. socket.onData = { (data: Data) in
  74. print("got some data: \(data.count)")
  75. }
  76. //you could do onPong as well.
  77. socket.connect()
  78. ```
  79. One more: you can listen to socket connection and disconnection via notifications. Starscream posts `WebsocketDidConnectNotification` and `WebsocketDidDisconnectNotification`. You can find an `Error` that caused the disconection by accessing `WebsocketDisconnectionErrorKeyName` on notification `userInfo`.
  80. ## The delegate methods give you a simple way to handle data from the server, but how do you send data?
  81. ### write a binary frame
  82. The writeData method gives you a simple way to send `Data` (binary) data to the server.
  83. ```swift
  84. socket.write(data: data) //write some Data over the socket!
  85. ```
  86. ### write a string frame
  87. The writeString method is the same as writeData, but sends text/string.
  88. ```swift
  89. socket.write(string: "Hi Server!") //example on how to write text over the socket!
  90. ```
  91. ### write a ping frame
  92. The writePing method is the same as write, but sends a ping control frame.
  93. ```swift
  94. socket.write(ping: Data()) //example on how to write a ping control frame over the socket!
  95. ```
  96. ### write a pong frame
  97. the writePong method is the same as writePing, but sends a pong control frame.
  98. ```swift
  99. socket.write(pong: Data()) //example on how to write a pong control frame over the socket!
  100. ```
  101. Starscream will automatically respond to incoming `ping` control frames so you do not need to manually send `pong`s.
  102. However if for some reason you need to control this process you can turn off the automatic `ping` response by disabling `respondToPingWithPong`.
  103. ```swift
  104. socket.respondToPingWithPong = false //Do not automaticaly respond to incoming pings with pongs.
  105. ```
  106. In most cases you will not need to do this.
  107. ### disconnect
  108. The disconnect method does what you would expect and closes the socket.
  109. ```swift
  110. socket.disconnect()
  111. ```
  112. The socket can be forcefully closed, by specifying a timeout (in milliseconds). A timeout of zero will also close the socket immediately without waiting on the server.
  113. ```swift
  114. socket.disconnect(forceTimeout: 10, closeCode: CloseCode.normal.rawValue)
  115. ```
  116. ### isConnected
  117. Returns if the socket is connected or not.
  118. ```swift
  119. if socket.isConnected {
  120. // do cool stuff.
  121. }
  122. ```
  123. ### Custom Headers
  124. You can also override the default websocket headers with your own custom ones like so:
  125. ```swift
  126. var request = URLRequest(url: URL(string: "ws://localhost:8080/")!)
  127. request.timeoutInterval = 5
  128. request.setValue("someother protocols", forHTTPHeaderField: "Sec-WebSocket-Protocol")
  129. request.setValue("14", forHTTPHeaderField: "Sec-WebSocket-Version")
  130. request.setValue("Everything is Awesome!", forHTTPHeaderField: "My-Awesome-Header")
  131. let socket = WebSocket(request: request)
  132. ```
  133. ### Custom HTTP Method
  134. Your server may use a different HTTP method when connecting to the websocket:
  135. ```swift
  136. var request = URLRequest(url: URL(string: "ws://localhost:8080/")!)
  137. request.httpMethod = "POST"
  138. request.timeoutInterval = 5
  139. let socket = WebSocket(request: request)
  140. ```
  141. ### Protocols
  142. If you need to specify a protocol, simple add it to the init:
  143. ```swift
  144. //chat and superchat are the example protocols here
  145. socket = WebSocket(url: URL(string: "ws://localhost:8080/")!, protocols: ["chat","superchat"])
  146. socket.delegate = self
  147. socket.connect()
  148. ```
  149. ### Self Signed SSL
  150. ```swift
  151. socket = WebSocket(url: URL(string: "ws://localhost:8080/")!, protocols: ["chat","superchat"])
  152. //set this if you want to ignore SSL cert validation, so a self signed SSL certificate can be used.
  153. socket.disableSSLCertValidation = true
  154. ```
  155. ### SSL Pinning
  156. SSL Pinning is also supported in Starscream.
  157. ```swift
  158. socket = WebSocket(url: URL(string: "ws://localhost:8080/")!, protocols: ["chat","superchat"])
  159. let data = ... //load your certificate from disk
  160. socket.security = SSLSecurity(certs: [SSLCert(data: data)], usePublicKeys: true)
  161. //socket.security = SSLSecurity() //uses the .cer files in your app's bundle
  162. ```
  163. You load either a `Data` blob of your certificate or you can use a `SecKeyRef` if you have a public key you want to use. The `usePublicKeys` bool is whether to use the certificates for validation or the public keys. The public keys will be extracted from the certificates automatically if `usePublicKeys` is choosen.
  164. ### SSL Cipher Suites
  165. To use an SSL encrypted connection, you need to tell Starscream about the cipher suites your server supports.
  166. ```swift
  167. socket = WebSocket(url: URL(string: "wss://localhost:8080/")!, protocols: ["chat","superchat"])
  168. // Set enabled cipher suites to AES 256 and AES 128
  169. socket.enabledSSLCipherSuites = [TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256]
  170. ```
  171. If you don't know which cipher suites are supported by your server, you can try pointing [SSL Labs](https://www.ssllabs.com/ssltest/) at it and checking the results.
  172. ### Compression Extensions
  173. Compression Extensions ([RFC 7692](https://tools.ietf.org/html/rfc7692)) is supported in Starscream. Compression is enabled by default, however compression will only be used if it is supported by the server as well. You may enable or disable compression via the `.enableCompression` property:
  174. ```swift
  175. socket = WebSocket(url: URL(string: "ws://localhost:8080/")!)
  176. socket.enableCompression = false
  177. ```
  178. Compression should be disabled if your application is transmitting already-compressed, random, or other uncompressable data.
  179. ### Custom Queue
  180. A custom queue can be specified when delegate methods are called. By default `DispatchQueue.main` is used, thus making all delegate methods calls run on the main thread. It is important to note that all WebSocket processing is done on a background thread, only the delegate method calls are changed when modifying the queue. The actual processing is always on a background thread and will not pause your app.
  181. ```swift
  182. socket = WebSocket(url: URL(string: "ws://localhost:8080/")!, protocols: ["chat","superchat"])
  183. //create a custom queue
  184. socket.callbackQueue = DispatchQueue(label: "com.vluxe.starscream.myapp")
  185. ```
  186. ## Example Project
  187. Check out the SimpleTest project in the examples directory to see how to setup a simple connection to a WebSocket server.
  188. ## Requirements
  189. Starscream works with iOS 7/OSX 10.9 or above. It is recommended to use iOS 8/10.10 or above for CocoaPods/framework support. To use Starscream with a project targeting iOS 7, you must include all Swift files directly in your project.
  190. ## Installation
  191. ### CocoaPods
  192. Check out [Get Started](http://cocoapods.org/) tab on [cocoapods.org](http://cocoapods.org/).
  193. To use Starscream in your project add the following 'Podfile' to your project
  194. source 'https://github.com/CocoaPods/Specs.git'
  195. platform :ios, '9.0'
  196. use_frameworks!
  197. pod 'Starscream', '~> 3.0.2'
  198. Then run:
  199. pod install
  200. ### Carthage
  201. Check out the [Carthage](https://github.com/Carthage/Carthage) docs on how to add a install. The `Starscream` framework is already setup with shared schemes.
  202. [Carthage Install](https://github.com/Carthage/Carthage#adding-frameworks-to-an-application)
  203. You can install Carthage with [Homebrew](http://brew.sh/) using the following command:
  204. ```bash
  205. $ brew update
  206. $ brew install carthage
  207. ```
  208. To integrate Starscream into your Xcode project using Carthage, specify it in your `Cartfile`:
  209. ```
  210. github "daltoniam/Starscream" >= 3.0.2
  211. ```
  212. ### Rogue
  213. First see the [installation docs](https://github.com/acmacalister/Rogue) for how to install Rogue.
  214. To install Starscream run the command below in the directory you created the rogue file.
  215. ```
  216. rogue add https://github.com/daltoniam/Starscream
  217. ```
  218. Next open the `libs` folder and add the `Starscream.xcodeproj` to your Xcode project. Once that is complete, in your "Build Phases" add the `Starscream.framework` to your "Link Binary with Libraries" phase. Make sure to add the `libs` folder to your `.gitignore` file.
  219. ### Swift Package Manager
  220. The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler.
  221. Once you have your Swift package set up, adding Starscream as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`.
  222. ```swift
  223. dependencies: [
  224. .Package(url: "https://github.com/daltoniam/Starscream.git", majorVersion: 3)
  225. ]
  226. ```
  227. ### Other
  228. Simply grab the framework (either via git submodule or another package manager).
  229. Add the `Starscream.xcodeproj` to your Xcode project. Once that is complete, in your "Build Phases" add the `Starscream.framework` to your "Link Binary with Libraries" phase.
  230. ### Add Copy Frameworks Phase
  231. If you are running this in an OSX app or on a physical iOS device you will need to make sure you add the `Starscream.framework` to be included in your app bundle. To do this, in Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the "Targets" heading in the sidebar. In the tab bar at the top of that window, open the "Build Phases" panel. Expand the "Link Binary with Libraries" group, and add `Starscream.framework`. Click on the + button at the top left of the panel and select "New Copy Files Phase". Rename this new phase to "Copy Frameworks", set the "Destination" to "Frameworks", and add `Starscream.framework` respectively.
  232. ## WebSocketAdvancedDelegate
  233. The advanced delegate acts just like the simpler delegate but provides some additional information on the connection and incoming frames.
  234. ```swift
  235. socket.advancedDelegate = self
  236. ```
  237. In most cases you do not need the extra info and should use the normal delegate.
  238. #### websocketDidReceiveMessage
  239. ```swift
  240. func websocketDidReceiveMessage(socket: WebSocketClient, text: String, response: WebSocket.WSResponse) {
  241. print("got some text: \(text)")
  242. print("First frame for this message arrived on \(response.firstFrame)")
  243. }
  244. ```
  245. #### websocketDidReceiveData
  246. ```swift
  247. func websocketDidReceiveData(socket: WebSocketClient, data: Date, response: WebSocket.WSResponse) {
  248. print("got some data it long: \(data.count)")
  249. print("A total of \(response.frameCount) frames were used to send this data")
  250. }
  251. ```
  252. #### websocketHttpUpgrade
  253. These methods are called when the HTTP upgrade request is sent and when the response returns.
  254. ```swift
  255. func websocketHttpUpgrade(socket: WebSocketClient, request: CFHTTPMessage) {
  256. print("the http request was sent we can check the raw http if we need to")
  257. }
  258. ```
  259. ```swift
  260. func websocketHttpUpgrade(socket: WebSocketClient, response: CFHTTPMessage) {
  261. print("the http response has returned.")
  262. }
  263. ```
  264. ## Swift versions
  265. * Swift 4.2 - 3.0.6
  266. ## KNOWN ISSUES
  267. - WatchOS does not have the the CFNetwork String constants to modify the stream's SSL behavior. It will be the default Foundation SSL behavior. This means watchOS CANNOT use `SSLCiphers`, `disableSSLCertValidation`, or SSL pinning. All these values set on watchOS will do nothing.
  268. - Linux does not have the security framework, so it CANNOT use SSL pinning or `SSLCiphers` either.
  269. ## TODOs
  270. - [ ] Add Unit Tests - Local WebSocket server that runs against Autobahn
  271. ## License
  272. Starscream is licensed under the Apache v2 License.
  273. ## Contact
  274. ### Dalton Cherry
  275. * https://github.com/daltoniam
  276. * http://twitter.com/daltoniam
  277. * http://daltoniam.com
  278. ### Austin Cherry ###
  279. * https://github.com/acmacalister
  280. * http://twitter.com/acmacalister
  281. * http://austincherry.me