1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 function WebSocketListener(closure
, ws
, sentMsg
) {
8 this._closure
= closure
;
10 this._sentMsg
= sentMsg
;
13 WebSocketListener
.prototype = {
18 QueryInterface
: ChromeUtils
.generateQI(["nsIWebSocketListener"]),
21 onBinaryMessageAvailable(aContext
, aMsg
) {
22 info("WsListener::onBinaryMessageAvailable");
23 this._received
= aMsg
;
24 this._ws
.close(0, null);
26 onMessageAvailable() {},
28 onWebSocketListenerStart() {},
30 this._ws
.sendMsg(this._sentMsg
);
32 onStop(aContext
, aStatusCode
) {
34 this._closure(aStatusCode
, this._received
);
37 do_throw("Error in closure function: " + ex
);
42 function makeWebSocketChan() {
43 let chan
= Cc
["@mozilla.org/network/protocol;1?name=wss"].createInstance(
44 Ci
.nsIWebSocketChannel
48 Services
.scriptSecurityManager
.getSystemPrincipal(),
49 null, // aTriggeringPrincipal
50 Ci
.nsILoadInfo
.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL
,
51 Ci
.nsIContentPolicy
.TYPE_WEBSOCKET
56 function openWebSocketChannelPromise(chan
, url
, msg
) {
57 let uri
= Services
.io
.newURI(url
);
58 return new Promise(resolve
=> {
59 function finish(status
, result
) {
60 resolve([status
, result
]);
67 new WebSocketListener(finish
, chan
, msg
),