Skip to content

Commit 4f8e4a2

Browse files
committed
Initial commit
0 parents  commit 4f8e4a2

File tree

10 files changed

+434
-0
lines changed

10 files changed

+434
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/*
2+
composer.lock
3+
/vendor

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 Gergely Horvath
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Yii2 [WebSocketServer](/WebSocketServer.php)
2+
3+
#### This package is a fixed and reduced version of the original yii2-websocket which is at the time is not maintained.
4+
#### You can find the original repository here: https://github.com/consik/yii2-websocket
5+
6+
I made these changes simply, because i will need this in another project.
7+
I cannot provide documentation yet, there are quite a few changes.
8+
Your only option is to read the code yourself.
9+
There may be some kind of docs here in the future.
10+
11+
Because of an ongoing project, you can expect this repo to be maintained by me.
12+
13+
Also, the command functionality has been removed, because i found it unsafe to use, and I'm
14+
going to have a different approach implementing that inside my project.
15+
16+
Used [Ratchet](http://socketo.me/)
17+
18+
## Installation
19+
20+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
21+
22+
Either run
23+
24+
```
25+
composer require hunwalk/websocket
26+
```
27+
28+
or add
29+
30+
```json
31+
"hunwalk/websocket": "^1.0"
32+
```

WebSocketServer.php

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
/**
3+
* This package was originally maintained by Sergey Poltaranin
4+
* Due to to the fact that the repo is abandoned i recreated it.
5+
* You can find the old repo here: https://github.com/consik/yii2-websocket
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace hunwalk\websocket;
11+
12+
use Exception;
13+
use hunwalk\websocket\events\ExceptionEvent;
14+
use hunwalk\websocket\traits\WebsocketEventTrait;
15+
use Ratchet\ConnectionInterface;
16+
use Ratchet\Http\HttpServer;
17+
use Ratchet\MessageComponentInterface;
18+
use Ratchet\Server\IoServer;
19+
use Ratchet\WebSocket\WsServer;
20+
use SplObjectStorage;
21+
use yii\base\Component;
22+
use yii\base\InvalidConfigException;
23+
24+
class WebSocketServer extends Component implements MessageComponentInterface
25+
{
26+
use WebsocketEventTrait;
27+
28+
/**
29+
* @event yii\base\Event Triggered when binding is successfully completed
30+
*/
31+
const EVENT_WEBSOCKET_OPEN = 'ws_open';
32+
33+
/**
34+
* @event yii\base\Event Triggered when socket listening is closed
35+
*/
36+
const EVENT_WEBSOCKET_CLOSE = 'ws_close';
37+
38+
/**
39+
* @event ExceptionEvent Triggered when throwed Exception on binding socket
40+
*/
41+
const EVENT_WEBSOCKET_OPEN_ERROR = 'ws_open_error';
42+
43+
/**
44+
* @event ClientEvent Triggered when client connected to the server
45+
*/
46+
const EVENT_CLIENT_CONNECTED = 'ws_client_connected';
47+
48+
/**
49+
* @event WSClientErrorEvent Triggered when an error occurs on a Connection
50+
*/
51+
const EVENT_CLIENT_ERROR = 'ws_client_error';
52+
53+
/**
54+
* @event ClientEvent Triggered when client close connection with server
55+
*/
56+
const EVENT_CLIENT_DISCONNECTED = 'ws_client_disconnected';
57+
58+
/**
59+
* @event WSClientMessageEvent Triggered when message recieved from client
60+
*/
61+
const EVENT_CLIENT_MESSAGE = 'ws_client_message';
62+
63+
/**
64+
* @var int $port
65+
*/
66+
public $port = 8080;
67+
68+
/**
69+
* @var bool $closeConnectionOnError
70+
*/
71+
protected $closeConnectionOnError = true;
72+
73+
/**
74+
* @var IoServer|null $server
75+
*/
76+
protected $server = null;
77+
78+
/**
79+
* @var null|SplObjectStorage $clients
80+
*/
81+
protected $clients = null;
82+
83+
/**
84+
* @return bool
85+
* @event yii\base\Event EVENT_WEBSOCKET_OPEN
86+
* @event ExceptionEvent EVENT_WEBSOCKET_OPEN_ERROR
87+
* @throws InvalidConfigException
88+
*/
89+
public function start(): bool
90+
{
91+
try {
92+
$this->server = IoServer::factory(
93+
new HttpServer(
94+
new WsServer(
95+
$this
96+
)
97+
),
98+
$this->port
99+
);
100+
$this->trigger(self::EVENT_WEBSOCKET_OPEN);
101+
$this->clients = new SplObjectStorage();
102+
$this->server->run();
103+
104+
return true;
105+
106+
} catch (Exception $e) {
107+
$event = $this->getExceptionEvent($e);
108+
$this->trigger(self::EVENT_WEBSOCKET_OPEN_ERROR, $event);
109+
return false;
110+
}
111+
}
112+
113+
/**
114+
* @return void
115+
* @event yii\base\Event EVENT_WEBSOCKET_CLOSE
116+
*/
117+
public function stop(): void
118+
{
119+
$this->server->socket->close();
120+
$this->trigger(self::EVENT_WEBSOCKET_CLOSE);
121+
}
122+
123+
/**
124+
* @param ConnectionInterface $conn
125+
* @throws InvalidConfigException
126+
* @event ClientEvent EVENT_CLIENT_CONNECTED
127+
*/
128+
function onOpen(ConnectionInterface $conn): void
129+
{
130+
$event = $this->getClientEvent($conn);
131+
$this->trigger(self::EVENT_CLIENT_CONNECTED, $event);
132+
133+
$this->clients->attach($conn);
134+
}
135+
136+
/**
137+
* @param ConnectionInterface $conn
138+
* @throws InvalidConfigException
139+
* @event ClientEvent EVENT_CLIENT_DISCONNECTED
140+
*/
141+
function onClose(ConnectionInterface $conn): void
142+
{
143+
$event = $this->getClientEvent($conn);
144+
$this->trigger(self::EVENT_CLIENT_DISCONNECTED, $event);
145+
146+
$this->clients->detach($conn);
147+
}
148+
149+
/**
150+
* @param ConnectionInterface $conn
151+
* @param Exception $e
152+
* @throws InvalidConfigException
153+
* @event ClientErrorEvent EVENT_CLIENT_ERROR
154+
*/
155+
function onError(ConnectionInterface $conn, Exception $e): void
156+
{
157+
$event = $this->getClientErrorEvent($conn, $e);
158+
$this->trigger(self::EVENT_CLIENT_ERROR, $event);
159+
160+
if ($this->closeConnectionOnError) {
161+
$conn->close();
162+
}
163+
}
164+
165+
/**
166+
* @param ConnectionInterface $from
167+
* @param string $msg
168+
* @throws InvalidConfigException
169+
* @event ClientMessageEvent EVENT_CLIENT_MESSAGE
170+
*/
171+
function onMessage(ConnectionInterface $from, $msg): void
172+
{
173+
$event = $this->getClientMessageEvent($from, $msg);
174+
$this->trigger(self::EVENT_CLIENT_MESSAGE, $event);
175+
}
176+
177+
}

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "hunwalk/yii2-websocket",
3+
"description": "Yii2 websocket server component",
4+
"type": "yii2-extension",
5+
"keywords": ["yii2", "websocket", "component", "ratchet"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Sergey Poltaranin (Consik)",
10+
"email": "consigliere.kz@gmail.com"
11+
}
12+
],
13+
"require": {
14+
"yiisoft/yii2": "*",
15+
"cboden/ratchet": "^0.4"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"hunwalk\\websocket\\": ""
20+
}
21+
},
22+
"repositories": [
23+
{
24+
"type": "composer",
25+
"url": "https://asset-packagist.org"
26+
}
27+
]
28+
}

