Answer the question
In order to leave comments, you need to log in
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);
});
});
<form>
<textarea name="text" id="comment"></textarea>
<button type="submit" class="btn btn-primary">Отправить</button>
</form>
<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>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question