A
A
ARX22019-04-07 09:06:36
Node.js
ARX2, 2019-04-07 09:06:36

How to send a message to a specific socket.io user?

Good morning. I don't know much about this, but I need
How to send a message to a specific user in socket.io?
Unsourced:
Server.js:

var io = require('socket.io')(6001);
io.on('connection', function(socket){

  console.log('New connection:', socket.id);
  socket.on('message', function(data){
    socket.broadcast.emit(data);
  });

});

Submission form:
<form>
      <textarea name="text" id="comment"></textarea>
      <button type="submit" class="btn btn-primary">Отправить</button>
     </form>

Shipping itself:
<script type="text/javascript">
                          var socket = io(':6001');
                          function appendMessage(data){
                            var regex = /<br\s*[\/]?>/gi;
                            $('.newMessage').append('<p style="float:left;">'+data.message.replace(/(?:\r\n|\r|\n)/g, '<br />')+'</p>');
                          }
                          function appendMessageTwo(data){
                            $('.newMessage').append('<p style="float:right;">'+data.message.replace(/(?:\r\n|\r|\n)/g, '<br />')+'</p>');
                          }
                          $('form').on('submit',function(){
                           var text =  $('textarea').val(),
                              msg = {message : text};
                              socket.send(msg);
                           appendMessage(msg);
                           $('textarea').val('');      
                           return false;

                          });

                          socket.on('message', function(data){
                              appendMessageTwo(data);
                          });
                          </script>

Sends to all users, but only a specific one needs it.
help me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-04-07
@ARX2

How to send a message to specific users?
https://yandex.ru/search/?text=%D0%9A%D0%B0%D0%BA%...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question