blob: fb69cf97926ef1cc5682ffcaec980c36246b05cb [file] [log] [blame]
Simon Hunt1e4a0012015-01-21 11:36:08 -08001/*
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 Hunt584122a2015-01-21 15:32:40 -080023 var fs;
Simon Hunt1e4a0012015-01-21 11:36:08 -080024
Simon Hunt584122a2015-01-21 15:32:40 -080025 angular.module('onosRemote')
26 .factory('WebSocketService', ['$location', 'UrlFnService', 'FnService',
27 function ($loc, ufs, _fs_) {
28 fs = _fs_;
29
30 // creates a web socket for the given path, returning a "handle".
31 // cb is the callbacks block.
32 function createWebSocket(path, cb) {
33 var fullUrl = ufs.wsUrl(path),
34 ws = new WebSocket(fullUrl),
35 api = {
36 meta: { path: fullUrl, ws: ws },
37 send: send,
38 close: close
39 };
40
41 ws.onopen = (cb && cb.onOpen) || null;
42 ws.onmessage = (cb && cb.onMessage) || null;
43 ws.onclose = (cb && cb.onClose) || null;
44
45 function send(msg) {
46 if (msg) {
47 if (ws) {
48 ws.send(msg);
49 } else {
50 $log.warn('ws.send() no web socket open!',
51 fullUrl, msg);
52 }
53 }
54 }
55
56 function close() {
57 if (ws) {
58 ws.close();
59 ws = null;
60 }
61 }
62
63 return api;
Simon Hunt1e4a0012015-01-21 11:36:08 -080064 }
65
66 return {
67 createWebSocket: createWebSocket
68 };
69 }]);
70
71}());