This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| public protocol Then {} | |
| extension Then where Self: Any { | |
| /// Makes it available to set properties with closures just after initializing and copying the value types. | |
| /// | |
| /// let frame = CGRect().with { | |
| /// $0.origin.x = 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // connection disconnected | |
| func urlSession(URLSession, webSocketTask: URLSessionWebSocketTask, | |
| didCloseWith: URLSessionWebSocketTask.CloseCode, | |
| reason: Data?) | |
| // connection established | |
| func urlSession(URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol: String?) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| webSocketTask.cancel(closeCode: .goingAway, reason: nil) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func sendPing() { | |
| webSocketTask.sendPing { (error) in | |
| if let error = error { | |
| print("Sending PING failed: \(error)") | |
| } | |
| DispatchQueue.main.asyncAfter(deadline: .now() + 10) { | |
| self.sendPing() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func receiveMessage() { | |
| webSocketTask.receive { result in | |
| switch result { | |
| case .failure(let error): | |
| print("Error in receiving message: \(error)") | |
| case .success(let message): | |
| switch message { | |
| case .string(let text): | |
| print("Received string: \(text)") | |
| case .data(let data): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| webSocketTask.receive { result in | |
| switch result { | |
| case .failure(let error): | |
| print("Error in receiving message: \(error)") | |
| case .success(let message): | |
| switch message { | |
| case .string(let text): | |
| print("Received string: \(text)") | |
| case .data(let data): | |
| print("Received data: \(data)") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let message = URLSessionWebSocketTask.Message.string("Hello World”) | |
| webSocketTask.send(message) { error in | |
| if let error = error { | |
| print("WebSocket couldn’t send message because: \(error)") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let urlSession = URLSession(configuration: .default) | |
| let webSocketTask = urlSession.webSocketTask(with: "wss://echo.websocket.org") | |
| webSocketTask.resume() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func webSocketTask(with: URL) -> URLSessionWebSocketTask | |
| func webSocketTask(with: URLRequest) -> URLSessionWebSocketTask | |
| func webSocketTask(with: URL, protocols: [String]) -> URLSessionWebSocketTask |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Put this file in /usr/local/bin and then run chmod +x on it to make it executable | |
| command=$1 | |
| shift | |
| case $command in | |
| "init" ) | |
| swift package init "$@" |