Created
April 22, 2016 13:19
-
-
Save mosfet1kg/edf8baca5f0ae588aa8c66bc000f6732 to your computer and use it in GitHub Desktop.
node js keyboard input + socket.io
This file contains hidden or 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
/////////// Server Part | |
var stdin = process.stdin; | |
var express = require('express'), | |
app = express(); | |
var server = app.listen(55555,function(){ | |
console.log('Port:',this.address().port); | |
}); | |
var io = require('socket.io')(server); | |
// without this, we would only get streams once enter is pressed | |
stdin.setRawMode( true ); | |
// resume stdin in the parent process (node app won't quit all by itself | |
// unless an error or process.exit() happens) | |
stdin.resume(); | |
// i don't want binary, do you? | |
stdin.setEncoding( 'utf8' ); | |
// on any data into stdin | |
stdin.on( 'data', function( key ){ | |
// ctrl-c ( end of text ) | |
if ( key === '\u0003' ) { | |
process.exit(); | |
} | |
// write the key to stdout all normal like | |
process.stdout.write( key ); | |
if(waitList.length){ | |
console.log('lengt is above 0'); | |
var f = waitList.shift(); | |
f(key); | |
} | |
}); | |
var waitList = []; | |
io.on('connection', function(socket) { | |
socket.on('select:eech', function(msg){ | |
console.log(msg); | |
(function(s){ | |
waitList.push(function(item){ | |
console.log('waitList'); | |
s.emit('res', "hi"+item); | |
}) | |
})(socket); | |
}); | |
}); | |
/////////// Client Part | |
var socket = require('socket.io-client')('http://localhost:55555'); | |
socket.emit('select:eech', "hello world!"); | |
socket.on('res', function(msg){ | |
console.log(msg); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment