EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
relay.js
Go to the documentation of this file.
1/**
2* @fileoverview Dynamic interface to MeshCentral2
3* @author Ylian Saint-Hilaire
4* @version v0.0.1
5*/
6
7var createMeshConnection = function (connectionId) {
8 var obj = {};
9 obj.connectionId = connectionId;
10 obj.state = 0;
11 obj.websocket = null;
12 obj.onStateChanged = null;
13 obj.onData = null;
14
15 obj.connect = function () {
16 if (obj.state == 0) {
17 obj.websocket = new WebSocket(window.location.protocol.replace('http', 'ws') + '//' + window.location.host + '/meshrelay.ashx?id=' + obj.connectionId);
18 obj.websocket.binaryType = "arraybuffer";
19 obj.websocket.onopen = function (e) { console.log('WebSocket Connected', e); };
20 obj.websocket.onmessage = function (e) {
21 console.log('WebSocket Message', e);
22 if ((obj.state = 1) && (e.data == 'c')) {
23 obj.state = 2;
24 if (obj.onStateChanged) { onStateChanged(obj, 2); }
25 console.log('WebSocket Peer Connection', e);
26 obj.send('bob');
27 } else {
28 if (obj.onData != null) { obj.onData(obj, e.data); }
29 }
30 };
31 obj.websocket.onclose = function (e) {
32 console.log('WebSocket Closed', e);
33 obj.state = 0;
34 if (obj.onStateChanged) { onStateChanged(obj, 0); }
35 };
36 obj.websocket.onerror = function (e) { console.log('WebSocket Error', e); };
37 obj.state = 1;
38 if (obj.onStateChanged) { onStateChanged(obj, 1); }
39 }
40 return obj;
41 };
42
43 obj.send = function (data) {
44 if ((obj.state == 2) && (obj.websocket != null)) { obj.websocket.send(data); }
45 };
46
47 return obj;
48}