-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamepad_stream.js
153 lines (153 loc) · 6.25 KB
/
gamepad_stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var Status;
(function (Status) {
Status["connecting"] = "connecting";
Status["start_control"] = "waiting key";
Status["controlling"] = "controlling";
Status["disconnected"] = "disconnected";
Status["connection_error"] = "connection_error";
Status["gamepad_disconnected"] = "gamepad_disconnected";
})(Status || (Status = {}));
var GamepadStream = /** @class */ (function () {
function GamepadStream(url, gamepad_index) {
// Tolerance of server failures when processing the messages.
this.fail_tolerance = 10;
// The streaming sending rate (Hz). Default is 5 Hz (200ms) the human reaction time
this.rate = 5;
this.running = false;
this.period = 0.1;
this.start_button = 0;
this.fail_count = 0;
// The status encapusulates the WebSocket and the Gamepad connection status.
this.status = Status.connecting;
if (!navigator.getGamepads)
throw "This browser doesn't support gamepads!";
this.websocket = new WebSocket(url);
var self = this;
this.websocket.onopen = function () { self.onOpen(); };
this.websocket.onclose = function () { self.onClose(); };
this.websocket.onerror = function (err) { self.onError(err); };
this.websocket.onmessage = function (msg) { self.onMessage(msg); };
this.gamepad_index = gamepad_index;
if (!this.isGamepadConnected())
throw "The gamepad provided is not available";
}
GamepadStream.getFirstValidGamepad = function (url) {
console.log("Trying to find a gamepad...");
var gamepads = navigator.getGamepads();
for (var i = 0; i < gamepads.length; i++) {
if (gamepads[i] === null || !gamepads[i].connected)
continue;
console.log("Gamepad sucefully conected!");
console.log(" Index: " + gamepads[i].index);
console.log(" ID: " + gamepads[i].id);
console.log(" Axes: " + gamepads[i].axes.length);
console.log(" Buttons: " + gamepads[i].buttons.length);
console.log(" Mapping: " + gamepads[i].mapping);
console.log("\n");
return new GamepadStream(url, i);
}
throw "Bad things happened, gamepad not found.";
};
GamepadStream.prototype.start = function (rate) {
if (!this.running) {
if (rate != undefined)
this.rate = rate;
var self = this;
this.poll = setInterval(function () { self.updateStatus(); }, 1000 / this.rate);
}
};
GamepadStream.prototype.stop = function () {
if (this.running)
clearInterval(this.poll);
};
GamepadStream.prototype.gamepad = function () {
return navigator.getGamepads()[this.gamepad_index];
};
GamepadStream.prototype.isGamepadConnected = function () {
if (this.gamepad() === null) {
return false;
}
return this.gamepad().connected;
};
GamepadStream.prototype.buildMessage = function () {
var msg = { 'axes': this.gamepad().axes,
'buttons': this.gamepad().buttons.map(function f(b) { return b.value; })
};
return msg;
};
GamepadStream.prototype.sendGamepadMessage = function () {
if (this.getStatus() != Status.controlling)
throw "Tried to send a message when is not controlling";
var msg = this.buildMessage();
this.websocket.send(JSON.stringify(msg));
};
GamepadStream.prototype.askControl = function () {
if (this.getStatus() != Status.start_control)
throw "Tried to ask control when is not starting the control";
var msg = { 'test_message': this.buildMessage() };
this.websocket.send(JSON.stringify(msg));
};
// Do the transitioning between the machine states and update it,
GamepadStream.prototype.updateStatus = function () {
switch (this.getStatus()) {
case Status.connecting:
console.log("waiting connection");
break;
case Status.gamepad_disconnected:
if (this.isGamepadConnected())
this.status = Status.start_control;
break;
case Status.start_control:
if (this.gamepad().buttons[this.start_button].value == 1)
this.askControl();
break;
case Status.controlling:
this.sendGamepadMessage();
break;
case Status.disconnected:
this.stop();
}
};
GamepadStream.prototype.getStatus = function () {
switch (this.websocket.readyState) {
case 0:// Socket has been created. The connection is not yet open.
this.status = Status.connecting;
return this.status;
case 1:// The connection is open and ready to communicate.
if (!this.isGamepadConnected())
this.status = Status.gamepad_disconnected;
return this.status;
default:// The connection is closing, closed or couldn't be opened.
this.status = Status.disconnected;
return this.status;
}
};
GamepadStream.prototype.onOpen = function () {
this.status = Status.start_control;
console.log('Gamepad websocket connected');
};
GamepadStream.prototype.onClose = function () {
this.status = Status.disconnected;
console.log('Gamepad websocket disconnected');
};
GamepadStream.prototype.onError = function (err) {
console.log(err);
this.status = Status.connection_error;
};
GamepadStream.prototype.onMessage = function (msg) {
if (!JSON.parse(msg.data).result) {
this.fail_count++;
if (this.fail_count > this.fail_tolerance) {
this.status = Status.disconnected;
this.websocket.close();
alert("Vessel failed to process joystick commands, disconnected.");
}
}
else {
this.fail_count = 0;
if (this.status == Status.start_control || this.status == Status.connection_error)
this.status = Status.controlling;
}
};
return GamepadStream;
}());