6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

socket.io 1.x でプライベートチャット作成

Last updated at Posted at 2015-09-06

socket.ioを使ってプライベートのチャットルームを作成してみました。

参考記事

http://qiita.com/mito_log/items/a6207d2b43189687d750
http://stackoverflow.com/questions/23619015/creating-a-private-chat-between-a-key-using-a-node-js-and-socket-io
http://socket.io/docs/rooms-and-namespaces/#joining-and-leaving

開発環境

ubuntu14.04 + nginx + unicorn
node + express + socket.io

ざっくりとした流れ

  1. クライアント側でRoomIdを生成(プライベートチャットユーザー間でRoomIdを共有する)
  2. クライアント側でroomIdを使ってjoinする
  3. メッセージを送信する時に、テキストとRoomIdをサーバに送信する
  4. サーバ側でtoでルームを指定して、emitする

サーバーサイド

index.js
var app = require('express')();
app.get('/',function(req, res) {
  res.sendFile('socket.html',{root:'./'});
  console.log("hosting socket.html");
});

var http = require('http').Server(app);
http.listen(1337, function() {
  console.log("listening on *:1337");
});

var io = require('socket.io')(http);
io.on('connection', function(socket){
  socket.on('joinRoom', function(roomId) {
    socket.join(roomId, function(){
      console.log('joined test room: '+ roomId);
    }); 
  }); 
  socket.on('disconnect', function(){
    console.log('user disconnected');
    io.emit('chatMessage','user disconnected');
  }); 
  socket.on('chatMessage', function(data) {
    console.log('message: '+data.message);
    //4. サーバ側でtoでルームを指定して、emitする
    io.to(data.room).emit('chatMessage', data.message);
  }); 
});

クライアント

socket.html
<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
    * { margin: 0; padding: 0; box-sizing: border-box; }
    body { font: 13px Helvetica, Arial; }
    form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
    form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
    form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
    </style>
    <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>

  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script>
      var socket = io.connect('/');
      var roomId = 'testRoom';
      socket.emit('joinRoom', roomId);
      $('form').submit(function(){
          socket.emit('chatMessage' ,{message:$('#m').val(),room:roomId});
          $('#m').val('');
          return false;
      });
      socket.on('chatMessage', function (msg) {
        $('#messages').append($('<li>').text(msg));
      });
    </script>
  </body>
</html>
6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?