blob: bdd1e3c4cbbd5a35294f424bfa6fc5f11a9f34e7 [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')
Simon Huntbb920fd2015-01-22 17:06:32 -080026 .factory('WebSocketService',
27 ['$log', '$location', 'UrlFnService', 'FnService',
28
29 function ($log, $loc, ufs, _fs_) {
Simon Hunt584122a2015-01-21 15:32:40 -080030 fs = _fs_;
31
32 // creates a web socket for the given path, returning a "handle".
Simon Huntbb920fd2015-01-22 17:06:32 -080033 // opts contains the event handler callbacks.
34 function createWebSocket(path, opts) {
35 var wsport = opts && opts.wsport,
36 fullUrl = ufs.wsUrl(path, wsport),
Simon Hunt584122a2015-01-21 15:32:40 -080037 ws = new WebSocket(fullUrl),
38 api = {
39 meta: { path: fullUrl, ws: ws },
40 send: send,
41 close: close
42 };
43
Simon Huntbb920fd2015-01-22 17:06:32 -080044 $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 Hunt584122a2015-01-21 15:32:40 -080049
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 Hunt1e4a0012015-01-21 11:36:08 -080069 }
70
71 return {
72 createWebSocket: createWebSocket
73 };
74 }]);
75
76}());