|
1 /* |
|
2 * This file is part of the TraKERS\Front JS package. |
|
3 * |
|
4 * (c) IRI <http://www.iri.centrepompidou.fr/> |
|
5 * |
|
6 * For the full copyright and license information, please view the LICENSE |
|
7 * file that was distributed with this source code. |
|
8 */ |
|
9 |
|
10 /* |
|
11 * Projet : TraKERS |
|
12 * Module : Front JS |
|
13 * Fichier : index.html |
|
14 * |
|
15 * Auteur : alexandre.bastien@iri.centrepompidou.fr |
|
16 * |
|
17 * Fonctionnalités : Définit le module de communication qui établit la connexion entre ce Front et le Middleware. |
|
18 */ |
|
19 |
|
20 /* |
|
21 * Un client est défini par une socket, qui permettra de recevoir les données du server websocket du Middleware. |
|
22 * On accède aussi à la classe des pointeurs. |
|
23 */ |
|
24 function client(host, port, _pointers) |
|
25 { |
|
26 this.socket; |
|
27 this.pointers = _pointers; |
|
28 |
|
29 this.pointerLeft = false; |
|
30 this.pointerRight = false; |
|
31 this.timePointers = 100; |
|
32 this.pointerLeftTimeout; |
|
33 this.pointerRightTimeout; |
|
34 this.isLeftHanded = false; |
|
35 |
|
36 this.createWebSocket('ws://' + host + ':' + port + '/'); |
|
37 } |
|
38 |
|
39 /* |
|
40 * Création et Initialisation des sockets et listeners permettant d'écouter le server. |
|
41 */ |
|
42 client.prototype.createWebSocket = function(host) |
|
43 { |
|
44 var _this = this; |
|
45 |
|
46 //Initialise la fonctionnalité websocket du naviguateur. |
|
47 if(window.MozWebSocket) |
|
48 { |
|
49 window.WebSocket = window.MozWebSocket; |
|
50 } |
|
51 |
|
52 //Si le naviguateur ne supporte pas les websockets, on s'arrête là. |
|
53 if(!window.WebSocket) |
|
54 { |
|
55 alert('Votre navigateur ne supporte pas les webSocket!'); |
|
56 return false; |
|
57 } |
|
58 //Sinon |
|
59 else |
|
60 { |
|
61 //On initialise la socket. |
|
62 this.socket = new WebSocket(host); |
|
63 |
|
64 //Si elle est ouverte, on notifie le Front. |
|
65 this.socket.onopen = function() |
|
66 { |
|
67 console.log('Socket ouverte.'); |
|
68 } |
|
69 //Si elle est fermée, on notifie le Front. |
|
70 this.socket.onclose = function() |
|
71 { |
|
72 console.log('Socket fermée.'); |
|
73 } |
|
74 //S'il y a un problème dans la connection, on notifie le Front. |
|
75 this.socket.onerror = function() |
|
76 { |
|
77 console.log('Une erreur est survenue dans le module de Communication.'); |
|
78 } |
|
79 //Si on reçoit un message. |
|
80 this.socket.onmessage = function(msg) |
|
81 { |
|
82 _this.processMsg(msg); |
|
83 } |
|
84 } |
|
85 } |
|
86 |
|
87 /* |
|
88 * Traite un message reçu du Middleware. |
|
89 */ |
|
90 client.prototype.processMsg = function(msg) |
|
91 { |
|
92 if(typeof msg === 'undefined' || typeof msg.data === 'undefined') |
|
93 { |
|
94 return; |
|
95 } |
|
96 |
|
97 var _this = this; |
|
98 |
|
99 //Sinon si ce sont les coordonnées de la main droite. |
|
100 if(msg.data[0] == '1') |
|
101 { |
|
102 var instruction = msg.data.substring(2, msg.data.length); |
|
103 pt = instruction.split(';'); |
|
104 var x = Math.abs(parseFloat(pt[0])), y = Math.abs(parseFloat(pt[1])); |
|
105 this.pointerLeft = true; |
|
106 clearTimeout(this.pointerLeftTimeout); |
|
107 |
|
108 this.pointers.refreshMainPointer(x, y); |
|
109 |
|
110 if(!this.pointers.isMainPointerDisplayed) |
|
111 { |
|
112 this.pointers.mainPointerDisplay(true); |
|
113 this.pointers.isMainPointerDisplayed = true; |
|
114 } |
|
115 |
|
116 this.pointerLeftTimeout = setTimeout(function() |
|
117 { |
|
118 _this.pointerLeft = false; |
|
119 |
|
120 if(_this.pointers.isMainPointerDisplayed) |
|
121 { |
|
122 _this.pointers.isMainPointerDisplayed = false; |
|
123 _this.pointers.mainPointerDisplay(false); |
|
124 } |
|
125 }, this.timePointers); |
|
126 } |
|
127 //Sinon si ce sont les coordonnées de la main gauche. |
|
128 else if(msg.data[0] == '0') |
|
129 { |
|
130 var instruction = msg.data.substring(2, msg.data.length); |
|
131 pt = instruction.split(';'); |
|
132 var x = Math.abs(parseFloat(pt[0])), y = Math.abs(parseFloat(pt[1])); |
|
133 |
|
134 this.pointerRight = true; |
|
135 clearTimeout(this.pointerRightTimeout); |
|
136 |
|
137 this.pointers.refreshSecondPointer(x, y); |
|
138 |
|
139 if(!this.pointers.isSecondPointerDisplayed) |
|
140 { |
|
141 this.pointers.secondPointerDisplay(true); |
|
142 this.pointers.isSecondPointerDisplayed = true; |
|
143 } |
|
144 |
|
145 this.pointerRightTimeout = setTimeout(function() |
|
146 { |
|
147 _this.pointerRight = false; |
|
148 |
|
149 if(_this.pointers.isSecondPointerDisplayed) |
|
150 { |
|
151 _this.pointers.isSecondPointerDisplayed = false; |
|
152 _this.pointers.secondPointerDisplay(false); |
|
153 } |
|
154 }, this.timePointers); |
|
155 } |
|
156 |
|
157 if(this.pointerLeft && !this.pointerRight || !this.pointerLeft && this.pointerRight || !this.pointerLeft && !this.pointerRight) |
|
158 { |
|
159 this.pointers.areBothPointersHere = false; |
|
160 } |
|
161 } |