なぜか質問を投稿すると反映されない。。 何かしら問題があってはじかれている??
Node.jsとSocket.IOとexpressを使用してリアルタイムチャットを作成
関連記事はこちらで見ることが出来ます。
記事「SOCKET.IOとEXPRESSをインストール」からのカスタマイズです。
ポートの解放
$ sudo firewall-cmd --add-port=3001/tcp --zone=public --permanent $ sudo firewall-cmd --reload
app.js
var app =require('express')();
var http=require('http').Server(app);
var io =require('socket.io')(http);
app.get('/',function(req,res){
res.sendFile(__dirname +'/get.html');
});
io.on('connection',function(socket){
socket.on('chat message',function(msg){
io.emit('chat message',msg);
});
});
http.listen(3001,function(){
console.log('listening on *:3001');
});
get.html
<!DOCTYPE html>
<html>
<head>
<title>socket.io</title>
</head>
<body>
<form action="">
<input id="sendmsg" /><button>Send</button>
</form>
<ul id="messages" style="list-style-type: none;"></ul>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#sendmsg').val());
$('#sendmsg').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>
起動
$ node app.js listening on *:3001
ブラウザでアクセス
http://hogehoge.com:3001
検索

コメントを残す