Skip to content

Commit 4a61f37

Browse files
committed
Change WebSocket library to nv-websocket-client
1 parent bacf85d commit 4a61f37

File tree

16 files changed

+1004
-264
lines changed

16 files changed

+1004
-264
lines changed

README.MD

Lines changed: 175 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,194 @@
33
```
44
jppm add jphp-websocket-client
55
```
6-
## Примеры
6+
## Пример
77
```php
88
use php\net\websocket\WebSocket;
9+
use php\net\websocket\WebSocketFrame;
10+
use php\net\websocket\WebSocketException;
911

10-
// Инициализация
11-
$client = new WebSocket();
12-
$client->url = 'wss://example.org/';
13-
$client->addHeader('Authorization', 'Bearer abcdefgh0123456789');
14-
15-
// События
16-
$client->on('open', function(int $code, string $message, array $headers){
17-
echo "Соединение установлено!\n";
12+
$ws = new WebSocket; // Создаём WebSocket
13+
$ws->url = 'ws://127.0.0.1:8888/simple_server/'; // Задаём url
14+
$ws->connectionTimeout = 5000; // Задаём максимальное время подключения
15+
// Настраиваем прокси
16+
$ws->proxyHost = '159.183.31.220';
17+
$ws->proxyPort = 3128;
18+
$ws->proxyPassword = 'sqrt322';
19+
// Навешиваем обработчики для событий
20+
$ws->on('stateChanged', function(string $state){
21+
echo "stateChanged: {$state}\n";
22+
});
23+
$ws->on('connected', function(array $headers)use($ws){
24+
//var_dump($headers);
25+
echo "Successful connection\n";
26+
$ws->sendText('hey');
27+
});
28+
$ws->on('textMessage', function(string $message)use($ws){
29+
echo "Input message: {$message}\n";
30+
if($message == 'bye'){
31+
$ws->sendText('see you later :)');
32+
}
33+
});
34+
$ws->on('connectError', function(WebSocketException $e){
35+
echo "Connection error: {$e->getMessage()}\n";
36+
});
37+
$ws->on('disconnected', function(WebSocketFrame $serverCloseFrame){
38+
echo "WebSocket closed by reason: {$serverCloseFrame->closeReason}\n";
1839
});
19-
$client->on('message', function(string $message){
20-
echo "Получено сообщение: {$message}\n";
40+
$ws->on('sendingHandshake', function($request, $headers){
41+
echo "sendingHandshake({$request}):\n";
42+
//var_dump($headers);
2143
});
22-
$client->on('close', function(int $code, string $message, bool $remote){
23-
echo "Соединение закрыто({$code}-{$message})\n";
44+
$ws->connectAsynchronously(); // Подключаемся
45+
```
46+
## События
47+
### connected
48+
```php
49+
$ws->on("connected", function(array $headers){
50+
// Вызывается после успешного подключения.
2451
});
25-
$client->on('error', function(\Exception $e){
26-
echo "Ошибка - {$e->getMessage()}\n";
52+
```
53+
### connectError
54+
```php
55+
$ws->on("connectError", function(WebSocketException $cause){
56+
// Вызывается после неудачного вызова connectAsynchronously.
57+
});
58+
```
59+
### stateChanged
60+
```php
61+
$ws->on("stateChanged", function(string $state){
62+
// Вызывается при смене состояния WebSocket`а. Возможные значения:
63+
// CREATED, CONNECTING, OPEN, CLOSING, CLOSED
64+
});
65+
```
66+
### disconnected
67+
```php
68+
$ws->on("disconnected", function(WebSocketFrame $serverCloseFrame, WebSocketFrame $clientCloseFrame, bool $closedByServer){
69+
// Вызывается после завершения подключения WebSocket`а.
70+
});
71+
```
72+
### frame
73+
```php
74+
$ws->on("frame", function(WebSocketFrame $frame){
75+
// Вызывается при получении фрейма.
76+
});
77+
```
78+
### continuationFrame
79+
```php
80+
$ws->on("continuationFrame", function(WebSocketFrame $frame){
81+
// Вызывается при получении "продолжительного" фрейма(fin=0).
82+
});
83+
```
84+
### textFrame
85+
```php
86+
$ws->on("textFrame", function(WebSocketFrame $frame){
87+
// Вызывается при получении текстового фрейма.
88+
});
89+
```
90+
### textMessage
91+
```php
92+
$ws->on("textMessage", function(string $message){
93+
// Вызывается при получении текстового сообщения.
94+
});
95+
```
96+
### binaryFrame
97+
```php
98+
$ws->on("binaryFrame", function(WebSocketFrame $frame){
99+
// Вызывается при получении бинарного фрейма.
100+
});
101+
```
102+
### pingFrame
103+
```php
104+
$ws->on("pingFrame", function(WebSocketFrame $frame){
105+
// Вызывается при получении "пинг" фрейма(для проверки подключения).
106+
});
107+
```
108+
### pongFrame
109+
```php
110+
$ws->on("pongFrame", function(string $message){
111+
// Вызывается при получении текстового сообщения.
112+
});
113+
```
114+
### sendingFrame
115+
```php
116+
$ws->on("sendingFrame", function(WebSocketFrame $frame){
117+
// Вызывается при отправлении фрейма.
118+
});
119+
```
120+
### frameSent
121+
```php
122+
$ws->on("frameSent", function(WebSocketFrame $frame){
123+
// Вызывается после успешного отправления фрейма.
124+
});
125+
```
126+
### frameUnsent
127+
```php
128+
$ws->on("frameUnsent", function(WebSocketFrame $frame){
129+
// Вызывается после ошибки при отправлении фрейма.
130+
});
131+
```
132+
### threadCreated
133+
```php
134+
$ws->on("threadCreated", function(string $threadType, php\lang\Thread $thread){
135+
// Вызывается при создании нового потока. Возможные значения для threadType:
136+
// READING_THREAD, WRITING_THREAD, CONNECT_THREAD, FINISH_THREAD
137+
});
138+
```
139+
### threadStarted
140+
```php
141+
$ws->on("threadStarted", function(string $threadType, php\lang\Thread $thread){
142+
// Вызывается при запуске созданного потока.
143+
});
144+
```
145+
### threadStopping
146+
```php
147+
$ws->on("threadStopping", function(string $threadType, php\lang\Thread $thread){
148+
// Вызывается при остановке созданного потока.
149+
});
150+
```
151+
### error
152+
```php
153+
$ws->on("error", function(WebSocketException $error){
154+
// Вызывается при ошибке.
155+
});
156+
```
157+
### frameError
158+
```php
159+
$ws->on("frameError", function(WebSocketException $error, WebSocketFrame $frame){
160+
// Вызывается при ошибке чтения фрейма.
161+
});
162+
```
163+
### messageError
164+
```php
165+
$ws->on("messageError", function(WebSocketException $error, array $frames){
166+
// Вызывается при ошибке построения сообщения.
167+
});
168+
```
169+
### sendError
170+
```php
171+
$ws->on("sendError", function(WebSocketException $error, WebSocketFrame $frame){
172+
// Вызывается при ошибке отправки фрейма.
173+
});
174+
```
175+
### unexpectedError
176+
```php
177+
$ws->on("unexpectedError", function(WebSocketException $error){
178+
// Вызывается при неожиданной ошибке.
179+
});
180+
```
181+
### sendingHandshake
182+
```php
183+
$ws->on("sendingHandshake", function(string $requestLine, array $headers){
184+
// Вызывается перед установкой соединения.
27185
});
28-
$client->connect();
29-
// $client->connectAsync();
30186
```
31187

32188
## Дополнительно
33189

34190
[Расширение для DevelNext](https://github.com/jphp-group/jphp-websocket-client/releases/latest)
35191

192+
[Оригинальная библиотека](https://github.com/TakahikoKawasaki/nv-websocket-client)
193+
36194
[Venity](https://vk.com/venity)
37195

38196
[DevelNext - extensions & manuals.](https://vk.com/dn_extension)

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: "java"
22
apply plugin: "idea"
33
apply plugin: "eclipse"
44

5-
version '1.0.6'
5+
version '1.1.4'
66

77
compileJava {
88
sourceCompatibility = '1.8'
@@ -26,10 +26,10 @@ configurations { provided }
2626
sourceSets { main { compileClasspath += configurations.provided } }
2727

2828
dependencies {
29-
provided files('jars/x-Java-WebSocket-1.3.8.jar')
30-
provided files('jars/x-jphp-websocket-client-1.0.6.jar')
29+
provided files('jars/x-jphp-websocket-client-1.1.4.jar')
30+
provided files('jars/x-nv-websocket-client-2.6.jar')
3131
provided files('vendor/jphp-runtime/jars/jphp-runtime-1.0.3.jar')
32-
compile 'org.java-websocket:Java-WebSocket:1.3.8'
32+
compile 'com.neovisionaries:nv-websocket-client:2.6'
3333
}
3434

3535
jar {

package-lock.php.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
dependencies:
22
jphp-runtime:
3-
version: 1.0.4
3+
version: 1.0.7
44
info:
5-
sha256: ba9ec5bc7e795cee9ebf2e0cb0e7aade9cbeb6d5e603d67e1aee435987e5ffca
6-
size: 1816951
5+
sha256: 36ed7baf5e61332446d93c67354e5fa4d159dd71a19443208fbed12ee44cf962
6+
size: 1817288
77
repo: http://api.develnext.org
88
jphp-core:
99
version: 1.0.4

package.php.yml

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: jphp-websocket-client
2-
version: 1.0.6
2+
version: 1.1.4
33

44
deps:
55
jphp-runtime: '*'
@@ -10,7 +10,7 @@ devDeps:
1010

1111
gradle:
1212
deps:
13-
- "org.java-websocket:Java-WebSocket:1.3.8"
13+
- 'com.neovisionaries:nv-websocket-client:2.6'
1414

1515
plugins:
1616
- Gradle
@@ -19,23 +19,30 @@ plugins:
1919

2020
config:
2121
ignore:
22-
- /sandbox/**
23-
- /.idea/**
24-
- /*.iml
25-
- /.git/**
26-
- /package.hub.yml
27-
- /bundle/**
28-
- /src-bundle/**
29-
- /build
30-
- /jars/**
31-
- /out/**
32-
- /dn-sources/**
22+
- /sandbox/**
23+
- /.idea/**
24+
- /*.iml
25+
- /.git/**
26+
- /package.hub.yml
27+
- /bundle/**
28+
- /src-bundle/**
29+
- /build
30+
- /out/**
31+
- /examples/**
32+
- /screenshots/**
33+
- /dn-sources/**
34+
- /.gitignore
35+
- /settings.gradle
3336

3437
develnext-bundle:
35-
version: 1.0.4
38+
version: 1.1.6
3639
name: "WebSocket Client"
3740
author: "broelik"
3841
icon: "develnext/bundle/wsclient/icon32.png"
3942
description: "Пакет для работы с WebSocket"
4043
group: "network"
41-
class: "develnext\\bundle\\wsclient\\WebSocketClientBundle"
44+
class: "develnext\\bundle\\wsclient\\WebSocketClientBundle"
45+
46+
history:
47+
1.1.4:
48+
- Change WebSocket library to nv-websocket-client

0 commit comments

Comments
 (0)