簡體   English   中英

與node.js中的特定用戶聊天

[英]chat with particular user in node.js

這是我與選定用戶進行私人聊天的代碼。 它不像私信那樣向所選用戶發送信息。

服務器端:

   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>

當我選擇發送私人消息的用戶時,它不起作用。 請幫我一個人

您在服務器套接字實現中缺少某些邏輯。

而不是使用io.sockets.emit(...)將消息發送給所有客戶端,您應該將其發送到特定的房間socket.to(...).emit(...)

請看一下文檔

這是抽象邏輯:

  1. 您的套接字應該屬於某個房間,以便接收“私有”消息( socket.join(...)

     socket.join('roomName'); 
  2. 您應該將“私人”消息廣播到特定的房間,而不是廣播給所有客戶。 您可以通過指定房間來做到這一點( socket.to(...) ):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM