Skip to content

Commit 284c177

Browse files
committed
HTTP: replacing wsConnect with wsClient (to be able to close the client connection)
1 parent b2fc924 commit 284c177

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

src/openaf/plugins/HTTP.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,39 @@ public Object wsConnect(String anURL, NativeFunction onConnect, NativeFunction o
367367
return WebSockets.wsConnect(authenticator, l, p, anURL, onConnect, onMsg, onError, onClose, aTimeout, supportSelfSigned);
368368
}
369369

370+
/**
371+
* <odoc>
372+
* <key>HTTP.wsClient(anURL, onConnect, onMsg, onError, onClose, aTimeout, supportSelfSigned) : WebSocketsReply</key>
373+
* Tries to establish a websocket connection (ws or wss) and returns a jetty WebSocketClient java object.
374+
* As callbacks you should defined onConnect, onMsg, onError and onClose. The onConnect callback will
375+
* provide, as argument, the created session that you should use to send data; the onMsg callback will
376+
* provide, as arguments, aType (either "text" or "bytes"), aPayload (string or array of bytes) and an offset
377+
* and length (in case type is "bytes"); the onError callback will provide the cause; the onClose callback
378+
* will provide aStatusCode and aReason. You can optionally provide aTimeout (number) and indicate if self signed SSL
379+
* certificates should be accepted (supportSelfSigned = true). Example:\
380+
* \
381+
* plugin("HTTP");\
382+
* var session; var output = "";\
383+
* var res = (new HTTP()).wsClient("ws://echo.websocket.org",\
384+
* function(aSession) { log("Connected"); session = aSession; },\
385+
* function(aType, aPayload, aOffset, aLength) { if (aType == "text") output += aPayload; },\
386+
* function(aCause) { logErr(aCause); },\
387+
* function(aStatusCode, aReason) { log("Closed (" + aReason + ")"); }\
388+
* );\
389+
* session.getRemote().sendString("Hello World!");\
390+
* while(output.length &lt; 1) { sleep(100); };\
391+
* res.client.stop();\
392+
* print(output);\
393+
* \
394+
* NOTE: this functionality is only available if used with JVM >= 1.8\
395+
* \
396+
* </odoc>
397+
*/
398+
@JSFunction
399+
public Object wsClient(String anURL, NativeFunction onConnect, NativeFunction onMsg, NativeFunction onError, NativeFunction onClose, Object aTimeout, boolean supportSelfSigned) throws Exception {
400+
return WebSockets.wsClient(authenticator, l, p, anURL, onConnect, onMsg, onError, onClose, aTimeout, supportSelfSigned);
401+
}
402+
370403
/**
371404
*
372405
* @param aURL

src/openaf/plugins/HTTPws/WebSockets.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,23 @@
1515

1616
/**
1717
* HTTP plugin websockets extension
18-
* @author Nuno Aguiar <[email protected]>
18+
* @author Nuno Aguiar
1919
*/
2020

2121
public class WebSockets {
22+
static public class WebSocketsReply {
23+
public org.eclipse.jetty.websocket.client.WebSocketClient client;
24+
public Future<org.eclipse.jetty.websocket.api.Session> fut;
25+
}
26+
2227
static public Object wsConnect(Authenticator authenticator, String u, String p, String anURL, NativeFunction onConnect, NativeFunction onMsg, NativeFunction onError,
2328
NativeFunction onClose, Object aTimeout, boolean supportSelfSigned) throws Exception {
29+
Object res = wsClient(authenticator, u, p, anURL, onConnect, onMsg, onError, onClose, aTimeout, supportSelfSigned);
30+
return ((WebSocketsReply) res).fut;
31+
}
32+
33+
static public Object wsClient(Authenticator authenticator, String u, String p, String anURL, NativeFunction onConnect, NativeFunction onMsg, NativeFunction onError,
34+
NativeFunction onClose, Object aTimeout, boolean supportSelfSigned) throws Exception {
2435

2536
class EventSocket extends org.eclipse.jetty.websocket.api.WebSocketAdapter {
2637
NativeFunction onConnect, onMsg, onError, onClose;
@@ -161,7 +172,11 @@ public void onWebSocketError(Throwable cause) {
161172
else
162173
session = fut.get();
163174

164-
return fut;
175+
WebSocketsReply res = new WebSocketsReply();
176+
res.fut = fut;
177+
res.client = client;
178+
179+
return res;
165180
} catch (Exception e) {
166181
client.stop();
167182
throw e;

tests/autoTestAll.HTTP.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@
3939
exports.testHTTPWSClient = function() {
4040
plugin("HTTP");
4141
var session; var output = "";
42-
var client = (new HTTP()).wsConnect("ws://echo.websocket.org",
42+
var res = (new HTTP()).wsClient("ws://echo.websocket.org",
4343
function(aSession) { session = aSession; },
4444
function(aType, aPayload, aOffset, aLength) { if (aType == "text") output += aPayload; },
4545
function(aCause) { },
4646
function(aStatusCode, aReason) { });
4747
session.getRemote().sendString("Hello World!");
48-
while(output.length < 1) { sleep(100); };
48+
while(output.length < 1) { res.fut.get(); sleep(100); };
4949
session.stop();
50+
res.client.stop();
5051

5152
ow.test.assert(output, "Hello World!", "Problem with testing websockets against echo.websocket.org");
5253
};

0 commit comments

Comments
 (0)