-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
117 lines (105 loc) · 6.11 KB
/
index.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="og:site_name" content="Kristaps Grinbergs">
<link rel="canonical" href="https://www.swiftwebsockets.com">
<meta name="twitter:url" content="https://www.swiftwebsockets.com">
<meta name="twitter:title" content="Swift WebSockets">
<meta name="og:url" content="https://www.swiftwebsockets.com">
<title>Swift Websockets by Kristaps Grinbergs</title>
<meta name="description" content="Swift Websockets by Kristaps Grinbergs">
<meta name="twitter:description" content="Swift Websockets by Kristaps Grinbergs">
<meta name="og:description" content="Swift Websockets by Kristaps Grinbergs">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="shortcut icon" href="/img/favicon.svg" type="image/svg">
<meta property="og:image" content="https://www.swiftwebsockets.com/img/social.png">
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="627" />
<meta property="og:type" content="website">
<meta property="og:url" content="https://www.swiftwebsockets.com">
<meta name="twitter:image" content="//www.swiftwebsockets.com/img/social.png">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600&display=swap&subset=latin-ext" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="midnight.css">
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="https://www.swiftwebsockets.com/img/apple-touch-icon-120x120.png">
</head>
<body>
<div class="wrapper">
<div class="intro">
<h1>
<img class="swift-logo" src="/img/swift-logo.svg" alt="Swift logo">
Swift Websockets
</h1>
<p class="intro-text">
This is a quick starter guide how to work with WebSockets in our Swift projects for iOS, iPadOS, tvOS, watchOS and macOS.
</p>
<p class="intro-text">
From iOS 13.0+, iPadOS 13.0+, macOS 10.15+, tvOS 13.0+ and watchOS 6.0+ you can use <a href="https://developer.apple.com/documentation/foundation/urlsessionwebsockettask" target="_blank">URLWebSocketTask</a>. For earlier versions please refer <a href="https://kristaps.me/websockets-swift/" target="_blank">here</a>.
</p>
</div>
<div class="example">
<h2>// <span class="caps">Create</span> the Connection</h2>
<pre><code><span class="keyword">let</span> url = <span class="type">URL</span>(string: <span class="string">"wss://echo.websocket.org"</span>)!
<span class="keyword">let</span> webSocketTask = <span class="type">URLSession</span>.<span class="property">shared</span>.<span class="call">webSocketTask</span>(with: url)
webSocketTask.<span class="call">resume</span>()</code></pre>
</div>
<div class="example">
<h2>// <span class="caps">Send</span> the Data</h2>
<pre><code>webSocketTask.<span class="call">send</span>(.<span class="call">string</span>(<span class="string">"Hello"</span>)) { error <span class="keyword">in
if let</span> error = error {
<span class="call">print</span>(<span class="string">"Error when sending a message</span> \(error)<span class="string">"</span>)
}
}</code></pre>
</div>
<div class="example">
<h2>// <span class="caps">Receive</span> the Data</h2>
<pre><code>webSocketTask.<span class="call">receive</span> { result <span class="keyword">in
switch</span> result {
<span class="keyword">case</span> .<span class="dotAccess">success</span>(<span class="keyword">let</span> message):
<span class="keyword">switch</span> message {
<span class="keyword">case</span> .<span class="dotAccess">data</span>(<span class="keyword">let</span> data):
<span class="call">print</span>(<span class="string">"Data received</span> \(data)<span class="string">"</span>)
<span class="keyword">case</span> .<span class="dotAccess">string</span>(<span class="keyword">let</span> text):
<span class="call">print</span>(<span class="string">"Text received</span> \(text)<span class="string">"</span>)
}
<span class="keyword">case</span> .<span class="dotAccess">failure</span>(<span class="keyword">let</span> error):
<span class="call">print</span>(<span class="string">"Error when receiving</span> \(error)<span class="string">"</span>)
}
}</code></pre>
</div>
<div class="example">
<h2>// <span class="caps">Ping/Pong</span></h2>
<pre><code>webSocketTask.<span class="call">sendPing</span> { error <span class="keyword">in
if let</span> error = error {
<span class="call">print</span>(<span class="string">"Error when sending PING</span> \(error)<span class="string">"</span>)
}
}</code></pre>
</div>
<div class="example">
<h2>// <span class="caps">Close</span> the Connection</h2>
<pre><code><span class="keyword">let</span> reason = <span class="string">"Goodbye"</span>.<span class="call">data</span>(using: .<span class="dotAccess">utf8</span>)
webSocketTask.<span class="call">cancel</span>(with: .<span class="dotAccess">goingAway</span>, reason: reason)</code></pre>
</div>
<div class="materials">
<a class="link--white" href="SwiftWebSockets.zip" target="_blank" download="SwiftWebSockets.zip">
<img class="link-icon" src="/img/playground-icon.svg" alt="Swift playgrounds" class="playground-icon">
Download Playground
</a>
</div>
<footer>
<p class="text--sm">Made with ❤️ in 🇱🇻</p>
<p class="text--sm">
by <a class="link--red" href="https://twitter.com/fassko" target="_blank">@fassko</a> & <a class="link--red" href="https://github.com/Ninuce" target="_blank">@ninuce</a>
</p>
<p class="github">
<a href="https://github.com/fassko/swiftwebsockets.com" target="_blank">
<img src="/img/github.svg">
</a>
</p>
</footer>
</div>
</body>
</html>