https://github.com/alexanderwe/websocketkit
Small utility around the Network framework and WebSockets
https://github.com/alexanderwe/websocketkit
network swift swift5 websocket
Last synced: 11 months ago
JSON representation
Small utility around the Network framework and WebSockets
- Host: GitHub
- URL: https://github.com/alexanderwe/websocketkit
- Owner: alexanderwe
- License: mit
- Created: 2020-10-17T11:41:53.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-30T18:09:46.000Z (over 5 years ago)
- Last Synced: 2024-03-14T14:02:15.277Z (over 2 years ago)
- Topics: network, swift, swift5, websocket
- Language: Swift
- Homepage: https://alexanderwe.github.io/WebSocketKit/
- Size: 201 KB
- Stars: 2
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# WebSocketKit
WebSocketKit is a small wrapper around the `Network` framework to work with websocket connections
## Installation
### Swift Package Manager
To integrate using Apple's [Swift Package Manager](https://swift.org/package-manager/), add the following as a dependency to your `Package.swift`:
```swift
dependencies: [
.package(url: "https://github.com/alexanderwe/WebSocketKit.git", from: "1.0.0")
]
```
Alternatively navigate to your Xcode project, select `Swift Packages` and click the `+` icon to search for `WebSocketKit`.
### Manually
If you prefer not to use any of the aforementioned dependency managers, you can integrate `WebSocketKit` into your project manually. Simply drag the `Sources` Folder into your Xcode project.
## Usage
At first import `WebSocketKit`
```swift
import WebSocketKit
```
Define a `WebsSocket` instance
```swift
let websocket = WebsSocket(url: URL(string: "wss://echo.websocket.org")!)
```
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`.
```swift
import Network
class WebSocketDelegate: WebSocketConnectionDelegate {
func webSocketDidConnect(connection: WebSocketConnection) {
print("WebSocket did connect")
}
func websocketDidPrepare(connection: WebSocketConnection) {
print("WebSocket did prepare")
}
func webSocketDidDisconnect(connection: WebSocketConnection, closeCode: NWProtocolWebSocket.CloseCode, reason: Data?) {
print("WebSocket did disconnect")
}
func websocketDidCancel(connection: WebSocketConnection) {
print("WebSocket did cancel")
}
func webSocketDidReceiveError(connection: WebSocketConnection, error: Error) {
print("WebSocket did receive error: \(error)")
}
func webSocketDidReceivePong(connection: WebSocketConnection) {
print("WebSocket did receive pong")
}
func webSocketDidReceiveMessage(connection: WebSocketConnection, string: String) {
print("WebSocket did receive string message: \(string)")
}
func webSocketDidReceiveMessage(connection: WebSocketConnection, data: Data) {
print("WebSocket did receive data message")
}
}
```
Set an instance of the delegate instance to the `Websocket` instance and start listening for events
```swift
let delegate = WebSocketDelegate()
websocket.delegate = delegate
websocket.connect() // Connects to the url specified in the initializer and listens for messages
```
### Custom headers
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.
```swift
let websocket = Websocket(url: URL(string: "wss://echo.websocket.org")!,
additionalHeaders: [
"Authorization:": "Bearer ",
"My-Custom-Header-Key:": "My-Custom-Header-Value"
]
)
```
## Contributing
Contributions are very welcome 🙌