2 * Copyright © 2018 European Support Limited
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
8 * http: //www.apache.org/licenses/LICENSE-2.0
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.
17 import store from 'sdc-app/AppStore.js';
18 import Configuration from 'sdc-app/config/Configuration.js';
19 import { actionTypes } from 'sdc-app/onboarding/userNotifications/UserNotificationsConstants.js';
20 import WorkerUtil from 'nfvo-utils/WorkerUtil';
22 export const websocketUrl =
23 Configuration.get('websocketProtocol') +
25 window.location.hostname +
27 Configuration.get('websocketPort') +
29 Configuration.get('websocketPath');
32 * Websocket is treated like a singleton. only need one for the application.
35 const workerUtil = new WorkerUtil();
37 open(url, { lastScanned }) {
39 websocket === undefined ||
40 websocket.readyState === websocket.CLOSED
42 websocket = new WebSocket(
43 `${url}?LAST_DELIVERED_EVENT_ID=${lastScanned}`
45 websocket.onmessage = event =>
47 type: actionTypes.NOTIFICATION,
48 data: JSON.parse(event.data)
50 websocket.onclose = event => {
51 if (event.code && event.code === 1001) {
53 const { lastScanned } = store.getState().notifications;
54 console.log('Reconnecting to Websocket');
55 this.open(websocketUrl, { lastScanned });
58 websocket.onerror = () => {
65 if (websocket !== undefined) {