Javascript Websockets与特定用户的群聊天
我的网站上有一个正在运行的聊天应用程序。它是使用websockets和php实现的,运行良好。问题是,每当我向单个用户发送消息时,它就会将该消息广播给当前连接到我的网站的所有用户。如果有人能告诉我,当有人连接到我们的应用程序时,如何获取每个用户的唯一id,这将非常有用。如果我可以获取当前登录聊天用户的用户ID,这将解决我的问题,但在用户连接时我无法检索该用户ID(websocket的打开状态)。我可以在建立连接时获取userid,但是在连接发生之前我需要userid。当连接发生时,它以串行方式(1,2,3..n)将自己的ID分配给连接的用户Javascript Websockets与特定用户的群聊天,javascript,php,websocket,chat,Javascript,Php,Websocket,Chat,我的网站上有一个正在运行的聊天应用程序。它是使用websockets和php实现的,运行良好。问题是,每当我向单个用户发送消息时,它就会将该消息广播给当前连接到我的网站的所有用户。如果有人能告诉我,当有人连接到我们的应用程序时,如何获取每个用户的唯一id,这将非常有用。如果我可以获取当前登录聊天用户的用户ID,这将解决我的问题,但在用户连接时我无法检索该用户ID(websocket的打开状态)。我可以在建立连接时获取userid,但是在连接发生之前我需要userid。当连接发生时,它以串行方式(
HTML
输入,文本区域{边框:1px实心#CCC;边距:0px;填充:0px}
#正文{最大宽度:800px;边距:自动}
#原木{宽度:100%;高度:400px}
#消息{宽度:100%;行高:20px}
var服务器;
功能日志(文本){
$log=$(“#log”);
//向日志添加文本
$log.append(($log.val()?“n”:“”)+文本);
//自动旋转
$log[0]。scrollTop=$log[0]。scrollHeight-$log[0]。clientHeight;
}
函数发送(文本){
发送('message',text);
}
$(文档).ready(函数(){
日志('连接…');
服务器=新的FancyWebSocket('ws://127.0.0.1:9300');
$(“#消息”)。按键(功能(e){
if(e.keyCode==13&&this.value){
日志('You:'+this.value);
发送(此值);
$(this.val(“”);
}
});
//让用户知道我们已连接
Server.bind('open',function(){
日志(“已连接”);
});
//哦,不!断开了。
Server.bind('close',函数(数据){
日志(“断开连接”);
});
//记录从服务器发送的任何消息
Server.bind('message',函数(有效负载){
日志(有效载荷);
});
connect();
});
temp\u users
数组temp\u users
数组中的相应ID。现在,您可以识别特定用户,并使用映射到其用户名的套接字向其发送消息<?php
// prevent the server from timing out
set_time_limit(0);
// include the web sockets server script (the server is started at the far bottom of this file)
require 'class.PHPWebSocket.php';
// when a client sends data to the server
function wsOnMessage($clientID, $message, $messageLength, $binary) {
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );
// check if message length is 0
if ($messageLength == 0) {
$Server->wsClose($clientID);
return;
}
//Send the message to everyone but the person who said it
foreach ( $Server->wsClients as $id => $client )
if ( $id != $clientID )
$Server->wsSend($id, "Visitor $clientID ($ip) said "$message"");
}
// when a client connects
function wsOnOpen($clientID)
{
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );
$Server->log( "$ip ($clientID) has connected." );
//Send a join notice to everyone but the person who joined
foreach ( $Server->wsClients as $id => $client )
if ( $id != $clientID )
$Server->wsSend($id, "Visitor $clientID ($ip) has joined the room.");
}
// when a client closes or lost connection
function wsOnClose($clientID, $status) {
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );
$Server->log( "$ip ($clientID) has disconnected." );
//Send a user left notice to everyone in the room
foreach ( $Server->wsClients as $id => $client )
$Server->wsSend($id, "Visitor $clientID ($ip) has left the room.");
}
// start the server
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
// for other computers to connect, you will probably need to change this to your LAN IP or external IP,
// alternatively use: gethostbyaddr(gethostbyname($_SERVER['SERVER_NAME']))
$Server->wsStartServer('127.0.0.1', 9300);
?>
<!doctype html>
<html>
<head>
<meta charset='UTF-8' />
<style>
input, textarea {border:1px solid #CCC;margin:0px;padding:0px}
#body {max-width:800px;margin:auto}
#log {width:100%;height:400px}
#message {width:100%;line-height:20px}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="fancywebsocket.js"></script>
<script>
var Server;
function log( text ) {
$log = $('#log');
//Add text to log
$log.append(($log.val()?"n":'')+text);
//Autoscroll
$log[0].scrollTop = $log[0].scrollHeight - $log[0].clientHeight;
}
function send( text ) {
Server.send( 'message', text );
}
$(document).ready(function() {
log('Connecting...');
Server = new FancyWebSocket('ws://127.0.0.1:9300');
$('#message').keypress(function(e) {
if ( e.keyCode == 13 && this.value ) {
log( 'You: ' + this.value );
send( this.value );
$(this).val('');
}
});
//Let the user know we're connected
Server.bind('open', function() {
log( "Connected." );
});
//OH NOES! Disconnection occurred.
Server.bind('close', function( data ) {
log( "Disconnected." );
});
//Log any messages sent from server
Server.bind('message', function( payload ) {
log( payload );
});
Server.connect();
});
</script>
</head>
<body>
<div id='body'>
<textarea id='log' name='log' readonly='readonly'></textarea><br/>
<input type='text' id='message' name='message' />
</div>
</body>
</html>