GUI -- WebSocket object now decodes message payload as JSON on behalf of the consumer.
Change-Id: If27679b2c4d3beaed6aee96233ca4856b673ab72
Showing
3 changed files
with
59 additions
and
17 deletions
... | @@ -22,6 +22,36 @@ | ... | @@ -22,6 +22,36 @@ |
22 | 22 | ||
23 | var fs; | 23 | var fs; |
24 | 24 | ||
25 | + function fnOpen(f) { | ||
26 | + return fs.isF(f); | ||
27 | + } | ||
28 | + | ||
29 | + function fnMessage(f) { | ||
30 | + // wrap the onMessage function; we will attempt to decode the | ||
31 | + // message event payload as JSON and pass that in... | ||
32 | + var fn = fs.isF(f); | ||
33 | + if (!fn) { | ||
34 | + return null; | ||
35 | + } | ||
36 | + | ||
37 | + return function (msgEvent) { | ||
38 | + var ev; | ||
39 | + try { | ||
40 | + ev = JSON.parse(msgEvent.data); | ||
41 | + } catch (e) { | ||
42 | + ev = { | ||
43 | + error: 'Failed to parse JSON', | ||
44 | + e: e | ||
45 | + }; | ||
46 | + } | ||
47 | + fn(ev); | ||
48 | + } | ||
49 | + } | ||
50 | + | ||
51 | + function fnClose(f) { | ||
52 | + return fs.isF(f); | ||
53 | + } | ||
54 | + | ||
25 | angular.module('onosRemote') | 55 | angular.module('onosRemote') |
26 | .factory('WebSocketService', | 56 | .factory('WebSocketService', |
27 | ['$log', '$location', 'UrlFnService', 'FnService', | 57 | ['$log', '$location', 'UrlFnService', 'FnService', |
... | @@ -32,20 +62,29 @@ | ... | @@ -32,20 +62,29 @@ |
32 | // creates a web socket for the given path, returning a "handle". | 62 | // creates a web socket for the given path, returning a "handle". |
33 | // opts contains the event handler callbacks. | 63 | // opts contains the event handler callbacks. |
34 | function createWebSocket(path, opts) { | 64 | function createWebSocket(path, opts) { |
35 | - var wsport = opts && opts.wsport, | 65 | + var o = opts || {}, |
66 | + wsport = opts && opts.wsport, | ||
36 | fullUrl = ufs.wsUrl(path, wsport), | 67 | fullUrl = ufs.wsUrl(path, wsport), |
37 | - ws = new WebSocket(fullUrl), | ||
38 | api = { | 68 | api = { |
39 | - meta: { path: fullUrl, ws: ws }, | 69 | + meta: { path: fullUrl, ws: null }, |
40 | send: send, | 70 | send: send, |
41 | close: close | 71 | close: close |
42 | - }; | 72 | + }, |
73 | + ws; | ||
74 | + | ||
75 | + try { | ||
76 | + ws = new WebSocket(fullUrl); | ||
77 | + api.meta.ws = ws; | ||
78 | + } catch (e) { | ||
79 | + } | ||
43 | 80 | ||
44 | $log.debug('Attempting to open websocket to: ' + fullUrl); | 81 | $log.debug('Attempting to open websocket to: ' + fullUrl); |
45 | 82 | ||
46 | - ws.onopen = (opts && opts.onOpen) || null; | 83 | + if (ws) { |
47 | - ws.onmessage = (opts && opts.onMessage) || null; | 84 | + ws.onopen = fnOpen(o.onOpen); |
48 | - ws.onclose = (opts && opts.onClose) || null; | 85 | + ws.onmessage = fnMessage(o.onMessage); |
86 | + ws.onclose = fnClose(o.onClose); | ||
87 | + } | ||
49 | 88 | ||
50 | function send(msg) { | 89 | function send(msg) { |
51 | if (msg) { | 90 | if (msg) { |
... | @@ -62,6 +101,7 @@ | ... | @@ -62,6 +101,7 @@ |
62 | if (ws) { | 101 | if (ws) { |
63 | ws.close(); | 102 | ws.close(); |
64 | ws = null; | 103 | ws = null; |
104 | + api.meta.ws = null; | ||
65 | } | 105 | } |
66 | } | 106 | } |
67 | 107 | ... | ... |
... | @@ -34,7 +34,7 @@ | ... | @@ -34,7 +34,7 @@ |
34 | var ovtopo, svg, defs, zoomLayer, map; | 34 | var ovtopo, svg, defs, zoomLayer, map; |
35 | 35 | ||
36 | // Internal state | 36 | // Internal state |
37 | - var zoomer; | 37 | + var zoomer, wsock; |
38 | 38 | ||
39 | // Note: "exported" state should be properties on 'self' variable | 39 | // Note: "exported" state should be properties on 'self' variable |
40 | 40 | ||
... | @@ -136,21 +136,20 @@ | ... | @@ -136,21 +136,20 @@ |
136 | 136 | ||
137 | } | 137 | } |
138 | 138 | ||
139 | - function onWsMessage(msg) { | 139 | + function onWsMessage(ev) { |
140 | - var ev = JSON.parse(msg.data); | 140 | + $log.log('got JSON event: ', ev); |
141 | - $log.log('got event: ', ev); | ||
142 | 141 | ||
143 | } | 142 | } |
144 | 143 | ||
145 | - function onWsClose(msg) { | 144 | + function onWsClose(closeEvent) { |
146 | - $log.log('web socket closed...', msg); | 145 | + $log.log('web socket closed...', closeEvent); |
147 | 146 | ||
148 | } | 147 | } |
149 | 148 | ||
150 | // wsport indicates web-socket-server port other than the default. | 149 | // wsport indicates web-socket-server port other than the default. |
151 | // Used for testing with the mock-web-socket-server. | 150 | // Used for testing with the mock-web-socket-server. |
152 | function setUpWebSocket(wsport) { | 151 | function setUpWebSocket(wsport) { |
153 | - var wsHandle = wss.createWebSocket('topology', { | 152 | + wsock = wss.createWebSocket('topology', { |
154 | onOpen: onWsOpen, | 153 | onOpen: onWsOpen, |
155 | onMessage: onWsMessage, | 154 | onMessage: onWsMessage, |
156 | onClose: onWsClose, | 155 | onClose: onWsClose, |
... | @@ -161,7 +160,7 @@ | ... | @@ -161,7 +160,7 @@ |
161 | // TODO: implement retry on close functionality | 160 | // TODO: implement retry on close functionality |
162 | 161 | ||
163 | 162 | ||
164 | - $log.log('created web socket', wsHandle); | 163 | + $log.log('created web socket', wsock); |
165 | // TODO: complete implementation... | 164 | // TODO: complete implementation... |
166 | 165 | ||
167 | } | 166 | } | ... | ... |
... | @@ -50,6 +50,7 @@ describe('factory: fw/remote/websocket.js', function () { | ... | @@ -50,6 +50,7 @@ describe('factory: fw/remote/websocket.js', function () { |
50 | }); | 50 | }); |
51 | 51 | ||
52 | it('should use the appropriate URL', function () { | 52 | it('should use the appropriate URL', function () { |
53 | + debugger; | ||
53 | var ws = wss.createWebSocket('foo/path'); | 54 | var ws = wss.createWebSocket('foo/path'); |
54 | expect(ws.meta.path).toEqual('ws://foo:80/onos/ui/ws/foo/path'); | 55 | expect(ws.meta.path).toEqual('ws://foo:80/onos/ui/ws/foo/path'); |
55 | }); | 56 | }); |
... | @@ -66,7 +67,8 @@ describe('factory: fw/remote/websocket.js', function () { | ... | @@ -66,7 +67,8 @@ describe('factory: fw/remote/websocket.js', function () { |
66 | }); | 67 | }); |
67 | 68 | ||
68 | expect(ws.meta.ws.onopen).toBe(oo); | 69 | expect(ws.meta.ws.onopen).toBe(oo); |
69 | - expect(ws.meta.ws.onmessage).toBe(om); | 70 | + // TODO: om is wrapped - we can't test by reference |
71 | + //expect(ws.meta.ws.onmessage).toBe(om); | ||
70 | expect(ws.meta.ws.onclose).toBe(oc); | 72 | expect(ws.meta.ws.onclose).toBe(oc); |
71 | }); | 73 | }); |
72 | 74 | ||
... | @@ -80,7 +82,8 @@ describe('factory: fw/remote/websocket.js', function () { | ... | @@ -80,7 +82,8 @@ describe('factory: fw/remote/websocket.js', function () { |
80 | }); | 82 | }); |
81 | 83 | ||
82 | expect(ws.meta.ws.onopen).toBe(oo); | 84 | expect(ws.meta.ws.onopen).toBe(oo); |
83 | - expect(ws.meta.ws.onmessage).toBe(om); | 85 | + // TODO: om is wrapped - we can't test by reference |
86 | + //expect(ws.meta.ws.onmessage).toBe(om); | ||
84 | expect(ws.meta.ws.onclose).toBeNull(); | 87 | expect(ws.meta.ws.onclose).toBeNull(); |
85 | }); | 88 | }); |
86 | 89 | ... | ... |
-
Please register or login to post a comment