forked from mixer/interactive-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.ts
76 lines (63 loc) · 2.75 KB
/
basic.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
/* tslint:disable:no-console */
import * as WebSocket from 'ws';
import {
GameClient,
IButton,
IParticipant,
setWebSocket,
} from '../lib';
import { makeControls } from './util';
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();
// Log when we're connected to interactive and setup your game!
client.on('open', () => {
console.log('Connected to Interactive!');
// Now that we've opened the connection 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.
client.createControls({
sceneID: 'default',
controls: makeControls(5, i => `Button ${i}`),
}).then(controls => {
// Now that the controls are created we can add some event listeners to them!
controls.forEach((control: IButton) => {
// mousedown here means that someone has clicked the button.
control.on('mousedown', (inputEvent, participant) => {
// Let's tell the user who they are, and what they pushed.
console.log(`${participant.username} pushed, ${inputEvent.input.controlID}`);
// Did this push involve a spark cost?
if (inputEvent.transactionID) {
// Unless you capture the transaction the sparks are not deducted.
client.captureTransaction(inputEvent.transactionID)
.then(() => {
console.log(`Charged ${participant.username} ${control.cost} sparks!`);
});
}
});
});
// Controls don't appear unless we tell Interactive that we are ready!
return 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 */