Events.php 无法获取 start_gateway.php 传递过来的$_session

0

在start_gateway.php里面

在event.php里面无法获取到$_SESSION['realIP'],得到的是null。请问怎么才能在event.php里面得到这个session

已邀请:

th

赞同来自:

$connection有onWebSocketConnect方法吗?是不是搞错了

walkor

赞同来自:

gatewayWorker 3.x才支持这个特性。你的2.0.3版本太老了。

dingdejing

赞同来自:

截图


在这里打印拿到了,但是在Event.php 仍然拿不到


版本3.x

dingdejing

赞同来自:

截图


在这里回复吧,不然传不了照片,


use Workerman\Worker;
use Workerman\Connection\ConnectionInterface;

class Websocket extends \Workerman\Protocols\Websocket {
/**
* Websocket encode.
*
* @param string $buffer
* @param ConnectionInterface $connection
* @return string
*/
public static function encode($buffer, ConnectionInterface $connection) {
if (!is_scalar($buffer)) {
throw new \Exception("You can't send(" . gettype($buffer) . ") to client, you need to convert it to a string. ");
}

if (empty($connection->websocketType)) {
$connection->websocketType = static::BINARY_TYPE_BLOB;
}

$first_byte = $connection->websocketType;

$beforeLen = strlen($buffer);
if ($beforeLen > 8) {
$first_byte = chr(ord($first_byte) | 64);
$buffer = $connection->encodeDeflate($buffer);
$afterLen = strlen($buffer);
echo "encodeSaveBytes:\t" . ($beforeLen - $afterLen) . "\t$afterLen\n";
}

$len = \strlen($buffer);

if ($len <= 125) {
$encode_buffer = $first_byte . \chr($len) . $buffer;
} else {
if ($len <= 65535) {
$encode_buffer = $first_byte . \chr(126) . \pack("n", $len) . $buffer;
} else {
$encode_buffer = $first_byte . \chr(127) . \pack("xxxxN", $len) . $buffer;
}
}

// Handshake not completed so temporary buffer websocket data waiting for send.
if (empty($connection->websocketHandshake)) {
if (empty($connection->tmpWebsocketData)) {
$connection->tmpWebsocketData = '';
}
// If buffer has already full then discard the current package.
if (\strlen($connection->tmpWebsocketData) > $connection->maxSendBufferSize) {
if ($connection->onError) {
try {
\call_user_func($connection->onError, $connection, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
} catch (\Exception $e) {
Worker::log($e);
exit(250);
} catch (\Error $e) {
Worker::log($e);
exit(250);
}
}
return '';
}
$connection->tmpWebsocketData .= $encode_buffer;
// Check buffer is full.
if ($connection->maxSendBufferSize <= \strlen($connection->tmpWebsocketData)) {
if ($connection->onBufferFull) {
try {
\call_user_func($connection->onBufferFull, $connection);
} catch (\Exception $e) {
Worker::log($e);
exit(250);
} catch (\Error $e) {
Worker::log($e);
exit(250);
}
}
}

// Return empty string.
return '';
}

return $encode_buffer;
}

/**
* Websocket decode.
*
* @param string $buffer
* @param ConnectionInterface $connection
* @return string
*/
public static function decode($buffer, ConnectionInterface $connection) {
$len = \ord($buffer[1]) & 127;
if ($len === 126) {
$masks = \substr($buffer, 4, 4);
$data = \substr($buffer, 8);
} else {
if ($len === 127) {
$masks = \substr($buffer, 10, 4);
$data = \substr($buffer, 14);
} else {
$masks = \substr($buffer, 2, 4);
$data = \substr($buffer, 6);
}
}
$dataLength = \strlen($data);
$masks = \str_repeat($masks, \floor($dataLength / 4)) . \substr($masks, 0, $dataLength % 4);
$decoded = $data ^ $masks;

if ((\ord($buffer[0]) & 64) === 64) {
$beforeLen = strlen($decoded);
$decoded = $connection->decodeDeflate($decoded);
echo "decodeSaveBytes:\t" . (strlen($decoded) - $beforeLen) . "\t$beforeLen\n";
}

if ($connection->websocketCurrentFrameLength) {
$connection->websocketDataBuffer .= $decoded;
return $connection->websocketDataBuffer;
} else {
if ($connection->websocketDataBuffer !== '') {
$decoded = $connection->websocketDataBuffer . $decoded;
$connection->websocketDataBuffer = '';
}
return $decoded;
}
}

/**
* Websocket handshake.
*
* @param string $buffer
* @param \Workerman\Connection\TcpConnection $connection
* @return int
*/
protected static function dealHandshake($buffer, $connection) {
// HTTP protocol.
if (0 === \strpos($buffer, 'GET')) {
// Find \r\n\r\n.
$heder_end_pos = \strpos($buffer, "\r\n\r\n");
if (!$heder_end_pos) {
return 0;
}
$header_length = $heder_end_pos + 4;

// Get Sec-WebSocket-Key.
$Sec_WebSocket_Key = '';
if (\preg_match("/Sec-WebSocket-Key: *(.*?)\r\n/i", $buffer, $match)) {
$Sec_WebSocket_Key = $match[1];
} else {
$connection->send("HTTP/1.1 200 Websocket\r\nServer: workerman/" . Worker::VERSION . "\r\n\r\n<div style=\"text-align:center\"><h1>Websocket</h1><hr>powerd by <a href=\"https://www.workerman.net\">workerman " . Worker::VERSION . "</a></div>",
true);
$connection->close();
return 0;
}
// Calculation websocket key.
$new_key = \base64_encode(sha1($Sec_WebSocket_Key . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));
// Handshake response data.
$handshake_message = "HTTP/1.1 101 Switching Protocols\r\n";
$handshake_message .= "Upgrade: websocket\r\n";
$handshake_message .= "Sec-WebSocket-Version: 13\r\n";
$handshake_message .= "Connection: Upgrade\r\n";
$handshake_message .= "Sec-WebSocket-Accept: " . $new_key . "\r\n";

$Sec_WebSocket_Extensions = '';
if (preg_match("/Sec-WebSocket-Extensions: *(.*?)\r\n/i", $buffer, $match)) {
$Sec_WebSocket_Extensions = $match[1];
$extensions = array_map("trim", explode(';', $Sec_WebSocket_Extensions));
$mapExtension = [];
foreach ($extensions as $extension) {
$items = array_map("trim", explode('=', $extension));
if (count($items) === 1) {
$mapExtension[$items[0]] = 0;
} elseif (count($items) === 2) {
$mapExtension[$items[0]] = $items[1];
}
}
// no_context_takeover 先不实现
if (!isset($mapExtension['server_no_context_takeover']) && !isset($mapExtension['client_no_context_takeover'])) {
if (isset($mapExtension['permessage-deflate'])) {
$handshake_message .= "Sec-WebSocket-Extensions: permessage-deflate";
$server_max_window_bits = 15;
$client_max_window_bits = 15;
if (isset($mapExtension['server_max_window_bits'])) {
if (!empty($mapExtension['server_max_window_bits'])) {
$server_max_window_bits = $mapExtension['server_max_window_bits'];
}
}
if (isset($mapExtension['client_max_window_bits'])) {
if (!empty($mapExtension['client_max_window_bits'])) {
$client_max_window_bits = $mapExtension['client_max_window_bits'];
}
}
$handshake_message .= "\r\n";
$connection->initDeflate($server_max_window_bits, $client_max_window_bits);
}
}
}

// Websocket data buffer.
$connection->websocketDataBuffer = '';
// Current websocket frame length.
$connection->websocketCurrentFrameLength = 0;
// Current websocket frame data.
$connection->websocketCurrentFrameBuffer = '';
// Consume handshake data.
$connection->consumeRecvBuffer($header_length);

// blob or arraybuffer
if (empty($connection->websocketType)) {
$connection->websocketType = static::BINARY_TYPE_BLOB;
}

$has_server_header = false;

// Try to emit onWebSocketConnect callback.
if (isset($connection->onWebSocketConnect) || isset($connection->worker->onWebSocketConnect)) {
static::parseHttpHeader($buffer);
try {
\call_user_func(isset($connection->onWebSocketConnect) ? $connection->onWebSocketConnect : $connection->worker->onWebSocketConnect, $connection, $buffer);
} catch (\Exception $e) {
Worker::log($e);
exit(250);
} catch (\Error $e) {
Worker::log($e);
exit(250);
}
if (!empty($_SESSION) && \class_exists('\GatewayWorker\Lib\Context')) {
$connection->session = \GatewayWorker\Lib\Context::sessionEncode($_SESSION);
}
$_GET = $_SERVER = $_SESSION = $_COOKIE = array();

if (isset($connection->headers)) {
if (\is_array($connection->headers)) {
foreach ($connection->headers as $header) {
if (\strpos($header, 'Server:') === 0) {
$has_server_header = true;
}
$handshake_message .= "$header\r\n";
}
} else {
$handshake_message .= "$connection->headers\r\n";
}
}
}
if (!$has_server_header) {
$handshake_message .= "Server: workerman/" . Worker::VERSION . "\r\n";
}
$handshake_message .= "\r\n";
// Send handshake response.
$connection->send($handshake_message, true);
// Mark handshake complete..
$connection->websocketHandshake = true;
// There are data waiting to be sent.
if (!empty($connection->tmpWebsocketData)) {
$connection->send($connection->tmpWebsocketData, true);
$connection->tmpWebsocketData = '';
}
if (\strlen($buffer) > $header_length) {
return static::input(\substr($buffer, $header_length), $connection);
}
return 0;
} // Is flash policy-file-request.
elseif (0 === \strpos($buffer, '<polic')) {
$policy_xml = '<?xml version="1.0"?><cross-domain-policy><site-control permitted-cross-domain-policies="all"/><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>' . "\0";
$connection->send($policy_xml, true);
$connection->consumeRecvBuffer(\strlen($buffer));
return 0;
}
// Bad websocket handshake request.
$connection->send("HTTP/1.1 200 Websocket\r\nServer: workerman/" . Worker::VERSION . "\r\n\r\n<div style=\"text-align:center\"><h1>Websocket</h1><hr>powerd by <a href=\"https://www.workerman.net\">workerman " . Worker::VERSION . "</a></div>",
true);
$connection->close();
return 0;
}
}

要回复问题请先登录注册