This repository has been archived by the owner on Jul 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
joystick.ts
99 lines (91 loc) · 2.98 KB
/
joystick.ts
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
/* tslint:disable:no-console */
import * as WebSocket from 'ws';
import {
GameClient,
IJoystick,
IJoystickData,
IParticipant,
setWebSocket,
} from '../lib';
if (process.argv.length < 4) {
console.log('Usage gameClient.exe <token> <versionId>');
process.exit();
}
// We need to tell the interactive client what type of websocket we are using.
setWebSocket(WebSocket);
// As we're on the Streamer's side we need a "GameClient" instance
const client = new GameClient();
const joystick: IJoystickData = {
sampleRate: 50,
kind: 'joystick',
controlID: 'joystick1',
position: [
{
size: 'large',
width: 12,
height: 12,
x: 2,
y: 1,
},
{
size: 'medium',
width: 12,
height: 12,
x: 1,
y: 0,
},
{
size: 'small',
width: 12,
height: 12,
x: 1,
y: 0,
},
],
};
// Log when we're connected to interactive and setup your game!
client.on('open', () => {
console.log('Connected to Interactive!');
// Now we can create the controls, We need to add them to a scene though.
// Every Interactive Experience has a "default" scene so we'll add them there there.
client
.createControls({
sceneID: 'default',
controls: [joystick],
})
.then(controls => {
// Now that the controls are created we can add some event listeners to them!
controls.forEach((control: IJoystick) => {
// move here means that someone has clicked the button.
control.on('move', (inputEvent, participant) => {
// Let's tell the user who they are, and where they moved the joystick
console.log(
`${participant.username} moved, ${
inputEvent.input.controlID
} to ${inputEvent.input.x}, ${inputEvent.input.y}`,
);
});
});
// Controls don't appear unless we tell Interactive that we are ready!
client.ready(true);
});
});
// These can be un-commented to see the raw JSON messages under the hood
client.on('message', (err: any) => console.log('<<<', err));
client.on('send', (err: any) => console.log('>>>', err));
// client.on('error', (err: any) => console.log(err));
// Now we open the connection passing in our authentication details and an experienceId.
client.open({
authToken: process.argv[2],
versionId: parseInt(process.argv[3], 10),
});
client.state.on('participantJoin', participant => {
console.log(`${participant.username}(${participant.sessionID}) Joined`);
});
client.state.on(
'participantLeave',
(participantSessionID: string, participant: IParticipant) => {
console.log(`${participant.username}(${participantSessionID}) Left`);
},
);
/* tslint:enable:no-console */