Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / fusion / scripts / socket / peerBroadcast.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20
21 var output = $('#peerBroadcastSection');
22 var websocket = '';
23 var currentUser = '';
24 var remoteUser = '';
25 var f = '';
26 var popupService;
27
28 function setPopupService(_popupService) {
29          popupService = _popupService;
30 }
31
32 function socketSetup(initialPageVisit,_currentUser, _remoteUser, _f) {
33         
34
35
36         if( true) {
37                   
38                 var href = window.location.href;
39             var hostPatt = new RegExp(window.location.host +"/[^/]*");
40             var res = hostPatt.exec(href);
41             var protocol = window.location.protocol.replace("http","ws");
42             
43             var signalingServerPath = protocol + "//" + res + "/contact";
44                   var wsUri = signalingServerPath;
45                   console.log("Connecting to " + wsUri);
46                   websocket = new WebSocket(wsUri);
47                   //localStorage.notifySocket = JSON.stringify(websocket);
48                   //window.top.notifySocket = websocket;
49                   
50                   currentUser = _currentUser;
51                   remoteUser = _remoteUser;
52                   f = socketSend;
53                  
54                   
55         }
56         
57         //var websocket = JSON.parse(localStorage.notifySocket || "{}") ;
58         if( websocket != null) {
59                 websocket.onopen = function(evt) { onOpen(evt); };
60                 websocket.onmessage = function(evt) { onMessage(evt); };
61                 websocket.onerror = function(evt) { onError(evt); };
62         
63         }
64         
65         //if(f != undefined)
66          // f();
67         
68         //window.top.notifySocket.send("");
69 }
70
71
72 function socketSend(currentUser, remoteUser) {
73         
74
75
76          websocket.send(JSON.stringify({
77          from: currentUser,
78          to: remoteUser
79      }));
80         
81         //window.top.notifySocket.send("");
82 }
83
84
85
86
87 function onOpen(evt) {
88     console.log("onOpen");
89     //writeToScreen("CONNECTED");
90     
91     if(f != undefined)
92                 f(currentUser, remoteUser);
93     
94 }
95
96 function onMessage(evt) {
97     if (typeof evt.data == "string") {
98         writeToScreen("RECEIVED (text): " + evt.data);
99         
100         if(popupService != undefined) {
101                 popupService.showMessage("Call Request","You are being contacted by " + JSON.parse(evt.data).from + ". Please click on the online icon and then click view");
102                 
103         }
104         //if (confirm("You are being contacted by " + JSON.parse(evt.data).from )) {
105            
106             //}
107         
108     } else {
109         writeToScreen("RECEIVED (binary): " + evt.data);
110     }
111 }
112
113 function onError(evt) {
114     writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
115 }
116
117 function writeToScreen(message) {
118     var pre = document.createElement("p");
119     pre.style.wordWrap = "break-word";
120     pre.innerHTML = message;
121     output.append(pre);
122 }