Php 如何使用socket.io向特定用户发送消息?
如何仅向具有我指定的特定ID的用户发送消息 例如,我有一个id为5的用户,我只想向他发送消息,而不是向所有连接的用户发送消息。当他连接时,应该如何将此id发送到服务器?有可能吗 客户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
<?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
});