https://nodejs.org/ja/ からダウンロードすることができる
WindowsやLinux、Macなどがある最も一般的なものを
利用します。汎用性が高いのが魅力ですね。
ラズパイですと、nvmでバージョン指定でインストールも便利です。
まず、サーバー側の簡単なプログラムをササッと
用意してみます。
app.js
var express = require('express');
var app = express();
var http = require('http').Server(app);
const io = require('socket.io')(http);
const PORT = process.env.PORT || 3000;
app.get('/' , function(req, res){
res.sendFile(__dirname+'/index.html');
});
io.on('connection',function(socket){
socket.on('message',function(msg){
console.log('message: ' + msg);
io.emit('message', msg);
});
});
http.listen(PORT, function(){
console.log('server listening. Port:' + PORT);
});
index.html
<!DOCTYPE html>
<html>
<head>
<title>socket.io chat</title>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
</head>
<body>
<ul id="messages"></ul>
<form id="message_form" action="#">
<input id="input_msg" autocomplete="off" /><button>Send</button>
</form>
<script>
var socketio = io();
$(function(){
$('#message_form').submit(function(){
socketio.emit('message', $('#input_msg').val());
$('#input_msg').val('');
return false;
});
socketio.on('message',function(msg){
$('#messages').append($('<li>').text(msg));
});
});
</script>
</body>
</html>









