mirror of
https://github.com/opennetworkinglab/onos.git
synced 2025-11-02 17:21:05 +01:00
GUI -- WebSocket object now decodes message payload as JSON on behalf of the consumer.
Change-Id: If27679b2c4d3beaed6aee96233ca4856b673ab72
This commit is contained in:
parent
bb920fdd5a
commit
970e7fda20
@ -22,6 +22,36 @@
|
||||
|
||||
var fs;
|
||||
|
||||
function fnOpen(f) {
|
||||
return fs.isF(f);
|
||||
}
|
||||
|
||||
function fnMessage(f) {
|
||||
// wrap the onMessage function; we will attempt to decode the
|
||||
// message event payload as JSON and pass that in...
|
||||
var fn = fs.isF(f);
|
||||
if (!fn) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return function (msgEvent) {
|
||||
var ev;
|
||||
try {
|
||||
ev = JSON.parse(msgEvent.data);
|
||||
} catch (e) {
|
||||
ev = {
|
||||
error: 'Failed to parse JSON',
|
||||
e: e
|
||||
};
|
||||
}
|
||||
fn(ev);
|
||||
}
|
||||
}
|
||||
|
||||
function fnClose(f) {
|
||||
return fs.isF(f);
|
||||
}
|
||||
|
||||
angular.module('onosRemote')
|
||||
.factory('WebSocketService',
|
||||
['$log', '$location', 'UrlFnService', 'FnService',
|
||||
@ -32,20 +62,29 @@
|
||||
// creates a web socket for the given path, returning a "handle".
|
||||
// opts contains the event handler callbacks.
|
||||
function createWebSocket(path, opts) {
|
||||
var wsport = opts && opts.wsport,
|
||||
var o = opts || {},
|
||||
wsport = opts && opts.wsport,
|
||||
fullUrl = ufs.wsUrl(path, wsport),
|
||||
ws = new WebSocket(fullUrl),
|
||||
api = {
|
||||
meta: { path: fullUrl, ws: ws },
|
||||
meta: { path: fullUrl, ws: null },
|
||||
send: send,
|
||||
close: close
|
||||
};
|
||||
},
|
||||
ws;
|
||||
|
||||
try {
|
||||
ws = new WebSocket(fullUrl);
|
||||
api.meta.ws = ws;
|
||||
} catch (e) {
|
||||
}
|
||||
|
||||
$log.debug('Attempting to open websocket to: ' + fullUrl);
|
||||
|
||||
ws.onopen = (opts && opts.onOpen) || null;
|
||||
ws.onmessage = (opts && opts.onMessage) || null;
|
||||
ws.onclose = (opts && opts.onClose) || null;
|
||||
if (ws) {
|
||||
ws.onopen = fnOpen(o.onOpen);
|
||||
ws.onmessage = fnMessage(o.onMessage);
|
||||
ws.onclose = fnClose(o.onClose);
|
||||
}
|
||||
|
||||
function send(msg) {
|
||||
if (msg) {
|
||||
@ -62,6 +101,7 @@
|
||||
if (ws) {
|
||||
ws.close();
|
||||
ws = null;
|
||||
api.meta.ws = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -34,7 +34,7 @@
|
||||
var ovtopo, svg, defs, zoomLayer, map;
|
||||
|
||||
// Internal state
|
||||
var zoomer;
|
||||
var zoomer, wsock;
|
||||
|
||||
// Note: "exported" state should be properties on 'self' variable
|
||||
|
||||
@ -136,21 +136,20 @@
|
||||
|
||||
}
|
||||
|
||||
function onWsMessage(msg) {
|
||||
var ev = JSON.parse(msg.data);
|
||||
$log.log('got event: ', ev);
|
||||
function onWsMessage(ev) {
|
||||
$log.log('got JSON event: ', ev);
|
||||
|
||||
}
|
||||
|
||||
function onWsClose(msg) {
|
||||
$log.log('web socket closed...', msg);
|
||||
function onWsClose(closeEvent) {
|
||||
$log.log('web socket closed...', closeEvent);
|
||||
|
||||
}
|
||||
|
||||
// wsport indicates web-socket-server port other than the default.
|
||||
// Used for testing with the mock-web-socket-server.
|
||||
function setUpWebSocket(wsport) {
|
||||
var wsHandle = wss.createWebSocket('topology', {
|
||||
wsock = wss.createWebSocket('topology', {
|
||||
onOpen: onWsOpen,
|
||||
onMessage: onWsMessage,
|
||||
onClose: onWsClose,
|
||||
@ -161,7 +160,7 @@
|
||||
// TODO: implement retry on close functionality
|
||||
|
||||
|
||||
$log.log('created web socket', wsHandle);
|
||||
$log.log('created web socket', wsock);
|
||||
// TODO: complete implementation...
|
||||
|
||||
}
|
||||
|
||||
@ -50,6 +50,7 @@ describe('factory: fw/remote/websocket.js', function () {
|
||||
});
|
||||
|
||||
it('should use the appropriate URL', function () {
|
||||
debugger;
|
||||
var ws = wss.createWebSocket('foo/path');
|
||||
expect(ws.meta.path).toEqual('ws://foo:80/onos/ui/ws/foo/path');
|
||||
});
|
||||
@ -66,7 +67,8 @@ describe('factory: fw/remote/websocket.js', function () {
|
||||
});
|
||||
|
||||
expect(ws.meta.ws.onopen).toBe(oo);
|
||||
expect(ws.meta.ws.onmessage).toBe(om);
|
||||
// TODO: om is wrapped - we can't test by reference
|
||||
//expect(ws.meta.ws.onmessage).toBe(om);
|
||||
expect(ws.meta.ws.onclose).toBe(oc);
|
||||
});
|
||||
|
||||
@ -80,7 +82,8 @@ describe('factory: fw/remote/websocket.js', function () {
|
||||
});
|
||||
|
||||
expect(ws.meta.ws.onopen).toBe(oo);
|
||||
expect(ws.meta.ws.onmessage).toBe(om);
|
||||
// TODO: om is wrapped - we can't test by reference
|
||||
//expect(ws.meta.ws.onmessage).toBe(om);
|
||||
expect(ws.meta.ws.onclose).toBeNull();
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user