Skip to content

Commit

Permalink
some modify for ws-server
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 3, 2018
1 parent 55bf3cd commit d614189
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
8 changes: 3 additions & 5 deletions src/Router/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function handshake(Request $request, Response $response): array
if ($e instanceof WsRouteException) {
return [
HandlerInterface::HANDSHAKE_FAIL,
$response->withStatus(404)->withAddedHeader('Failed-Reason', 'Route not found')
$response->withStatus(404)->withAddedHeader('Failure-Reason', 'Route not found')
];
}

Expand Down Expand Up @@ -105,10 +105,8 @@ public function message(Server $server, Frame $frame)
$handler = \bean($className);
$handler->onMessage($server, $frame);
} catch (\Throwable $e) {
/** @var \Swoft\Event\EventManager $em */
$em = App::getBean('eventManager');

if ($em->hasListenerQueue(WsEvent::ON_ERROR)) {
/** @see \Swoft\Event\EventManager::hasListenerQueue() */
if (App::hasBean('eventManager') && \bean('eventManager')->hasListenerQueue(WsEvent::ON_ERROR)) {
App::trigger(WsEvent::ON_ERROR, $frame, $e);
} else {
App::error($e->getMessage(), ['fd' => $fd, 'data' => $frame->data]);
Expand Down
2 changes: 1 addition & 1 deletion src/WebSocketContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public static function getFdByCoId()
* @param int|null $cid
* @return bool
*/
public static function delFdToCoId(int $cid = null): bool
public static function delFdByCoId(int $cid = null): bool
{
$cid = $cid > -1 ? $cid : self::getCoroutineId();

Expand Down
4 changes: 2 additions & 2 deletions src/WebSocketEventTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function onHandShake(Request $request, Response $response): bool
});

// delete coId to fd mapping
WebSocketContext::delFdToCoId();
WebSocketContext::delFdByCoId();

return true;
}
Expand Down Expand Up @@ -168,7 +168,7 @@ public function onMessage(Server $server, Frame $frame)
\bean('wsDispatcher')->message($server, $frame);

// delete coId to fd mapping
WebSocketContext::delFdToCoId();
WebSocketContext::delFdByCoId();
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/WebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function sendTo(int $receiver, string $data, int $sender = 0): int
}

/**
* broadcast message, will exclude self.
* broadcast message, will exclude sender.
* @param string $data 消息数据
* @param int $sender 发送者
* @param int[] $receivers 指定接收者们
Expand All @@ -194,15 +194,16 @@ public function broadcast(string $data, array $receivers = [], array $excluded =
return $this->sendTo((int)\array_shift($receivers), $data, $sender);
}

// excepted itself
if ($sender) {
$excluded[] = $sender;
}

// to all
if (!$excluded && !$receivers) {
return $this->sendToAll($data, $sender);
}

if ($sender) {
$excluded[] = $sender;
}

// to some
return $this->sendToSome($data, $receivers, $excluded, $sender);
}
Expand Down

0 comments on commit d614189

Please sign in to comment.