|
1 | | -# WebsocketKit |
| 1 | +# WebSocketKit |
2 | 2 |
|
3 | | -A description of this package. |
| 3 | +<p align="center"> |
| 4 | + <a href="https://developer.apple.com/swift/"> |
| 5 | + <img src="https://img.shields.io/badge/Swift-5.0-orange.svg?style=flat" alt="Swift 5.0"> |
| 6 | + </a> |
| 7 | + <a href="https://github.com/apple/swift-package-manager"> |
| 8 | + <img src="https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg" alt="SPM"> |
| 9 | + </a> |
| 10 | + |
| 11 | + <a href="https://github.com/alexanderwe/LoggingKit"> |
| 12 | + <img src="https://github.com/alexanderwe/WebSocketKit/workflows/CI/badge.svg" alt="CI"> |
| 13 | + </a> |
| 14 | + <a href="https://codecov.io/gh/alexanderwe/WebSocketKit"> |
| 15 | + <img src="https://codecov.io/gh/alexanderwe/WebSocketKit/branch/main/graph/badge.svg?token=zNmgmMp5zB" alt="Code coverage"> |
| 16 | + </a> |
| 17 | +</p> |
| 18 | + |
| 19 | +<p align="center"> |
| 20 | + WebSocketKit is a small wrapper around the `Network` framework to work with websocket connections |
| 21 | +</p> |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +### Swift Package Manager |
| 26 | + |
| 27 | +To integrate using Apple's [Swift Package Manager](https://swift.org/package-manager/), add the following as a dependency to your `Package.swift`: |
| 28 | + |
| 29 | +```swift |
| 30 | +dependencies: [ |
| 31 | + .package(url: "https://github.com/alexanderwe/WebSocketKit.git", from: "1.0.0") |
| 32 | +] |
| 33 | +``` |
| 34 | + |
| 35 | +Alternatively navigate to your Xcode project, select `Swift Packages` and click the `+` icon to search for `WebSocketKit`. |
| 36 | + |
| 37 | +### Manually |
| 38 | + |
| 39 | +If you prefer not to use any of the aforementioned dependency managers, you can integrate LoggingKit into your project manually. Simply drag the `Sources` Folder into your Xcode project. |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +At first import `WebSocketKit` |
| 44 | + |
| 45 | +```swift |
| 46 | +import WebSocketKit |
| 47 | +``` |
| 48 | + |
| 49 | +Define a `WebsSocket` instance |
| 50 | + |
| 51 | +```swift |
| 52 | +let websocket = WebsSocket(url: URL(string: "wss://echo.websocket.org")!) |
| 53 | +``` |
| 54 | + |
| 55 | +It also makes sense to create a instance of a class that conforms to the `WebSocketConnectionDelegate` in order to receive websocket events. Be aware that you also need to import the `Network` framework in order to have access to `NWProtocolWebSocket`. |
| 56 | + |
| 57 | +```swift |
| 58 | +import Network |
| 59 | + |
| 60 | + |
| 61 | +class WebSocketDelegate: WebSocketConnectionDelegate { |
| 62 | + |
| 63 | + func webSocketDidConnect(connection: WebSocketConnection) { |
| 64 | + print("WebSocket did connect") |
| 65 | + } |
| 66 | + |
| 67 | + func websocketDidPrepare(connection: WebSocketConnection) { |
| 68 | + print("WebSocket did prepare") |
| 69 | + } |
| 70 | + |
| 71 | + func webSocketDidDisconnect(connection: WebSocketConnection, closeCode: NWProtocolWebSocket.CloseCode, reason: Data?) { |
| 72 | + print("WebSocket did disconnect") |
| 73 | + } |
| 74 | + |
| 75 | + func websocketDidCancel(connection: WebSocketConnection) { |
| 76 | + print("WebSocket did cancel") |
| 77 | + } |
| 78 | + |
| 79 | + func webSocketDidReceiveError(connection: WebSocketConnection, error: Error) { |
| 80 | + print("WebSocket did receive error: \(error)") |
| 81 | + } |
| 82 | + |
| 83 | + func webSocketDidReceivePong(connection: WebSocketConnection) { |
| 84 | + print("WebSocket did receive pong") |
| 85 | + } |
| 86 | + |
| 87 | + func webSocketDidReceiveMessage(connection: WebSocketConnection, string: String) { |
| 88 | + print("WebSocket did receive string message: \(string)") |
| 89 | + } |
| 90 | + |
| 91 | + func webSocketDidReceiveMessage(connection: WebSocketConnection, data: Data) { |
| 92 | + print("WebSocket did receive data message") |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Set an instance of the delegate instance to the `Websocket` instance and start listening for events |
| 98 | + |
| 99 | +```swift |
| 100 | +let delegate = WebSocketDelegate() |
| 101 | +websocket.delegate = delegate |
| 102 | + |
| 103 | +websocket.connect() // Connects to the url specified in the initializer and listens for messages |
| 104 | +``` |
| 105 | + |
| 106 | +### Custom headers |
| 107 | + |
| 108 | +Often it is necessary to attach custom headers to the connection. You can do so by specifying them in the initializer of the `Websocket` class. |
| 109 | + |
| 110 | +```swift |
| 111 | +let websocket = Websocket(url: URL(string: "wss://echo.websocket.org")!, |
| 112 | + additionalHeaders: [ |
| 113 | + "Authorization:": "Bearer <someToken>", |
| 114 | + "My-Custom-Header-Key:": "My-Custom-Header-Value" |
| 115 | + ] |
| 116 | +) |
| 117 | +``` |
| 118 | + |
| 119 | +## Contributing |
| 120 | + |
| 121 | +Contributions are very welcome 🙌 |
0 commit comments