与node.js中的特定用户聊天

时间:2015-01-06 12:02:36

标签: javascript jquery html node.js socket.io

这是我与所选用户进行私聊的代码。它不会像私人消息一样向所选用户发送消息。

服务器端:

   var fs = require('fs'),
    http = require('http'),

    sio = require('socket.io');




  var server = http.createServer( function(req, res) {

    if (req.url === '/index') {
        fs.readFile('./index.html', function(err, page) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(page);
            res.end();

        });
    }
    else if (req.url === '/karthick') {
        fs.readFile('./karthick.html', function(err, page) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(page);
            res.end();
        });
    }
    else if (req.url === '/raj') {
        fs.readFile('./raj.html', function(err, page) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(page);
            res.end();
        });
    }

else if (req.url === '/Harendra') {
        fs.readFile('./Harendra.html', function(err, page) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(page);
            res.end();
        });
    }
    else if (req.url === '/send') {
        fs.readFile('./sendingmsg.html', function(err, page) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(page);
            res.end();
        });
    }
    else {
            res.writeHead(301,
              {Location: '/index'}
            );
            res.end();
        }
});
server.listen(8000, function() {
  console.log('Server listening at http://192.168.1.16/8000');
});
io = sio.listen(server);
// store messages
var messages = [];

io.sockets.on('connection', function(socket){
  socket.on('chat message', function(msg){
    console.log('Received: ', msg);
    messages.push(msg);
    io.sockets.emit('chat message', msg);
  });
  messages.forEach(function(msg) {
    socket.send(msg);
  })
});

客户端:发送

<!DOCTYPE html>
<html>
    <body>
        <title>send message</title>
        <ul id="messages"></ul>
        <form action="">
            <select>
                 <option id="kar"  value="karthick">karthick</option>
                 <option id="raj" value="Raj">Raj</option>
                 <option id="haren" value="Harendra">Harendra</option>
          </select><br />
             <textarea id="m"  rows="4" cols="50">

            </textarea><br/>
                <button id=btn>Send</button>
        </form>
         <script src="https://cdn.socket.io/socket.io-1.1.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io.connect('http://192.168.1.21:8000');
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('disconnect', function() {
        $('#messages').append('<li>Disconnected</li>');
      });
     </script>
    </body>
</html>

收到客户:1

<!DOCTYPE html>
<html>
    <body>
        <title>Users</title>
        Welcome Harendra
        <ul id="messages"></ul>
        <script src="/socket.io/socket.io.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
        <script>
         $(function(){
    var socket = io.connect();
    socket.on('connect', function () {
      socket.on('message', function(message) {
        $('#messages').append($('<li>').text(message));
      });
      socket.on('disconnect', function() {
        $('#messages').append('<li>Disconnected</li>');
      });
    });


  });
        </script>

    </body>
</html>

当我选择用户发送私信时,它无效。请帮帮我一个人

1 个答案:

答案 0 :(得分:1)

您在服务器套接字实现中缺少一些逻辑。

不应将邮件发送给io.sockets.emit(...)的所有客户,而应将其发送到特定的会议室 - socket.to(...).emit(...)

请查看documentation

这是抽象逻辑:

  1. 你的插座应该属于某个房间,以便接收&#34;私人&#34;消息(socket.join(...)

    socket.join('roomName');
    
  2. 你应该播放你的私人&#34;发送到特定房间的消息,而不是将消息广播给所有客户。您可以指定房间(socket.to(...)):

    socket.to('roomName').emit('chat message', msg);