mirror of
				https://github.com/opennetworkinglab/onos.git
				synced 2025-11-04 02:01:11 +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;
 | 
					    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')
 | 
					    angular.module('onosRemote')
 | 
				
			||||||
    .factory('WebSocketService',
 | 
					    .factory('WebSocketService',
 | 
				
			||||||
            ['$log', '$location', 'UrlFnService', 'FnService',
 | 
					            ['$log', '$location', 'UrlFnService', 'FnService',
 | 
				
			||||||
@ -32,20 +62,29 @@
 | 
				
			|||||||
            // creates a web socket for the given path, returning a "handle".
 | 
					            // creates a web socket for the given path, returning a "handle".
 | 
				
			||||||
            // opts contains the event handler callbacks.
 | 
					            // opts contains the event handler callbacks.
 | 
				
			||||||
            function createWebSocket(path, opts) {
 | 
					            function createWebSocket(path, opts) {
 | 
				
			||||||
                var wsport = opts && opts.wsport,
 | 
					                var o = opts || {},
 | 
				
			||||||
 | 
					                    wsport = opts && opts.wsport,
 | 
				
			||||||
                    fullUrl = ufs.wsUrl(path, wsport),
 | 
					                    fullUrl = ufs.wsUrl(path, wsport),
 | 
				
			||||||
                    ws = new WebSocket(fullUrl),
 | 
					 | 
				
			||||||
                    api = {
 | 
					                    api = {
 | 
				
			||||||
                        meta: { path: fullUrl, ws: ws },
 | 
					                        meta: { path: fullUrl, ws: null },
 | 
				
			||||||
                        send: send,
 | 
					                        send: send,
 | 
				
			||||||
                        close: close
 | 
					                        close: close
 | 
				
			||||||
                    };
 | 
					                    },
 | 
				
			||||||
 | 
					                    ws;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                try {
 | 
				
			||||||
 | 
					                    ws = new WebSocket(fullUrl);
 | 
				
			||||||
 | 
					                    api.meta.ws = ws;
 | 
				
			||||||
 | 
					                } catch (e) {
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                $log.debug('Attempting to open websocket to: ' + fullUrl);
 | 
					                $log.debug('Attempting to open websocket to: ' + fullUrl);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                ws.onopen = (opts && opts.onOpen) || null;
 | 
					                if (ws) {
 | 
				
			||||||
                ws.onmessage = (opts && opts.onMessage) || null;
 | 
					                    ws.onopen = fnOpen(o.onOpen);
 | 
				
			||||||
                ws.onclose = (opts && opts.onClose) || null;
 | 
					                    ws.onmessage = fnMessage(o.onMessage);
 | 
				
			||||||
 | 
					                    ws.onclose = fnClose(o.onClose);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                function send(msg) {
 | 
					                function send(msg) {
 | 
				
			||||||
                    if (msg) {
 | 
					                    if (msg) {
 | 
				
			||||||
@ -62,6 +101,7 @@
 | 
				
			|||||||
                    if (ws) {
 | 
					                    if (ws) {
 | 
				
			||||||
                        ws.close();
 | 
					                        ws.close();
 | 
				
			||||||
                        ws = null;
 | 
					                        ws = null;
 | 
				
			||||||
 | 
					                        api.meta.ws = null;
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -34,7 +34,7 @@
 | 
				
			|||||||
    var ovtopo, svg, defs, zoomLayer, map;
 | 
					    var ovtopo, svg, defs, zoomLayer, map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Internal state
 | 
					    // Internal state
 | 
				
			||||||
    var zoomer;
 | 
					    var zoomer, wsock;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Note: "exported" state should be properties on 'self' variable
 | 
					    // Note: "exported" state should be properties on 'self' variable
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -136,21 +136,20 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function onWsMessage(msg) {
 | 
					    function onWsMessage(ev) {
 | 
				
			||||||
        var ev = JSON.parse(msg.data);
 | 
					        $log.log('got JSON event: ', ev);
 | 
				
			||||||
        $log.log('got event: ', ev);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function onWsClose(msg) {
 | 
					    function onWsClose(closeEvent) {
 | 
				
			||||||
        $log.log('web socket closed...', msg);
 | 
					        $log.log('web socket closed...', closeEvent);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // wsport indicates web-socket-server port other than the default.
 | 
					    // wsport indicates web-socket-server port other than the default.
 | 
				
			||||||
    // Used for testing with the mock-web-socket-server.
 | 
					    // Used for testing with the mock-web-socket-server.
 | 
				
			||||||
    function setUpWebSocket(wsport) {
 | 
					    function setUpWebSocket(wsport) {
 | 
				
			||||||
        var wsHandle = wss.createWebSocket('topology', {
 | 
					        wsock = wss.createWebSocket('topology', {
 | 
				
			||||||
            onOpen: onWsOpen,
 | 
					            onOpen: onWsOpen,
 | 
				
			||||||
            onMessage: onWsMessage,
 | 
					            onMessage: onWsMessage,
 | 
				
			||||||
            onClose: onWsClose,
 | 
					            onClose: onWsClose,
 | 
				
			||||||
@ -161,7 +160,7 @@
 | 
				
			|||||||
        // TODO: implement retry on close functionality
 | 
					        // TODO: implement retry on close functionality
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $log.log('created web socket', wsHandle);
 | 
					        $log.log('created web socket', wsock);
 | 
				
			||||||
        // TODO: complete implementation...
 | 
					        // TODO: complete implementation...
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -50,6 +50,7 @@ describe('factory: fw/remote/websocket.js', function () {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it('should use the appropriate URL', function () {
 | 
					    it('should use the appropriate URL', function () {
 | 
				
			||||||
 | 
					        debugger;
 | 
				
			||||||
        var ws = wss.createWebSocket('foo/path');
 | 
					        var ws = wss.createWebSocket('foo/path');
 | 
				
			||||||
        expect(ws.meta.path).toEqual('ws://foo:80/onos/ui/ws/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.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);
 | 
					        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.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();
 | 
					        expect(ws.meta.ws.onclose).toBeNull();
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user