还有另一个问题。我开始习惯websocket的工作方式。我什至设法实现了跨域通信。但现在我还没有达到另一个里程碑。
这是我当前实现的一个片段
public function onMessage(ConnectionInterface $conn, $msg)
{
$msgjson = json_decode($msg);
$tag = $msgjson->tag;
global $users;
if($tag == "[msgsend]")
{
foreach($this->clients as $client)
{
$client->send($msg);
}
}
else if($tag == "[bye]")
{
foreach($this->clients as $client)
{
$client->send($msg);
}
foreach($users as $key => $user)
{
if($user->name == $msgjson->uname)
{
unset($users[$key]);
}
}
$this->clients->detach($conn);
}
else if($tag == "[connected]")
{
//store client information
$temp = new Users();
$temp->name = $msgjson->uname;
$temp->connection = $conn;
$temp->timestamp = new 'DateTime();
$users[] = $temp;
usort($users, array($this, "cmp"));
//send out messages
foreach($this->clients as $client)
{
$client->send($msg);
}
}
else if($tag == "[imalive]")
{
//update user timestamp who sent [imalive]
global $users;
foreach($users as $user)
{
if($msgjson->uname == $user->name)
{
$user->timestamp = new 'DateTime();
}
}
}
}
现在我的问题是。正如我们所看到的,在onMessage((函数和我完成的教程中,我知道如何读取和解析JSON数据,理解消息,告诉消息来自谁($conn(。
但是,假设当我在 JSON 数据包中发送消息时,我想包括消息来自谁以及消息将发送给谁的昵称。这将允许我在我正在构建的社交网络和聊天室中实现私人即时消息。
我只想将消息发送到特定的客户端,而不是 for 循环向所有连接的客户端发送消息。我知道客户有一个属性($this->$client->resourceID或类似的东西(,但也不确定如何将其合并为解决方案。我还希望用户在跳转到网站上的不同页面时保持连接,即使在刷新后,仍然能够继续发送消息。我假设每次刷新都会断开客户端的连接。所以我必须有一种方法,让服务器每次都能分辨出谁是谁,消息来自哪里以及它们要去哪里。
但是,是的,私人消息传递。我不想向所有人或意想不到的目标发送不合时宜的信息。我怎样才能做到这一点?我希望我的问题有意义。谢谢。
能够唯一标识连接到 WebSocket 服务器的用户,然后能够定位这些用户,特别是在发送消息时,需要从实际与服务器协商连接的onOpen
回调开始。
在onOpen
方法中,您应该有某种方法通过全局存储在数据库或持久性存储中的某个用户 ID 来唯一标识系统上的用户。由于连接是通过HTTP协商的,因此您可以通过$conn->WebSocket->request
访问HTTP请求,这是一个包含客户端HTTP请求信息的GuzzleHttp
对象。例如,您可以使用它来提取包含一些用户 ID 数据或令牌的 cookie,您可以将这些数据或令牌与您的数据库进行比较,以确定用户是谁,然后将其存储为 $client
对象的属性。
现在,假设您正在编写一个普通的 PHP 脚本,其中您通过 HTTP 进行用户身份验证并将用户 ID 存储在会话中。此会话在客户端计算机上设置一个包含会话 ID 的 cookie(默认情况下,cookie 名称是会话名称,除非您更改它,否则通常PHPSESSID
(。此会话 ID 可以在 WebSocket 服务器中使用,以与通常在 PHP 中相同的方式访问会话存储。
下面是一个简单的示例,其中我们希望请求中名为 PHPSESSID
的 cookie 从 cookie 捕获会话 ID。
public function onOpen(ConnectionInterface $conn) {
// extract the cookie header from the HTTP request as a string
$cookies = (string) $conn->WebSocket->request->getHeader('Cookie');
// look at each cookie to find the one you expect
$cookies = array_map('trim', explode(';', $cookies));
$sessionId = null;
foreach($cookies as $cookie) {
// If the string is empty keep going
if (!strlen($cookie)) {
continue;
}
// Otherwise, let's get the cookie name and value
list($cookieName, $cookieValue) = explode('=', $cookie, 2) + [null, null];
// If either are empty, something went wrong, we'll fail silently here
if (!strlen($cookieName) || !strlen($cookieValue)) {
continue;
}
// If it's not the cookie we're looking for keep going
if ($cookieName !== "PHPSESSID") {
continue;
}
// If we've gotten this far we have the session id
$sessionId = urldecode($cookieValue);
break;
}
// If we got here and $sessionId is still null, then the user isn't logged in
if (!$sessionId) {
return $conn->close(); // close the connection - no session!
}
}
现在您实际上已经有了$sessionId
您可以使用它来访问该会话的会话存储,将会话信息拉入 WebSocket 服务器,并将其存储为客户端连接对象 $conn
的属性。
因此,从上面的示例继续,让我们将此代码添加到 onOpen
方法中。
public function onOpen(ConnectionInterface $conn) {
$conn->session = $this->methodToGetSessionData($sessionId);
// now you have access to things in the session
$this->clinets[] = $conn;
}
现在,让我们回到您的示例,您希望仅向一个用户发送消息。假设我们在会话中存储了以下属性,现在可以从客户端连接对象访问这些属性...
$conn->session->userName = 'Bob';
$conn->session->userId = 1;
因此,假设 Bob 想向 Jane 发送一条消息。一个请求进入您的 WS 服务器,类似于 {"from":1,"to":2,tag:"[msgsend]"}
其中该 JSON 的to
和from
属性基本上分别是消息来自的用户的用户 ID 和消息要发送到的用户。假设简是这个例子userId = 2
。
public function onMessage(ConnectionInterface $conn, $msg) {
$msgjson = json_decode($msg);
$tag = $msgjson->tag;
if ($tag == '[msgsend]') {
foreach($this->clients as $client) {
// only send to the designated recipient user id
if ($msgjson->to == $client->session->userId) {
$client->send($msg);
}
}
}
}
显然,您可能希望在那里进行更详细的验证,但您应该能够从这里对此进行扩展。