Created
June 9, 2017 12:29
-
-
Save Spriithy/368f5ab55e7aacf5fb37738cbee5d03a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class Client { | |
constructor() { | |
this.local = window.location.href.indexOf("file://") > -1 || window.location.href.indexOf("localhost") > -1; | |
this.twitchKey = this.local ? "miocs45t65k5y2ym8vtfq56lpofbocv" : "74q6uco0vgklzwgehthw3fbyfif54er"; | |
this.racers = []; | |
this.websocket = null; | |
this.wsUri = "ws://perso.mog-creations.com:43042/"; | |
Twitch.init({ clientId: this.twitchKey }, function (error, status) { | |
if (error) { | |
console.log(error); | |
} | |
if (status.authenticated) { | |
console.log('Authenticated to Twitch!') | |
} | |
}); | |
} | |
init = () => { | |
if (this.websocket != null && this.websocket.readyState === this.websocket.OPEN) { | |
this.websocket.close(); | |
this.closed = true; | |
} | |
// Initialisation | |
this.websocket = new WebSocket(this.wsUri); | |
this.websocket.onopen = this.onOpen; | |
this.websocket.onclose = this.onClose; | |
this.websocket.onmessage = this.onMessage; | |
this.websocket.onerror = this.onError; | |
this.closed = false; | |
} | |
login = () => { | |
try { | |
Twitch.login({ | |
redirect_uri: this.local ? 'http://localhost:3000/' : 'http://tmc2017.mog-creations.com/', | |
scope: ['user_read'] | |
}); | |
} catch (e) { | |
console.log(e); | |
} | |
} | |
onOpen = (evt) => { | |
console.log('CONNECTED'); | |
} | |
onClose = (evt) => { | |
console.log('DISCONNECTED'); | |
} | |
onError = (evt) => { | |
console.log('ERROR: ' + evt.data); | |
} | |
onMessage = (evt) => { | |
var data = JSON.parse(evt.data); | |
console.log('RECEIVED:'); | |
console.log(data); | |
switch (data.id) { | |
case 1: // HandShake packet | |
if (!data.status) alert("Bad protocol version. Please update your client."); | |
else this.send({ | |
id: 2, | |
token: Twitch.getToken() | |
}); | |
break; | |
case 2: // Auth status packet, defines if moderator or not | |
this.moderator = data.moderator; | |
break; | |
case 3: // Error packet | |
alert(data.error); | |
break; | |
case 4: // Place in Auth queue | |
// TODO... | |
break; | |
case 5: // Current currency packet | |
this.balance = data.currency; | |
break; | |
case 6: // Current Bet value packet on a racer | |
this.racers[data.racer_id].currentValue = data.bid_value; | |
break; | |
case 7: // Session start packet | |
this.racers = data.racers; | |
break; | |
case 8: // Session end packet | |
// const winner = data.winner; | |
break; | |
case 9: // Bet currently placed | |
break; | |
default: | |
console.log("PACKET NOT IMPLEMENTED"); | |
console.log(data); | |
} | |
} | |
send = (toSend, stringify = true) => { | |
var snd = stringify ? JSON.stringify(toSend) : toSend; | |
console.log('SENDING:'); | |
console.log(snd); | |
this.websocket.send(snd); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment