This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
http-to-websocket-upgrade.html
326 lines (268 loc) · 23 KB
/
http-to-websocket-upgrade.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta property="og:url" content="https://ballerinalang.org" />
<meta property="og:type" content="article" />
<meta property="og:title" content="Flexible. Powerful. Beautiful." />
<meta property="og:description" content="Ballerina by Example is a hands-on introduction to Ballerina using annotated example programs. Check out the first example or browse the full list below. " />
<meta property="og:image" content="http://ballerinalang.org/img/ballerina-fb.jpg" />
<link rel="shortcut icon" href="https://ballerinalang.org/img/favicon.ico" type="image/x-icon">
<link rel="icon" href="https://ballerinalang.org/img/favicon.ico" type="image/x-icon">
<title>Ballerina by Example: HTTP to WebSocket Upgrade</title>
<!--GA-->
<script>
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-92163714-1', 'auto');
ga('send', 'pageview');
</script>
<!-- Google Tag Manager -->
<script>(function (w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({'gtm.start':
new Date().getTime(), event: 'gtm.js'});
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-KW2LLTW');
</script>
<!-- End Google Tag Manager -->
<!-- Hotjar Tracking Code for https://ballerinalang.org/ -->
<script>
(function (h, o, t, j, a, r) {
h.hj = h.hj || function () {
(h.hj.q = h.hj.q || []).push(arguments)
};
h._hjSettings = {hjid: 442732, hjsv: 5};
a = o.getElementsByTagName('head')[0];
r = o.createElement('script');
r.async = 1;
r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv;
a.appendChild(r);
})(window, document, '//static.hotjar.com/c/hotjar-', '.js?sv=');
</script>
<link href="https://b.content.wso2.com/sites/all/bootstrap/css/bootstrap.css?230617" rel="stylesheet">
<script src="https://b.content.wso2.com/sites/all/bootstrap/js/jquery.js?230617"></script>
<script src="https://b.content.wso2.com/sites/all/bootstrap/js/bootstrap.js?230617"></script>
<script src="https://b.content.wso2.com/sites/all/ballerina/ballerina-example.js"></script>
<script defer="defer" src="https://wso2.cachefly.net/wso2/sites/all/ballerina/js/ballerina.js"></script>
<link rel=stylesheet href="site.css">
<link href="https://fonts.googleapis.com/css?family=Cairo:200,300,400,600,700,900" rel="stylesheet">
<link rel=stylesheet href="https://b.content.wso2.com/sites/all/ballerina/ballerina-example.css">
</head>
<body>
<div class="row cBallerinaNav" id="iBallerinaNavigation">
</div>
<div class="row cBallerinaBySampleRow">
<div class="container cBallerinaBySampleContainer">
<div class="FullCode">
<div class="highlight"><pre><span class="kn">import</span> <span class="nx">ballerina</span><span class="p">.</span><span class="nx">net</span><span class="p">.</span><span class="nx">http</span><span class="p">;</span>
<span class="kn">import</span> <span class="nx">ballerina</span><span class="p">.</span><span class="nx">net</span><span class="p">.</span><span class="nx">ws</span><span class="p">;</span><span class="nd">@http:configuration {</span>
<span class="nx">basePath</span><span class="p">:</span><span class="s">"/hello"</span><span class="p">,</span>
<span class="nx">webSocket</span><span class="p">:</span><span class="nd">@http:webSocket {</span>
<span class="nx">upgradePath</span><span class="p">:</span><span class="s">"/ws"</span><span class="p">,</span>
<span class="nx">serviceName</span><span class="p">:</span><span class="s">"wsService"</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kd">service</span><span class="p"><</span><span class="nx">http</span><span class="p">></span> <span class="nx">httpService</span> <span class="p">{</span> <span class="nd">@http:resourceConfig {</span>
<span class="nx">path</span><span class="p">:</span><span class="s">"/world"</span><span class="p">,</span>
<span class="nx">methods</span><span class="p">:[</span><span class="s">"POST"</span><span class="p">,</span><span class="s">"GET"</span><span class="p">,</span><span class="s">"PUT"</span><span class="p">,</span><span class="s">"My"</span><span class="p">]</span>
<span class="p">}</span>
<span class="kd">resource</span> <span class="nx">testResource</span><span class="p">(</span><span class="nx">http</span><span class="p">:</span><span class="nx">Connection</span> <span class="nx">conn</span><span class="p">,</span> <span class="nx">http</span><span class="p">:</span><span class="nx">InRequest</span> <span class="nx">req</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">http</span><span class="p">:</span><span class="nx">OutResponse</span> <span class="nx">resp</span> <span class="p">=</span> <span class="p">{};</span>
<span class="kt">string</span> <span class="nx">payload</span> <span class="p">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">getStringPayload</span><span class="p">();</span>
<span class="nb">println</span><span class="p">(</span><span class="nx">payload</span><span class="p">);</span>
<span class="nx">resp</span><span class="p">.</span><span class="nx">setStringPayload</span><span class="p">(</span><span class="s">"I received"</span><span class="p">);</span>
<span class="nx">_</span> <span class="p">=</span> <span class="nx">conn</span><span class="p">.</span><span class="nx">respond</span><span class="p">(</span><span class="nx">resp</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nd">@ws:configuration {</span>
<span class="nx">basePath</span><span class="p">:</span><span class="s">"world/ws"</span><span class="p">,</span>
<span class="nx">subProtocols</span><span class="p">:[</span><span class="s">"xml, json"</span><span class="p">],</span>
<span class="nx">idleTimeoutInSeconds</span><span class="p">:</span><span class="mi">5</span>
<span class="p">}</span>
<span class="kd">service</span><span class="p"><</span><span class="nx">ws</span><span class="p">></span> <span class="nx">wsService</span> <span class="p">{</span> <span class="kd">resource</span> <span class="nx">onOpen</span><span class="p">(</span><span class="nx">ws</span><span class="p">:</span><span class="nx">Connection</span> <span class="nx">conn</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">println</span><span class="p">(</span><span class="s">"New WebSocket connection: "</span> <span class="o">+</span> <span class="nx">conn</span><span class="p">.</span><span class="nx">getID</span><span class="p">());</span>
<span class="p">}</span> <span class="kd">resource</span> <span class="nx">onTextMessage</span><span class="p">(</span><span class="nx">ws</span><span class="p">:</span><span class="nx">Connection</span> <span class="nx">conn</span><span class="p">,</span> <span class="nx">ws</span><span class="p">:</span><span class="nx">TextFrame</span> <span class="nx">frame</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">println</span><span class="p">(</span><span class="nx">frame</span><span class="p">.</span><span class="nx">text</span><span class="p">);</span>
<span class="nx">conn</span><span class="p">.</span><span class="nx">pushText</span><span class="p">(</span><span class="nx">frame</span><span class="p">.</span><span class="nx">text</span><span class="p">);</span>
<span class="p">}</span> <span class="kd">resource</span> <span class="nx">onIdleTimeout</span><span class="p">(</span><span class="nx">ws</span><span class="p">:</span><span class="nx">Connection</span> <span class="nx">conn</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">println</span><span class="p">(</span><span class="s">"Idle timeout: "</span> <span class="o">+</span> <span class="nx">conn</span><span class="p">.</span><span class="nx">getID</span><span class="p">());</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="example" id="http-to-websocket-upgrade">
<div class="col-xs-11 col-sm-11 col-md-11">
<h2><a href="./">Ballerina by Example</a>: HTTP to WebSocket Upgrade</h2>
</div>
<div class="col-xs-1 col-sm-1 col-md-1">
<a href="./" class="cBackButton"><img src="http://b.content.wso2.com/sites/all/ballerina/backbutton.svg"/></a>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<table>
<tr>
<td class="docs">
<p>This sample explains how a Http endpoint can be updated to a WebSocket endpoint using configuration annotation.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="nx">ballerina</span><span class="p">.</span><span class="nx">net</span><span class="p">.</span><span class="nx">http</span><span class="p">;</span>
<span class="kn">import</span> <span class="nx">ballerina</span><span class="p">.</span><span class="nx">net</span><span class="p">.</span><span class="nx">ws</span><span class="p">;</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="nd">@http:configuration {</span>
<span class="nx">basePath</span><span class="p">:</span><span class="s">"/hello"</span><span class="p">,</span>
<span class="nx">webSocket</span><span class="p">:</span><span class="nd">@http:webSocket {</span>
<span class="nx">upgradePath</span><span class="p">:</span><span class="s">"/ws"</span><span class="p">,</span>
<span class="nx">serviceName</span><span class="p">:</span><span class="s">"wsService"</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kd">service</span><span class="p"><</span><span class="nx">http</span><span class="p">></span> <span class="nx">httpService</span> <span class="p">{</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nd">@http:resourceConfig {</span>
<span class="nx">path</span><span class="p">:</span><span class="s">"/world"</span><span class="p">,</span>
<span class="nx">methods</span><span class="p">:[</span><span class="s">"POST"</span><span class="p">,</span><span class="s">"GET"</span><span class="p">,</span><span class="s">"PUT"</span><span class="p">,</span><span class="s">"My"</span><span class="p">]</span>
<span class="p">}</span>
<span class="kd">resource</span> <span class="nx">testResource</span><span class="p">(</span><span class="nx">http</span><span class="p">:</span><span class="nx">Connection</span> <span class="nx">conn</span><span class="p">,</span> <span class="nx">http</span><span class="p">:</span><span class="nx">InRequest</span> <span class="nx">req</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">http</span><span class="p">:</span><span class="nx">OutResponse</span> <span class="nx">resp</span> <span class="p">=</span> <span class="p">{};</span>
<span class="kt">string</span> <span class="nx">payload</span> <span class="p">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">getStringPayload</span><span class="p">();</span>
<span class="nb">println</span><span class="p">(</span><span class="nx">payload</span><span class="p">);</span>
<span class="nx">resp</span><span class="p">.</span><span class="nx">setStringPayload</span><span class="p">(</span><span class="s">"I received"</span><span class="p">);</span>
<span class="nx">_</span> <span class="p">=</span> <span class="nx">conn</span><span class="p">.</span><span class="nx">respond</span><span class="p">(</span><span class="nx">resp</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Note: When a WebSocket upgrade path is defined in HTTP configuration in WebSocket configuration there can be \n - Full service configuration: There will be two base paths for the same service from either HTTP or WebSocket \n - Without service configuration: WebSocket service will be a slave service of HTTP service. Then only the upgrade path can be there. \n - Configuration without basePath: It will act as a slave service but can configure sub protocols, idle timeout etc…</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="nd">@ws:configuration {</span>
<span class="nx">basePath</span><span class="p">:</span><span class="s">"world/ws"</span><span class="p">,</span>
<span class="nx">subProtocols</span><span class="p">:[</span><span class="s">"xml, json"</span><span class="p">],</span>
<span class="nx">idleTimeoutInSeconds</span><span class="p">:</span><span class="mi">5</span>
<span class="p">}</span>
<span class="kd">service</span><span class="p"><</span><span class="nx">ws</span><span class="p">></span> <span class="nx">wsService</span> <span class="p">{</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="kd">resource</span> <span class="nx">onOpen</span><span class="p">(</span><span class="nx">ws</span><span class="p">:</span><span class="nx">Connection</span> <span class="nx">conn</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">println</span><span class="p">(</span><span class="s">"New WebSocket connection: "</span> <span class="o">+</span> <span class="nx">conn</span><span class="p">.</span><span class="nx">getID</span><span class="p">());</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="kd">resource</span> <span class="nx">onTextMessage</span><span class="p">(</span><span class="nx">ws</span><span class="p">:</span><span class="nx">Connection</span> <span class="nx">conn</span><span class="p">,</span> <span class="nx">ws</span><span class="p">:</span><span class="nx">TextFrame</span> <span class="nx">frame</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">println</span><span class="p">(</span><span class="nx">frame</span><span class="p">.</span><span class="nx">text</span><span class="p">);</span>
<span class="nx">conn</span><span class="p">.</span><span class="nx">pushText</span><span class="p">(</span><span class="nx">frame</span><span class="p">.</span><span class="nx">text</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code">
<div class="highlight"><pre> <span class="kd">resource</span> <span class="nx">onIdleTimeout</span><span class="p">(</span><span class="nx">ws</span><span class="p">:</span><span class="nx">Connection</span> <span class="nx">conn</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">println</span><span class="p">(</span><span class="s">"Idle timeout: "</span> <span class="o">+</span> <span class="nx">conn</span><span class="p">.</span><span class="nx">getID</span><span class="p">());</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>To run the program, put the code in <code>http-to-websocket-upgrade.bal</code>
and use <code>ballerina run http-to-websocket-upgrade.bal</code> command.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> ballerina run http-to-websocket-upgrade.bal
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>To check the sample,use Chrome or Firefox javascript console and run the below commands <br>
change “xml” to another sub protocol to observe the behavior of WebSocket server.
There are two endpoints for this WebSocket sample since it is configured that way.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> var <span class="nv">ws</span> <span class="o">=</span> new WebSocket<span class="o">(</span><span class="s2">"ws://localhost:9090/hello/ws"</span>, <span class="s2">"xml"</span>, <span class="s2">"my-protocol"</span><span class="o">)</span><span class="p">;</span>
<span class="gp">$</span> var <span class="nv">ws</span> <span class="o">=</span> new WebSocket<span class="o">(</span><span class="s2">"ws://localhost:9090/world/ws"</span>, <span class="s2">"xml"</span>, <span class="s2">"my-protocol"</span><span class="o">)</span><span class="p">;</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> ws.onmessage <span class="o">=</span> <span class="k">function</span><span class="o">(</span>frame<span class="o">)</span> <span class="o">{</span>console.log<span class="o">(</span>frame.data<span class="o">)}</span><span class="p">;</span>
<span class="gp">$</span> ws.onclose <span class="o">=</span> <span class="k">function</span><span class="o">(</span>frame<span class="o">)</span> <span class="o">{</span>console.log<span class="o">(</span>frame<span class="o">)}</span><span class="p">;</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>To send messages</p>
</td>
<td class="code">
<div class="highlight"><pre><span class="gp">$</span> ws.send<span class="o">(</span><span class="s2">"hello world"</span><span class="o">)</span><span class="p">;</span>
</pre></div>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="http-cors">HTTP CORS</a>.
</p>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 cGOfooter">
<p class="footer">
by <a href="https://twitter.com/ballerinalang">@ballerinalang</a> | <a href="mailto:ballerina-dev@googlegroups.com">feedback</a> | <a href="https://github.com/ballerinalang/ballerina/tree/master/samples/ballerina-by-example/examples/http-to-websocket-upgrade">source</a>
</p>
<p class="attrib">
This site was generated using a <a href="https://github.com/mmcgrana/gobyexample/">tool</a> developed by <a href="https://twitter.com/mmcgrana">@mmcgrana</a>, licensed under <a href="https://creativecommons.org/licenses/by/2.0/">CC BY</a> / modified from original
</p>
</div>
</div>
</div>
</div>
<div class="row cBallerinaFooter" id="iBallerinaFooter">
</div>
</body>
</html>