LoginSignup
0
0

More than 5 years have passed since last update.

ズンドコキヨシ with socket.io

Posted at

socket.io で作ってみました。
特別な事はしていません。

var app  = require('express')();
var http = require('http').Server(app);
var io   = require('socket.io')(http);

var messages = {
  0: 'ズン',
  1: 'ドコ',
  2: 'キ・ヨ・シ!',
};

var current  = [];
var expected = [0, 0, 0, 0, 1];

function zundoko(socket) {
  var rand = Math.floor(Math.random() * 2); // 0 or 1
  if (current.push(rand) > 5) {
     current.shift();
  }
  emit(socket, rand); // zun or doko ..

  // kiyoshi ?
  if (current.toString() == expected.toString()) {
    emit(socket, 2);
  }
}

function emit(socket, index) {
  socket.emit('message', messages[index]);
}

io.on('connection', function(socket) {
  setInterval(zundoko.bind(this, socket), 1500);
});

app.get('/', function(req, res) {
  res.sendFile(__dirname + '/index.html');
});

http.listen(3050, function() {
  console.log('listening on *:3050');
});

ソースは、
https://github.com/nilesflow/zundoko-socket.io

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0