Skip to content

Instantly share code, notes, and snippets.

View fassko's full-sized avatar
👋
Hi! I'm Kristaps - Web3 and iOS Swift developer. Let's chat!

Kristaps Grinbergs fassko

👋
Hi! I'm Kristaps - Web3 and iOS Swift developer. Let's chat!
View GitHub Profile
@fassko
fassko / Then.swift
Created April 8, 2020 08:25
Do / with shorthand
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
@fassko
fassko / websocket8.swift
Created July 30, 2019 08:11
Websocket connection state with iOS 13
// connection disconnected
func urlSession(URLSession, webSocketTask: URLSessionWebSocketTask,
didCloseWith: URLSessionWebSocketTask.CloseCode,
reason: Data?)
// connection established
func urlSession(URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol: String?)
@fassko
fassko / websocket7.swift
Created July 30, 2019 08:10
Close Websocket
webSocketTask.cancel(closeCode: .goingAway, reason: nil)
@fassko
fassko / websocket6.swift
Created July 30, 2019 08:06
Ping/ping with Websockets iOS13
func sendPing() {
webSocketTask.sendPing { (error) in
if let error = error {
print("Sending PING failed: \(error)")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
self.sendPing()
}
}
@fassko
fassko / websocket5.swift
Created July 30, 2019 08:05
Receiving messages with Websocket iOS 13
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):
@fassko
fassko / websocket4.swift
Created July 30, 2019 08:03
Receiving messages with Websocket iOS13
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)")
@fassko
fassko / websocket3.swift
Created July 30, 2019 06:09
Sending messages with Websocket iOS 13
let message = URLSessionWebSocketTask.Message.string("Hello World”)
webSocketTask.send(message) { error in
if let error = error {
print("WebSocket couldn’t send message because: \(error)")
}
}
@fassko
fassko / websocket2.swift
Created July 30, 2019 06:08
Opening connection
let urlSession = URLSession(configuration: .default)
let webSocketTask = urlSession.webSocketTask(with: "wss://echo.websocket.org")
webSocketTask.resume()
@fassko
fassko / websockets1.swift
Created July 30, 2019 06:05
WebSockets iOS13 #1
func webSocketTask(with: URL) -> URLSessionWebSocketTask
func webSocketTask(with: URLRequest) -> URLSessionWebSocketTask
func webSocketTask(with: URL, protocols: [String]) -> URLSessionWebSocketTask
@fassko
fassko / spm
Created September 28, 2018 09:29 — forked from JohnSundell/spm
A script that makes it easier to use the Swift Package Manager by making common commands less verbose 👍
#!/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 "$@"