-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContents.swift
67 lines (54 loc) · 2.1 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Playground generated with 🏟 Arena (https://github.com/finestructure/arena)
// ℹ️ If running the playground fails with an error "no such module ..."
// go to Product -> Build to re-trigger building the SPM package.
// ℹ️ Please restart Xcode if autocomplete is not working.
import Atlantis
import PlaygroundSupport
import Foundation
// Make sure the playground doesn't terminate
PlaygroundPage.current.needsIndefiniteExecution = true
// Accept all challenges from Charles or Proxyman for self-signed certificates
class NetworkSSlProxying: NSObject, URLSessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}
}
let httpEndpoint = "https://httpbin.org/get?name=proxyman"
// Init session
let delegate = NetworkSSlProxying()
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: delegate, delegateQueue: nil)
// HTTPS
let task = session.dataTask(with: URLRequest(url: URL(string: httpEndpoint)!)) { (data, response, error) in
guard let response = response else {
return
}
print(response)
}
// Web Socket
let wsTask = session.webSocketTask(with: URL(string: "wss://echo.websocket.events")!)
wsTask.receive { result in
switch result {
case .success(let message):
switch message {
case .string(let text):
print("[WSS] Received: \(text)")
case .data(let data):
print("[WSS] Received: Data count = \(data.count)")
}
case .failure(let error):
print(error)
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
let content = "Hello from Proxyman and Atlantis"
wsTask.send(URLSessionWebSocketTask.Message.string(content)) { error in
if let error = error {
print(error)
}
}
}
// Run on Playground mode
Atlantis.setIsRunningOniOSPlayground(true)
Atlantis.start()
task.resume()
wsTask.resume()