|
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 : client.js |
|
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, _gestures) |
|
25 { |
|
26 this.socket; |
|
27 this.gestures = _gestures; |
|
28 this.showGestureTimeout; |
|
29 |
|
30 this.createWebSocket('ws://' + host + ':' + port + '/'); |
|
31 } |
|
32 |
|
33 /* |
|
34 * Création et Initialisation des sockets et listeners permettant d'écouter le server. |
|
35 */ |
|
36 client.prototype.createWebSocket = function(host) |
|
37 { |
|
38 var _this = this; |
|
39 |
|
40 //Initialise la fonctionnalité websocket du naviguateur. |
|
41 if(window.MozWebSocket) |
|
42 { |
|
43 window.WebSocket = window.MozWebSocket; |
|
44 } |
|
45 |
|
46 //Si le naviguateur ne supporte pas les websockets, on s'arrête là. |
|
47 if(!window.WebSocket) |
|
48 { |
|
49 alert('Votre navigateur ne supporte pas les webSocket!'); |
|
50 return false; |
|
51 } |
|
52 //Sinon |
|
53 else |
|
54 { |
|
55 //On initialise la socket. |
|
56 this.socket = new WebSocket(host); |
|
57 |
|
58 //Si elle est ouverte, on notifie le Front. |
|
59 this.socket.onopen = function() |
|
60 { |
|
61 console.log('Socket ouverte.'); |
|
62 } |
|
63 //Si elle est fermée, on notifie le Front. |
|
64 this.socket.onclose = function() |
|
65 { |
|
66 console.log('Socket fermée.'); |
|
67 } |
|
68 //S'il y a un problème dans la connection, on notifie le Front. |
|
69 this.socket.onerror = function() |
|
70 { |
|
71 console.log('Une erreur est survenue dans le module de Communication.'); |
|
72 } |
|
73 //Si on reçoit un message. |
|
74 this.socket.onmessage = function(msg) |
|
75 { |
|
76 _this.processMsg(msg); |
|
77 } |
|
78 } |
|
79 } |
|
80 |
|
81 /* |
|
82 * Traite un message reçu du Middleware. |
|
83 */ |
|
84 client.prototype.processMsg = function(msg) |
|
85 { |
|
86 if(typeof msg === 'undefined' || typeof msg.data === 'undefined') |
|
87 { |
|
88 return; |
|
89 } |
|
90 |
|
91 var _this = this; |
|
92 |
|
93 //Sinon si ce sont les coordonnées de la main droite. |
|
94 if(msg.data[0] == '2') |
|
95 { |
|
96 var instruction = msg.data.substring(2, msg.data.length); |
|
97 |
|
98 //S'il ne s'agit pas d'une gesture relative à la position de l'utilisateur par rapport à la Kinect. |
|
99 if(instruction != "NO-USER" && instruction.indexOf("INCOMING") == -1 && instruction != "MOSAIC" && instruction != "FILTER") |
|
100 { |
|
101 //On affiche la notification de gesture. |
|
102 this.gestures.showGesture(instruction); |
|
103 } |
|
104 } |
|
105 } |