events/ClientErrorEvent.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace hunwalk\websocket\events;
6+
7+
use Exception;
8+
9+
class ClientErrorEvent extends ClientEvent
10+
{
11+
/**
12+
* @var Exception $exception
13+
*/
14+
public $exception;
15+
}

events/ClientEvent.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace hunwalk\websocket\events;
6+
7+
use Ratchet\ConnectionInterface;
8+
use yii\base\Event;
9+
10+
/**
11+
* Class ClientEvent
12+
* @package hunwalk\websocket\events
13+
*
14+
* @property ConnectionInterface $client
15+
*/
16+
class ClientEvent extends Event
17+
{
18+
/**
19+
* @var ConnectionInterface $_client
20+
*/
21+
private $_client;
22+
23+
/**
24+
* @return ConnectionInterface
25+
*/
26+
public function getClient(): ConnectionInterface
27+
{
28+
return $this->_client;
29+
}
30+
31+
/**
32+
* @param ConnectionInterface $client
33+
*/
34+
public function setClient(ConnectionInterface $client): void
35+
{
36+
$this->_client = $client;
37+
}
38+
}

events/ClientMessageEvent.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace hunwalk\websocket\events;
6+
7+
/**
8+
* Class ClientMessageEvent
9+
* @package hunwalk\websocket\events
10+
*
11+
* @property string $message
12+
*/
13+
class ClientMessageEvent extends ClientEvent
14+
{
15+
/**
16+
* @var string $message
17+
*/
18+
private $_message;
19+
20+
/**
21+
* @return string
22+
*/
23+
public function getMessage(): string
24+
{
25+
return $this->_message;
26+
}
27+
28+
/**
29+
* @param $message
30+
*/
31+
public function setMessage($message): void
32+
{
33+
$this->_message = $message;
34+
}
35+
}

events/ExceptionEvent.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace hunwalk\websocket\events;
6+
7+
use Exception;
8+
use yii\base\Event;
9+
10+
/**
11+
* Class ExceptionEvent
12+
* I'm not really going to place here getters and setters.
13+
* This is fine as it is.
14+
* @package hunwalk\websocket\events
15+
*/
16+
class ExceptionEvent extends Event
17+
{
18+
/**
19+
* @var Exception $exception
20+
*/
21+
public $exception;
22+
}

0 commit comments

Comments
 (0)