Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | ONOS GUI -- Remote -- Web Socket Service |
| 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 23 | var fs; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 24 | |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 25 | angular.module('onosRemote') |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame^] | 26 | .factory('WebSocketService', |
| 27 | ['$log', '$location', 'UrlFnService', 'FnService', |
| 28 | |
| 29 | function ($log, $loc, ufs, _fs_) { |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 30 | fs = _fs_; |
| 31 | |
| 32 | // creates a web socket for the given path, returning a "handle". |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame^] | 33 | // opts contains the event handler callbacks. |
| 34 | function createWebSocket(path, opts) { |
| 35 | var wsport = opts && opts.wsport, |
| 36 | fullUrl = ufs.wsUrl(path, wsport), |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 37 | ws = new WebSocket(fullUrl), |
| 38 | api = { |
| 39 | meta: { path: fullUrl, ws: ws }, |
| 40 | send: send, |
| 41 | close: close |
| 42 | }; |
| 43 | |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame^] | 44 | $log.debug('Attempting to open websocket to: ' + fullUrl); |
| 45 | |
| 46 | ws.onopen = (opts && opts.onOpen) || null; |
| 47 | ws.onmessage = (opts && opts.onMessage) || null; |
| 48 | ws.onclose = (opts && opts.onClose) || null; |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 49 | |
| 50 | function send(msg) { |
| 51 | if (msg) { |
| 52 | if (ws) { |
| 53 | ws.send(msg); |
| 54 | } else { |
| 55 | $log.warn('ws.send() no web socket open!', |
| 56 | fullUrl, msg); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | function close() { |
| 62 | if (ws) { |
| 63 | ws.close(); |
| 64 | ws = null; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return api; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | return { |
| 72 | createWebSocket: createWebSocket |
| 73 | }; |
| 74 | }]); |
| 75 | |
| 76 | }()); |