PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/project/network/init.js

https://gitlab.com/BGCX261/zlatnaspirala2-svn-to-git
JavaScript | 462 lines | 313 code | 97 blank | 52 comment | 48 complexity | c57cf0f2395b563dc735e951eb65d9c2 MD5 | raw file
  1. //////////////////
  2. //make id network
  3. //////////////////
  4. var Net_id = new Array();
  5. var Net_x = new Array();
  6. var Net_y = new Array();
  7. var Net_z = new Array();
  8. var netPlayer_avatar = new Array();
  9. //////////////
  10. var eLog = null,
  11. eKeepAlive = null,
  12. eMessage = null,
  13. eChannelId = null,
  14. eChannelName = null,
  15. eAccessKey = null,
  16. eSecretKey = null,
  17. eIsPrivate = null,
  18. eIsSystem = null,
  19. eKeepAlive = null,
  20. eChannelSel = null;
  21. function log( aString ) {
  22. eLog.innerHTML +=
  23. aString + "<br>";
  24. if( eLog.scrollHeight > eLog.clientHeight ) {
  25. eLog.scrollTop = eLog.scrollHeight - eLog.clientHeight;
  26. }
  27. }
  28. function clearLog() {
  29. eLog.innerHTML = "";
  30. eLog.scrollTop = 0;
  31. }
  32. var lWSC = null;
  33. function login() {
  34. // URL is ws[s]://[hostname|localhost]:8787[/context][/servlet/][;args...]
  35. //var lURL = jws.getDefaultServerURL();
  36. var lURL = "ws://" + SERVER[0].address + ":8787/jWebSocket/jWebSocket";
  37. //var lURL = "ws://188.226.129.11:8787/jWebSocket/jWebSocket";
  38. console.log("IP >>>>" + lURL);
  39. //var lURL = "ws://192.168.1.10:8787/jWebSocket/jWebSocket";
  40. log( "Login to " + lURL + " ...'" );
  41. try {
  42. var lRes = lWSC.logon( lURL, jws.DEMO_ROOT_LOGINNAME, jws.DEMO_ROOT_PASSWORD, {
  43. // OnOpen callback
  44. OnOpen: function( aEvent ) {
  45. log( "jWebSocket connection established." );
  46. jws.$("simgStatus").src = "images/yes.png";
  47. console.log("You are connected");
  48. },
  49. OnWelcome: function(aEvent) {
  50. //getChannels();
  51. setTimeout(function(){auth()},1500);
  52. setTimeout(function(){subscribeChannel()},2400);
  53. setTimeout(function(){publish_FOR_NEW()},3100);
  54. },
  55. // OnMessage callback
  56. OnMessage: function( aEvent ,aToken ) {
  57. // log( "<font style='color:#888'>jWebSocket message received: '" + aEvent.data + "'</font>" );
  58. //////////////////////////////////////////////
  59. if (aToken.type == "event" && aToken.name == "login") {
  60. setTimeout(function(){ publish_FOR_NEW(); } , 3500 );
  61. console.log("LOGOVAO SE >>>>"+aToken.sourceId);
  62. }
  63. if (aToken.type == "event" && aToken.name == "logout") {
  64. if ( aToken.sourceId != lWSC.getId() ) {
  65. console.log("Neko je Otisao >>>>"+aToken.sourceId);
  66. }
  67. }
  68. if (aToken.type == "data"){
  69. if ( aToken.publisher != lWSC.getId() ) { //eliminate your's data only other
  70. // console.log("TIP PODATKA DATA");
  71. var buf = Net_id.indexOf(aToken.publisher);
  72. if (buf == -1) // Multiplayer buffer data
  73. {
  74. if (aToken.publisher != null) {
  75. Net_id.push(aToken.publisher);
  76. Net_x.push(aToken.map.position[0]);
  77. Net_y.push(aToken.map.position[1]);
  78. Net_z.push(aToken.map.position[2]);
  79. if (aToken.map.avatar === "undefined"){ }else{
  80. try{
  81. netPlayer_avatar.push(aToken.map.avatar[0]);
  82. var hand_https = "OK";
  83. hand_https = aToken.map.avatar[0].toString();
  84. console.log(hand_https);
  85. var hand_http=hand_https.replace("https","http");
  86. console.log(hand_http + "from google+");
  87. //getE("netAvatarIMG").src = hand_http;
  88. //var IMG_BASE62 = getE("netAvatarIMG").src;
  89. var img = new Image,
  90. canvas = document.createElement("canvas"),
  91. ctx = canvas.getContext("2d"),
  92. src = hand_http; // insert image url here
  93. //alert(hand_http);
  94. img.crossOrigin = "Anonymous";
  95. img.onload = function() {
  96. canvas.width = img.width;
  97. canvas.height = img.height;
  98. ctx.drawImage( img, 0, 0 );
  99. localStorage.setItem( "PROFILE_AVATAR", canvas.toDataURL("image/png") );
  100. }
  101. img.src = src;
  102. // make sure the load event fires for cached images too
  103. if ( img.complete || img.complete === undefined ) {
  104. img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
  105. img.src = src;
  106. }
  107. //getE("netAvatarIMG").src = getBase64FromImageUrl(IMG_BASE62);
  108. //getE("netAvatarIMG").src = uInfo.image.url;
  109. //CREATE_TEXTURE("T" + Net_id.indexOf(aToken.publisher).toString(), img.src );
  110. CREATE_TEXTURE("T" + Net_id.indexOf(aToken.publisher).toString(), localStorage.getItem( "PROFILE_AVATAR") );
  111. }catch(e){console.log(e + "NEBI TREBALO DA SE DESI");}
  112. }
  113. console.log("PRVI PUT");
  114. }
  115. }
  116. else {//replace new data about network player
  117. Net_x.splice(Net_id.indexOf(aToken.publisher),1, aToken.map.position[0]);
  118. Net_y.splice(Net_id.indexOf(aToken.publisher),1, aToken.map.position[1]);
  119. Net_z.splice(Net_id.indexOf(aToken.publisher),1, aToken.map.position[2]);
  120. if (aToken.map.avatar === "undefined"){ }else{
  121. try{
  122. netPlayer_avatar.splice(Net_id.indexOf(aToken.publisher),1, aToken.map.avatar[0]);
  123. getE("netAvatarIMG").src = aToken.map.avatar[0].toString();
  124. CREATE_TEXTURE("T" + Net_id.indexOf(aToken.publisher).toString(), getE("netAvatarIMG").src );
  125. console.log("USO U REPLACE ZA AVATARA");
  126. } catch(e){console.log("GRESKA2");}
  127. }
  128. }
  129. if (aToken.data != "") {
  130. console.log("USO za CHAT>>>>" + aToken.data + "<><><><<>< event data >>>" + aEvent.data );
  131. var DATA_CHAT = "\n" + "userID:" + aToken.publisher + " message: " + aToken.data;
  132. if (MAIN.browser.name == "android_tab_firefox"){
  133. getE('CHAT_DIV').innerHTML += '<div style="width:97%;height:auto;background-color:yellow;border-radius:14px;color:black;padding-left:10px;padding-top:4px;" >'+DATA_CHAT+'</div>';
  134. var oDiv = document.getElementById("CHAT_DIV");
  135. oDiv.scrollTop = oDiv.scrollHeight;
  136. var o1 = document.getElementById("CHAT_DIV");
  137. o1.style.overflow ="scroll";
  138. }
  139. if (MAIN.browser.name == "desktop_firefox"){
  140. getE('CHAT_DIV').innerHTML += '<div style="width:97%;height:auto;background-color:yellow;border-radius:14px;color:black;padding-left:10px;padding-top:4px;" >'+DATA_CHAT+'</div>';
  141. var oDiv = document.getElementById("CHAT_DIV");
  142. oDiv.scrollTop = oDiv.scrollHeight;
  143. }
  144. if (MAIN.browser.name == "desktop_chrome"){
  145. getE('CHAT_DIV').innerHTML += '<div style="width:97%;height:auto;background-color:yellow;border-radius:14px;color:black;padding-left:10px;padding-top:4px;" >'+DATA_CHAT+'</div>';
  146. var oDiv = document.getElementById("CHAT_DIV");
  147. oDiv.scrollTop = oDiv.scrollHeight;
  148. document.getElementById('CHAT_DIV').scrollTop = 9999999;
  149. }
  150. // console.log("CHAT PODATAK:"+aToken.data+" <> OD <>:"+aToken.publisher); === "undefined"
  151. }
  152. }
  153. }
  154. // detect disconnect if END signal not reseived
  155. if (aToken.type == "event") {
  156. // console.log(aEvent.data + " EVENT <<<" );
  157. var CHECK_ID = Net_id.indexOf(aToken.sourceId);
  158. if (aToken.type == "event" && aToken.name == "logout" && CHECK_ID !== -1 ) {
  159. //console.log("!!!!!!!!!!!!!EVENT END" + aToken.sourceId + ":" + CHECK_ID );
  160. netPlayer_avatar.splice(Net_id.indexOf(aToken.sourceId),1);
  161. Net_x.splice(Net_id.indexOf(aToken.sourceId),1);
  162. Net_y.splice(Net_id.indexOf(aToken.sourceId),1);
  163. Net_z.splice(Net_id.indexOf(aToken.sourceId),1);
  164. Net_id.splice(Net_id.indexOf(aToken.sourceId),1);
  165. }
  166. }
  167. },
  168. // OnClose callback
  169. OnClose: function( aEvent ) {
  170. log( "<font style='color:#888'>jWebSocket connection closed.</font>" );
  171. jws.$("simgStatus").src = "images/disconnected.png";
  172. jws.$("slblClientId").innerHTML = "&nbsp;Client&#x2011;Id:&nbsp;-";
  173. }
  174. });
  175. } catch( ex ) {
  176. log( "Exception: " + ex.message );
  177. }
  178. }
  179. // log out the client from the jWebSocket server
  180. function logout() {
  181. if( lWSC ) {
  182. lWSC.stopKeepAlive();
  183. log( "Disconnecting..." );
  184. var lRes = lWSC.close({ timeout: 3000 });
  185. log( lWSC.resultToString( lRes ) );
  186. }
  187. }
  188. // try to create a new channel on the server
  189. // on success the OnChannelCreated event is fired
  190. function createChannel() {
  191. var lChannelId = eChannelId.value;
  192. var lChannelName = eChannelName.value;
  193. var lIsPrivate = eIsPrivate.checked;
  194. var lIsSystem = eIsSystem.checked;
  195. var lAccessKey = eAccessKey.value;
  196. var lSecretKey = eSecretKey.value;
  197. log( "Creating channel '" + lChannelId + "'..." );
  198. var lRes = lWSC.channelCreate(
  199. lChannelId,
  200. lChannelName,
  201. { isPrivate: lIsPrivate,
  202. isSystem: lIsSystem,
  203. accessKey: lAccessKey,
  204. secretKey: lSecretKey
  205. }
  206. );
  207. log( lWSC.resultToString( lRes ) );
  208. }
  209. // try to remove an existing channel on the server
  210. // on success the OnChannelRemoved event is fired
  211. function removeChannel() {
  212. var lChannelId = eChannelId.value;
  213. var lAccessKey = eAccessKey.value;
  214. var lSecretKey = eSecretKey.value;
  215. log( "Removing channel '" + lChannelId + "'..." );
  216. var lRes = lWSC.channelRemove(
  217. lChannelId,
  218. { accessKey: lAccessKey,
  219. secretKey: lSecretKey
  220. }
  221. );
  222. log( lWSC.resultToString( lRes ) );
  223. }
  224. // try to subscribe at a certain channel
  225. function subscribeChannel() {
  226. var lChannel = "fastmatrix";
  227. var lAccessKey = eAccessKey.value;
  228. log( "Subscribing at channel '" + lChannel + "'..." );
  229. var lRes = lWSC.channelSubscribe( lChannel, lAccessKey );
  230. log( lWSC.resultToString( lRes ) );
  231. }
  232. // try to unsubscribe from a certain channel
  233. function unsubscribeChannel() {
  234. var lChannel = "fastmatrix";
  235. log( "Unsubscribing from channel '" + lChannel + "'..." );
  236. var lRes = lWSC.channelUnsubscribe( lChannel );
  237. log( lWSC.resultToString( lRes ) );
  238. }
  239. // try to authenticate against a channel to publish data
  240. function auth() {
  241. var lChannel = "fastmatrix";
  242. var lAccessKey = eAccessKey.value;
  243. var lSecretKey = eSecretKey.value;
  244. log( "Authenticating against channel '" + lChannel + "'..." );
  245. // use access key and secret key for this channel to authenticate
  246. // required to publish data only
  247. var lRes = lWSC.channelAuth( lChannel, lAccessKey, lSecretKey );
  248. log( lWSC.resultToString( lRes ) );
  249. }
  250. // try to publish data on a certain channel
  251. function publish() {
  252. var lChannel = "fastmatrix";
  253. var lRes = lWSC.channelPublish( lChannel, eMessage.value, {
  254. position: [ transX.toFixed(2) , transY.toFixed(2),transZ.toFixed(2) ]
  255. });
  256. //log( lWSC.resultToString( lRes ) );
  257. }
  258. function publish_FOR_NEW() {
  259. var lChannel = "fastmatrix";
  260. var lRes = lWSC.channelPublish( lChannel, "NewPlayer", {
  261. position: [ transX.toFixed(2) , transY.toFixed(2),transZ.toFixed(2) ] ,
  262. avatar: [getE("PRO").src]
  263. });
  264. //log( lWSC.resultToString( lRes ) );
  265. }
  266. // try to obtain all available channels on the server
  267. function getChannels() {
  268. log( "Trying to obtain channels..." );
  269. var lRes = lWSC.channelGetIds();
  270. log( lWSC.resultToString( lRes ) );
  271. }
  272. // try to obtain all subscribers for a certain channel
  273. function getSubscribers() {
  274. var lChannel = "fastmatrix";
  275. var lAccessKey = eAccessKey.value;
  276. log( "Trying to obtain subscribers for channel '" + lChannel + "'..." );
  277. var lRes = lWSC.channelGetSubscribers(lChannel, lAccessKey);
  278. log( lWSC.resultToString( lRes ) );
  279. }
  280. // try to obtain all channels the client has subscribed to
  281. function getSubscriptions() {
  282. log( "Trying to obtain subscriptions for client..." );
  283. var lRes = lWSC.channelGetSubscriptions();
  284. log( lWSC.resultToString( lRes ) );
  285. }
  286. function toggleKeepAlive() {
  287. if( eKeepAlive.checked ) {
  288. lWSC.startKeepAlive({ interval: 3000 });
  289. } else {
  290. lWSC.stopKeepAlive();
  291. }
  292. }
  293. // this method is called when a new channel has been created on the server
  294. // add the new channel to the drop down box on the client as well
  295. function onChannelCreatedObs( aEvent ) {
  296. var lOption = document.createElement("option");
  297. lOption.value = aEvent.channelId;
  298. lOption.text = aEvent.channelName;
  299. eChannelSel.appendChild(lOption);
  300. }
  301. // this method is called when a channel has been removed from the server
  302. // remove the channel from the drop down box on the client as well
  303. function onChannelRemovedObs( aEvent ) {
  304. for( var lIdx = 0, lCnt = eChannelSel.options.length; lIdx < lCnt; lIdx++ ) {
  305. var lOption = eChannelSel.options[ lIdx ];
  306. if( lOption.value == aEvent.channelId ) {
  307. eChannelSel.removeChild(lOption);
  308. break;
  309. }
  310. }
  311. }
  312. // this method is called when a channel has been removed from the server
  313. // remove the channel from the drop down box on the client as well
  314. function onChannelsReceivedObs( aEvent ) {
  315. // remove all existing channels in drop down box
  316. while( eChannelSel.options.length > 0 ) {
  317. var lOption = eChannelSel.options[ 0 ];
  318. eChannelSel.removeChild(lOption);
  319. }
  320. // add all channels from event
  321. for( var lIdx = 0, lCnt = aEvent.channels.length; lIdx < lCnt; lIdx++ ) {
  322. var lOption = document.createElement("option");
  323. lOption.value = aEvent.channels[ lIdx ].id;
  324. lOption.text = aEvent.channels[ lIdx ].name;
  325. eChannelSel.appendChild(lOption);
  326. }
  327. }
  328. function initPage() {
  329. eLog = jws.$( "sdivChat" );
  330. eMessage = jws.$( "stxfMsg" );
  331. eChannelId = jws.$( "stxfChannelId" );
  332. eChannelName = jws.$( "stxfChannelId" );
  333. eAccessKey = jws.$( "stxfAccessKey" );
  334. eSecretKey = jws.$( "stxfSecretKey" );
  335. eIsPrivate = jws.$( "schkPrivate" );
  336. eIsSystem = jws.$( "schkSystem" );
  337. eKeepAlive = jws.$( "schkKeepAlive" );
  338. eChannelSel = jws.$( "sselChannel" );
  339. if( window.WebSocket ) {
  340. lWSC = new jws.jWebSocketJSONClient();
  341. lWSC.setChannelCallbacks({
  342. OnChannelCreated: onChannelCreatedObs,
  343. OnChannelRemoved: onChannelRemovedObs,
  344. OnChannelsReceived: onChannelsReceivedObs
  345. });
  346. } else {
  347. jws.$( "sbtnConnect" ).setAttribute( "disabled", "disabled" );
  348. jws.$( "sbtnDisconnect" ).setAttribute( "disabled", "disabled" );
  349. jws.$( "sbtnClear" ).setAttribute( "disabled", "disabled" );
  350. jws.$( "sbtnSubscribe" ).setAttribute( "disabled", "disabled" );
  351. jws.$( "sbtnUnsubscribe" ).setAttribute( "disabled", "disabled" );
  352. //jws.$( "schkKeepAlive" ).setAttribute( "disabled", "disabled" );
  353. var lMsg = jws.MSG_WS_NOT_SUPPORTED;
  354. //alert( lMsg );
  355. log( lMsg );
  356. }
  357. }
  358. function exitPage() {
  359. lWSC.stopKeepAlive();
  360. logout();
  361. }
  362. initPage();
  363. setTimeout(function(){
  364. login();
  365. },500);