Skip to content

Commit 75f9ad9

Browse files
committed
Fixed unknown socket resource problem and setted close socket connection method in socket handlers destructor methods
1 parent d377e16 commit 75f9ad9

File tree

4 files changed

+63
-25
lines changed

4 files changed

+63
-25
lines changed

src/Client.php

+29-19
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,71 @@
1-
<?php
1+
<?php
22

33
namespace WebSocket;
44

55
class Client extends SocketEndpoint implements ISocketImplementer
66
{
77
/**
8-
* Create socket using connectToSocket
9-
* method
10-
* @var resource
8+
* Socket resource created using
9+
* acceptSocketConnection method
10+
* @var resource
1111
*/
12-
private $createdSocket;
12+
protected $createdSocket;
1313

1414
/**
15-
* Create connection for server socket
15+
* Accept server socket connection
1616
* @return resource|\WebSocket\SocketError
1717
*/
1818
public function create()
1919
{
2020
$this->createdSocket = $this->connectToSocket();
2121

2222
if (!$this->createdSocket) {
23-
return new SocketError('Can\'t create and connect to socket');
23+
return new SocketError('Can\'t accepted connected socket');
2424
}
2525

2626
return $this->createdSocket;
2727
}
2828

2929
/**
30-
* Read message from created server socket
31-
* @return \WebSocket\SocketError|WebSocket\SocketMessage
30+
* Read message from accepted server socket
31+
* @return \WebSocket\SocketError|\WebSocket\SocketMessage
3232
*/
3333
public function read()
3434
{
35-
$responseFromSocket = new SocketMessage(
35+
$socketResponse = new SocketMessage(
3636
$this->readFromSocket($this->createdSocket)
3737
);
3838

39-
if (!$responseFromSocket->getSocketResponse()) {
40-
return new SocketError('Can\'t read from server socket');
39+
if (!$socketResponse->getSocketResponse()) {
40+
return new SocketError('Can\'t read message from client socket');
4141
}
4242

43-
return $responseFromSocket;
43+
return $socketResponse;
4444
}
4545

4646
/**
47-
* Write message to created server socket
48-
* @param string $message
47+
* Write message to server accepted socket
48+
* @param string $message
4949
* @return int|\WebSocket\SocketError
5050
*/
5151
public function write(string $message)
5252
{
53-
$writtenSocket = $this->write($message);
53+
$writtenMessage = $this->writeToSocket($this->createdSocket, $message);
5454

55-
if (!$writtenSocket) {
56-
return new SocketError('Can\'t write to server socket');
55+
if (!$writtenMessage) {
56+
return new SocketError('Can\'t write to accepted client socket');
5757
}
5858

59-
return $writtenSocket;
59+
return $writtenMessage;
60+
}
61+
62+
/**
63+
* Call Client destructor method when socket
64+
* connection was broken
65+
* @return void
66+
*/
67+
public function __destruct()
68+
{
69+
return $this->closeSocketConnection($this->createdSocket);
6070
}
6171
}

src/Server.php

+10
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,14 @@ public function write(string $message)
5858

5959
return $writtenMessage;
6060
}
61+
62+
/**
63+
* Call Server destructor method when socket
64+
* connection was broken
65+
* @return void
66+
*/
67+
public function __destruct()
68+
{
69+
return $this->closeSocketConnection($this->createdSocket);
70+
}
6171
}

src/Socket.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class Socket
2727
*/
2828
protected string $content = '';
2929

30+
protected $clientSocket;
31+
32+
protected $serverSocket;
33+
34+
protected $createdSocket;
35+
3036
/**
3137
* Socket constructor method
3238
* @param string $host
@@ -121,7 +127,7 @@ public function connect()
121127
$this->socket, $this->host, $this->port
122128
);
123129

124-
return $this;
130+
return $this->socket;
125131
}
126132

127133
/**
@@ -151,11 +157,12 @@ public function read($socket)
151157
}
152158

153159
/**
154-
* Socket detructor method which
155-
* close current listening socket
160+
* Close specified socket connection
161+
* @param resource $socket
162+
* @return void
156163
*/
157-
public function __destruct()
164+
public function close($socket)
158165
{
159-
socket_close($this->socket);
166+
return socket_close($socket);
160167
}
161168
}

src/SocketEndpoint.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function acceptConnectionOnSocket()
5858
*/
5959
protected function connectToSocket()
6060
{
61-
return $this->getSocket()->connect()->getConnectedSocket();
61+
return $this->getSocket()->connect();
6262
}
6363

6464
/**
@@ -81,4 +81,15 @@ protected function readFromSocket($socket)
8181
{
8282
return $this->getSocket()->read($socket);
8383
}
84+
85+
/**
86+
* Close socket connection which was created
87+
* acceptSocketConnection() or connectToSocket()
88+
* @param resource $socket
89+
* @return void
90+
*/
91+
protected function closeSocketConnection($socket)
92+
{
93+
return $this->getSocket()->close($socket);
94+
}
8495
}

0 commit comments

Comments
 (0)