Skip to content

Instantly share code, notes, and snippets.

@cranic
Created March 20, 2014 13:43
Show Gist options
  • Save cranic/9663995 to your computer and use it in GitHub Desktop.
Save cranic/9663995 to your computer and use it in GitHub Desktop.
Client de comandos por chat do Twitch.tv
var express = require('express');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io').listen(http);
var irc = require('irc');
var commands = ['up', 'down', 'left', 'right', 'reset'];
// Conexão com o IRC do canal
var client = new irc.Client('irc.twitch.tv', 'cooplays', {
nick: 'cooplays',
userName: 'cooplays',
channels: ['#cooplays'],
password: 'oauth:meuoauth',
sasl: true,
port: 6667
});
client.addListener('error', function(err){
throw err;
});
// Ao receber uma msg verificamos se é um comando
client.addListener('message#cooplays', function (from, message) {
// O comando digitado é válido?
var cmd = commands.filter(function(val){
return message.toLowerCase().indexOf(val) === 0;
})[0];
// Se for um comando eviamos para o navegador
if(cmd)
io.sockets.emit('command', {
user: from,
command : cmd
});
});
// Nada de novo aqui, só o express
app.use(express.static('./public'));
// Servimos a página principal com um iframe e
// um painel lateral
app.get('/', function(req, res){
res.sendfile(__dirname + '/view/index.html');
});
// O jogo mesmo, carrega dentro de um iframe, assim
// não precisamos mexer no CSS, hue hue br
app.get('/game', function(req, res){
res.sendfile(__dirname + '/view/game.html');
});
// Iniciamos a aplicação e bindamos ela em um
// endereço local para ninguém ter acesso
http.listen(8085, '127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment