forked from cockpit-project/cockpit-project.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcockpit-channels.html
More file actions
306 lines (306 loc) · 16.4 KB
/
cockpit-channels.html
File metadata and controls
306 lines (306 loc) · 16.4 KB
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
---
layout: guide
---
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>cockpit.js: Raw Channels</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Cockpit Guide">
<link rel="up" href="api-base1.html" title="API: base1">
<link rel="prev" href="cockpit-series-data.html" title="cockpit.js: Series Data">
<link rel="next" href="cockpit-location.html" title="cockpit.js: Page Location and Jumping">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
<td><a accesskey="p" href="cockpit-series-data.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="api-base1.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">Cockpit Guide</th>
<td><a accesskey="n" href="cockpit-location.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="refentry">
<a name="cockpit-channels"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2>cockpit.js: Raw Channels</h2>
<p>cockpit.js: Raw Channels — Raw communication channels</p>
</td>
<td valign="top" align="right"></td>
</tr></table></div>
<div class="refsynopsisdiv">
<h2>Synopsis</h2>
<p>At a low level Cockpit communicates with the system via messages passed
through various channels. These are usually exposed via higher level APIs,
such as the <code class="code"><a class="link" href="cockpit-spawn.html" title="cockpit.js: Spawning Processes">cockpit.spawn()</a></code> function.
It is rare to use raw channels directly.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-channel"></a><h2>cockpit.channel()</h2>
<pre class="programlisting">
channel = cockpit.channel(options)
</pre>
<p>This function creates a new channel for communication with the system.
It returns a new channel object. The <code class="code">options</code> argument is a
plain object. At least the <code class="code">"payload"</code> option is required, and
based on the payload type, other options may be required.</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><code class="code">"binary"</code></span></p></td>
<td><p>Set to <code class="code">true</code> to transfer binary payloads. Both messages
sent via <a class="link" href="cockpit-channels.html#cockpit-channels-send" title="channel.send()"><code class="code">channel.send()</code></a>
and those received via
<a class="link" href="cockpit-channels.html#cockpit-channels-message" title="channel.onmessage"><code class="code">channel.onmessage</code></a>
should be arrays of bytes, either <code class="code">Uint8Array</code> or <code class="code">Array</code>
depending on browser support.</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="code">"payload"</code></span></p></td>
<td><p>The payload type for the channel. Only specific payload
types are supported.</p></td>
</tr>
<tr>
<td><p><span class="term"><code class="code">"superuser"</code></span></p></td>
<td>
<p>Set to <code class="code">"require"</code> to open this channel as root. If the currently
logged in user is not permitted to become root (eg: via <code class="code">pkexec</code>) then the
<code class="code">channel</code> will immediately be
<a class="link" href="cockpit-channels.html#cockpit-channels-close-ev" title="channel.onclose">closed</a> with a <code class="code">"access-denied"</code>
problem code.</p>
<p>Set to <code class="code">"try"</code> to try to open the channel as root, but if that fails,
then fall back to an unprivileged channel.</p>
</td>
</tr>
</tbody>
</table></div>
<p>The channel object returned has the following fields and methods and
events. You should call the
<code class="code"><a class="link" href="cockpit-channels.html#cockpit-channels-close" title="channel.close()">channel.close()</a></code>
method when done with the channel.</p>
<p>A valid channel will always be returned and the is ready to
<code class="code"><a class="link" href="cockpit-channels.html#cockpit-channels-send" title="channel.send()">channel.send()</a></code>. The channel may
<a class="link" href="cockpit-channels.html#cockpit-channels-close-ev" title="channel.onclose">close shortly afterword</a> due
to a failure.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-binary"></a><h2>channel.binary</h2>
<p>Will be <code class="code">true</code> for an binary channel. Will be set to <code class="code">false</code> if the
channel is textual.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-options"></a><h2>channel.options</h2>
<p>The options used to open this channel. This should not be changed.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-valid"></a><h2>channel.valid</h2>
<p>Will be <code class="code">true</code> for an open channel. Will be set to <code class="code">false</code> if the channel
closes.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-send"></a><h2>channel.send()</h2>
<pre class="programlisting">
channel.send(data)
</pre>
<p>Send a message over the channel. The contents of the message depends on the
payload type of the channel. If a binary channel, then <code class="code">data</code> is expected
to be an <code class="code">Array</code> of bytes or a <code class="code">Uint8Array</code>. If not binary,
then the <code class="code">data</code> will be converted to a string if not already a string.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-control"></a><h2>channel.control()</h2>
<pre class="programlisting">
channel.control(options)
</pre>
<p>Notify the channel to tune certain parameters on the fly. The <code class="code">options</code>
is a plain javascript object, and the contents depend on the <code class="code">"payload"</code>
of the channel.</p>
<p>One common operation is to set <code class="code">"command"</code> to <code class="code">"done"</code> in the
options field. To indicate that no further messages will be sent through the channel.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-wait"></a><h2>channel.wait()</h2>
<pre class="programlisting">
promise = channel.wait([callback])
</pre>
<p>Returns a <code class="code">promise</code> that is ready when the channel is ready, or fails if the
client closes. If a <code class="code">callback</code> is specified, it is attached to the promise. The
promise will be rejected or resolved with the contents <code class="code">options</code> passed to the
<a class="link" href="cockpit-channels.html#cockpit-channels-onready" title="channel.onready">channel.onready</a> and
<a class="link" href="cockpit-channels.html#cockpit-channels-close-ev" title="channel.onclose">channel.onclose</a> events respectively.</p>
<p>In general it's not necessary to wait for the channel before starting to use the channel.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-close"></a><h2>channel.close()</h2>
<pre class="programlisting">
channel.close([options])
</pre>
<p>Close the channel.</p>
<p>If <code class="code">options</code> is present it can be a plain javascript object
containing additional channel close options to send to the peer. If closing for
because of a problem, set the <code class="code">"problem"</code> field to a
<a class="link" href="cockpit-error.html#cockpit-problems" title="Problem Codes">problem code</a>. If <code class="code">options</code>
is not an object it will be treated as a <code class="code">"problem"</code>.</p>
<p>The <a class="link" href="cockpit-channels.html#cockpit-channels-close-ev" title="channel.onclose">close event</a> will fire.
A channel can also be closed by a peer or if the underlying transport closes.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-message"></a><h2>channel.onmessage</h2>
<pre class="programlisting">
channel.addEventListener("message", function(event, data) { ... })
</pre>
<p>An event triggered when the channel receives a message. The message is
passed as a string to the handler in the <code class="code">data</code>. In the case of binary
channels <code class="code">data</code> is an <code class="code">Uint8Array</code> or an <code class="code">Array</code>
of bytes if the former is not supported by the browser. The contents of
the message depends on the payload type of the channel.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-oncontrol"></a><h2>channel.oncontrol</h2>
<pre class="programlisting">
channel.addEventListener("control", function(event, options) { ... })
</pre>
<p>An event triggered when the channel receives an control message in the
middle of the flow. One particular use is when the <code class="code">command</code> is set to
<code class="code">"done"</code> then no further messages will be received in the channel.
The exact form of these messages depend on the <code class="code">"payload"</code> of the
channel.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-onready"></a><h2>channel.onready</h2>
<pre class="programlisting">
channel.addEventListener("ready", function(event, options) { ... })
</pre>
<p>An event triggered when the other end of the channel is ready to start processing
messages. This indicates the channel is completely open. It is possible to start
sending messages on the channel before this point.</p>
</div>
<div class="refsection">
<a name="cockpit-channels-close-ev"></a><h2>channel.onclose</h2>
<pre class="programlisting">
channel.addEventListener("close", function(event, options) { ... })
</pre>
<p>An event triggered when the channel closes. This can happen either because
<a class="link" href="cockpit-channels.html#cockpit-channels-close" title="channel.close()">channel.close()</a> function was called,
or if the peer closed the channel, or the underlying transport closes.</p>
<p>The <code class="code">options</code> will contain various close information, including a
<code class="code">"problem"</code> field which will be set if the channel was closed because
of a problem.</p>
</div>
<div class="refsection">
<a name="cockpit-transport-origin"></a><h2>cockpit.transport.origin</h2>
<pre class="programlisting">
cockpit.transport.origin
</pre>
<p>The HTTP origin that is being used by the underlying channel transport. This is
read-only, you should not assign a value. If the browser supports
<code class="code">window.location.origin</code> then this will be identical to that value.</p>
</div>
<div class="refsection">
<a name="cockpit-transport-host"></a><h2>cockpit.transport.host</h2>
<pre class="programlisting">
cockpit.transport.host
</pre>
<p>The host that this transport is going to talk to by default. This is
read-only, you should not assign a value. If the value is null that means that the
transport has not been setup yet.</p>
</div>
<div class="refsection">
<a name="cockpit-transport-csrf-token"></a><h2>cockpit.transport.csrf_token</h2>
<pre class="programlisting">
cockpit.transport.csrf_token
</pre>
<p>A cross site request forgery token for use with external channels. This becomes
valid once the connection is properly established.</p>
</div>
<div class="refsection">
<a name="cockpit-transport-options"></a><h2>cockpit.transport.options</h2>
<pre class="programlisting">
cockpit.transport.options
</pre>
<p>Initialization options received over the underlying channel transport. These
will be empty until connection is properly established.</p>
</div>
<div class="refsection">
<a name="cockpit-transport-wait"></a><h2>cockpit.transport.wait()</h2>
<pre class="programlisting">
cockpit.transport.wait(callback)
</pre>
<p>Call the <code class="code">callback</code> function once the underlying channel transport is initialized.
This will start the initialization if not already in progress or completed. If the
channel transport is already initialized, then <code class="code">callback</code> will be called
immediately.</p>
<p>In general it's not necessary to wait for the transport before starting to open channels.</p>
</div>
<div class="refsection">
<a name="cockpit-transport-close"></a><h2>cockpit.transport.close()</h2>
<pre class="programlisting">
cockpit.transport.close([problem])
</pre>
<p>Close the underlying channel transport. All channels open channels will close.
The <code class="code">problem</code> argument should be a problem code string. If not specified
it will default to <code class="code">"disconnected"</code>.</p>
</div>
<div class="refsection">
<a name="cockpit-transport-filter"></a><h2>cockpit.transport.filter()</h2>
<pre class="programlisting">
cockpit.transport.filter((message, channelid, control) => { ... })
</pre>
<p>Add a filter to the underlying channel transport. All incoming messages will be
passed to each of the filter callbacks that are registered.</p>
<p>This function is rarely used.</p>
<p>Filter callbacks are called in the order they are registered. If a filter
callback returns <code class="code">false</code> then the message will not be dispatched
further, whether to other filters, or to channels, etc.</p>
<p>The <code class="code">message</code> is the string or array with the raw message including,
the framing. The <code class="code">channelid</code> is the channel identifier or an empty string
for control messages. If <code class="code">control</code> is set then this is a control message,d
and the <code class="code">control</code> argument contains the parsed JSON object of the
control message.</p>
</div>
<div class="refsection">
<a name="cockpit-transport-inject"></a><h2>cockpit.transport.inject()</h2>
<pre class="programlisting">
cockpit.transport.inject(message, [out])
</pre>
<p>Inject a message into the underlying channel transport. The <code class="code">message</code>
should be a <code class="code">string</code> or an array of bytes, and should be valid
according to the Cockpit message protocol. If the <code class="code">out</code> argument is equal
to <code class="code">false</code> then the message will be injected as an incoming message as if
it was received on the underlying channel transport.</p>
<p>This function is rarely used. In general you should only <code class="code">inject()</code>
messages you got from a <code class="code"><a class="link" href="cockpit-channels.html#cockpit-transport-filter" title="cockpit.transport.filter()">filter()</a></code>.</p>
</div>
<div class="refsection">
<a name="cockpit-base64-encode"></a><h2>cockpit.base64_encode()</h2>
<pre class="programlisting">
string = cockpit.base64_encode(data)
</pre>
<p>Encode binary data into a string using the Base64 encoding. The <code class="code">data</code>
argument can either be a <code class="code">string</code>, an <code class="code">Array</code>, an <code class="code">ArrayBuffer</code>
or a <code class="code">Uint8Array</code>. The return value is a string.</p>
</div>
<div class="refsection">
<a name="cockpit-base64-decode"></a><h2>cockpit.base64_decode()</h2>
<pre class="programlisting">
data = cockpit.base64_decode(string, [constructor])
</pre>
<p>Decode binary data from a Base64 encoded string. The <code class="code">string</code>
argument should be a javascript string. The returned <code class="code">data</code>> will be an
array of bytes.</p>
<p>You can pass <code class="code">Uint8Array</code>, <code class="code">Array</code> or <code class="code">String</code>
as an alternate <code class="code">constructor</code> if you want the decoded data in an
alternate form. The default is to return an <code class="code">Array</code>. Note that if you use a
<code class="code">String</code> for the decoded data, then you must guarantee that the data
does not contain bytes that would be invalid for a string.</p>
</div>
</div>
<div class="footer"><hr></div>
</body>
</html>