Skip to content

Commit

Permalink
完善socketio
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Jan 21, 2021
1 parent e9b872c commit 474c38d
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 187 deletions.
54 changes: 26 additions & 28 deletions src/Websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function onOpen($fd, Request $request)
*/
public function onMessage(Frame $frame)
{
$this->event->trigger("swoole.websocket.Message", $frame);
$this->event->trigger("swoole.websocket.Event", $this->decode($frame->data));
}

Expand Down Expand Up @@ -172,33 +173,7 @@ public function leave($rooms = []): self
return $this;
}

protected function encode(string $event, $data)
{
return json_encode([
'type' => $event,
'data' => $data,
]);
}

protected function decode($payload)
{
$data = json_decode($payload, true);

return [
'type' => $data['type'] ?? null,
'data' => $data['data'] ?? null,
];
}

/**
* Emit data and reset some status.
*
* @param string
* @param mixed
*
* @return boolean
*/
public function emit(string $event, $data = null): bool
public function push($data)
{
$fds = $this->getFds();
$assigned = !empty($this->getTo());
Expand All @@ -213,7 +188,7 @@ public function emit(string $event, $data = null): bool
'descriptors' => $fds,
'broadcast' => $this->isBroadcast(),
'assigned' => $assigned,
'payload' => $this->encode($event, $data),
'payload' => $data,
]);

$result = $this->server->task($job);
Expand All @@ -224,6 +199,29 @@ public function emit(string $event, $data = null): bool
}
}

public function emit(string $event, $data = null): bool
{
return $this->push($this->encode([
'type' => $event,
'data' => $data,
]));
}

protected function encode($packet)
{
return json_encode($packet);
}

protected function decode($payload)
{
$data = json_decode($payload, true);

return [
'type' => $data['type'] ?? null,
'data' => $data['data'] ?? null,
];
}

/**
* Close current connection.
*
Expand Down
42 changes: 0 additions & 42 deletions src/contract/websocket/HandlerInterface.php

This file was deleted.

81 changes: 81 additions & 0 deletions src/websocket/socketio/EnginePacket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace think\swoole\websocket\socketio;

class EnginePacket
{
/**
* Engine.io packet type `open`.
*/
const OPEN = 0;

/**
* Engine.io packet type `close`.
*/
const CLOSE = 1;

/**
* Engine.io packet type `ping`.
*/
const PING = 2;

/**
* Engine.io packet type `pong`.
*/
const PONG = 3;

/**
* Engine.io packet type `message`.
*/
const MESSAGE = 4;

/**
* Engine.io packet type 'upgrade'
*/
const UPGRADE = 5;

/**
* Engine.io packet type `noop`.
*/
const NOOP = 6;

public $type;

public $data = '';

public function __construct($type, $data = '')
{
$this->type = $type;
$this->data = $data;
}

public static function open($payload)
{
return new static(self::OPEN, $payload);
}

public static function pong($payload = '')
{
return new static(self::PONG, $payload);
}

public static function ping()
{
return new static(self::PING);
}

public static function message($payload)
{
return new static(self::MESSAGE, $payload);
}

public static function fromString(string $packet)
{
return new static($packet{0}, substr($packet, 1) ?? '');
}

public function toString()
{
return $this->type . $this->data;
}
}
Loading

0 comments on commit 474c38d

Please sign in to comment.