blob: de7b076bb92049fa774da924e92ba17814c7a02b [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) {
Simon Hunta25cdcd2015-01-22 13:11:42 -080033 //var fullUrl = ufs.wsUrl(path),
34 var fullUrl = 'ws://localhost:8123/foo',
Simon Hunt584122a2015-01-21 15:32:40 -080035 ws = new WebSocket(fullUrl),
36 api = {
37 meta: { path: fullUrl, ws: ws },
38 send: send,
39 close: close
40 };
41
42 ws.onopen = (cb && cb.onOpen) || null;
43 ws.onmessage = (cb && cb.onMessage) || null;
44 ws.onclose = (cb && cb.onClose) || null;
45
46 function send(msg) {
47 if (msg) {
48 if (ws) {
49 ws.send(msg);
50 } else {
51 $log.warn('ws.send() no web socket open!',
52 fullUrl, msg);
53 }
54 }
55 }
56
57 function close() {
58 if (ws) {
59 ws.close();
60 ws = null;
61 }
62 }
63
64 return api;
Simon Hunt1e4a0012015-01-21 11:36:08 -080065 }
66
67 return {
68 createWebSocket: createWebSocket
69 };
70 }]);
71
72}());