Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用socket.io向特定用户发送消息?_Php_Sockets_Websocket_Socket.io_Chat - Fatal编程技术网

Php 如何使用socket.io向特定用户发送消息?

Php 如何使用socket.io向特定用户发送消息?,php,sockets,websocket,socket.io,chat,Php,Sockets,Websocket,Socket.io,Chat,如何仅向具有我指定的特定ID的用户发送消息 例如,我有一个id为5的用户,我只想向他发送消息,而不是向所有连接的用户发送消息。当他连接时,应该如何将此id发送到服务器?有可能吗 客户 <?php $id=5; // id to send to echo ' <div id="id">'.$id.'</div> <div id="messages"></div> <input type="text" id="type"> <d

如何仅向具有我指定的特定ID的用户发送消息

例如,我有一个id为5的用户,我只想向他发送消息,而不是向所有连接的用户发送消息。当他连接时,应该如何将此id发送到服务器?有可能吗

客户

<?php
$id=5; // id to send to
echo '
<div id="id">'.$id.'</div>
<div id="messages"></div>
<input type="text" id="type">
<div id="btn">Press</div>
';
?>

<script>
$(document).ready(function(){
var id=$('#id').html();
var socket=io.connect('http://localhost:8010');
socket.on('connecting',function(){alert('Connecting');});
socket.on('connect',function(){alert('Connected');});
socket.on('message',function(data){message(data.text);});
function message(text){$('#messages').append(text+'<br>');}
$('#btn').click(function(){
    var text=$('#type').val();
    socket.emit("message",{text:text});
});
});
</script>

您可以在握手时将用户ID从客户端传递到服务器,并让用户加入组(例如“user5”)。然后可以向该组发射:

客户端:

var id=$('#id').html();
var socket=io.connect('http://localhost:8010', {
    query: 'userId=' + id
});
io.sockets.on('connection',function(client){
    var userId = client.handshake.query.userId;
    client.join('user' + userId);
    //from now each client joins his personal group...
    //... and you can send a message to user with id=5 like this:
    io.to('user5').emit('test', 'hello');

    //your further code
});
服务器端:

var id=$('#id').html();
var socket=io.connect('http://localhost:8010', {
    query: 'userId=' + id
});
io.sockets.on('connection',function(client){
    var userId = client.handshake.query.userId;
    client.join('user' + userId);
    //from now each client joins his personal group...
    //... and you can send a message to user with id=5 like this:
    io.to('user5').emit('test', 'hello');

    //your further code
});

您可以在握手时将用户ID从客户端传递到服务器,并让用户加入组(例如“user5”)。然后可以向该组发射:

客户端:

var id=$('#id').html();
var socket=io.connect('http://localhost:8010', {
    query: 'userId=' + id
});
io.sockets.on('connection',function(client){
    var userId = client.handshake.query.userId;
    client.join('user' + userId);
    //from now each client joins his personal group...
    //... and you can send a message to user with id=5 like this:
    io.to('user5').emit('test', 'hello');

    //your further code
});
服务器端:

var id=$('#id').html();
var socket=io.connect('http://localhost:8010', {
    query: 'userId=' + id
});
io.sockets.on('connection',function(client){
    var userId = client.handshake.query.userId;
    client.join('user' + userId);
    //from now each client joins his personal group...
    //... and you can send a message to user with id=5 like this:
    io.to('user5').emit('test', 'hello');

    //your further code
});

您可以在握手时将用户ID从客户端传递到服务器,并让用户加入组(例如“user5”)。然后可以向该组发射:

客户端:

var id=$('#id').html();
var socket=io.connect('http://localhost:8010', {
    query: 'userId=' + id
});
io.sockets.on('connection',function(client){
    var userId = client.handshake.query.userId;
    client.join('user' + userId);
    //from now each client joins his personal group...
    //... and you can send a message to user with id=5 like this:
    io.to('user5').emit('test', 'hello');

    //your further code
});
服务器端:

var id=$('#id').html();
var socket=io.connect('http://localhost:8010', {
    query: 'userId=' + id
});
io.sockets.on('connection',function(client){
    var userId = client.handshake.query.userId;
    client.join('user' + userId);
    //from now each client joins his personal group...
    //... and you can send a message to user with id=5 like this:
    io.to('user5').emit('test', 'hello');

    //your further code
});

您可以在握手时将用户ID从客户端传递到服务器,并让用户加入组(例如“user5”)。然后可以向该组发射:

客户端:

var id=$('#id').html();
var socket=io.connect('http://localhost:8010', {
    query: 'userId=' + id
});
io.sockets.on('connection',function(client){
    var userId = client.handshake.query.userId;
    client.join('user' + userId);
    //from now each client joins his personal group...
    //... and you can send a message to user with id=5 like this:
    io.to('user5').emit('test', 'hello');

    //your further code
});
服务器端:

var id=$('#id').html();
var socket=io.connect('http://localhost:8010', {
    query: 'userId=' + id
});
io.sockets.on('connection',function(client){
    var userId = client.handshake.query.userId;
    client.join('user' + userId);
    //from now each client joins his personal group...
    //... and you can send a message to user with id=5 like this:
    io.to('user5').emit('test', 'hello');

    //your further code
});