Skip to content

Instantly share code, notes, and snippets.

@masayuki5160
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save masayuki5160/2eefc6be0550888fb19d to your computer and use it in GitHub Desktop.

Select an option

Save masayuki5160/2eefc6be0550888fb19d to your computer and use it in GitHub Desktop.
EC2でnode.js, socket.ioのテスト
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs');
app.listen(3000);
io.set('log level', 1);
function handler(req, res) {
fs.readFile(__dirname + '/index.html', function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error');
}
res.writeHead(200);
res.write(data);
res.end();
})
}
io.sockets.on('connection', function(socket) {
socket.on('emit_from_client', function(data) {
socket.join(data.room);
socket.emit('emit_from_server', 'you are in ' + data.room);
socket.broadcast.to(data.room).emit('emit_from_server', data.msg);
});
});
作業ログ
$ sudo yum update -y
$ sudo yum install git
$ git clone https://github.com/creationix/nvm.git ~/.nvm
$ source ~/.nvm/nvm.sh
$ nvm ls-remote
$ nvm install v0.12.1
//httpモジュールをインポート
var http = require('http');
//Webサーバーの設定
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000);
console.log('Server running. ');
node.js起動時エラー : Error: listen EADDRINUSE
http://qiita.com/kkam0907/items/65ceedbf3c71ae086838
[ec2-user@ip-172-16-0-55 ~]$ ps aux | grep node
ec2-user 5725 0.0 2.0 760544 21144 pts/0 Tl 01:32 0:00 node app.js
ec2-user 5761 0.0 0.0 114620 916 pts/0 S+ 01:43 0:00 grep node
[ec2-user@ip-172-16-0-55 ~]$ sudo kill -9 5725
[1]+ 強制終了 node app.js
[ec2-user@ip-172-16-0-55 ~]$
[ec2-user@ip-172-16-0-55 ~]$
$ npm install socket.io
> [email protected] install /home/ec2-user/node_modules/socket.io/node_modules/engine.io/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)
make: ディレクトリ `/home/ec2-user/node_modules/socket.io/node_modules/engine.io/node_modules/ws/build' に入ります
CXX(target) Release/obj.target/bufferutil/src/bufferutil.o
make: ディレクトリ `/home/ec2-user/node_modules/socket.io/node_modules/engine.io/node_modules/ws/build' から出ます
> [email protected] install /home/ec2-user/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)
make: ディレクトリ `/home/ec2-user/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/build' に入ります
CXX(target) Release/obj.target/bufferutil/src/bufferutil.o
make: ディレクトリ `/home/ec2-user/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/build' から出ます
[email protected] node_modules/socket.io
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
@masayuki5160
Copy link
Author

@masayuki5160
Copy link
Author

あいかわらずドットインストールがすごい
参考:http://dotinstall.com/lessons/basic_socketio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment