Comments (8)
You need to check if the third parameter $reactorId
in the onClose
event is -1 to determine if the connection was closed by the client or the server.
from swoole-src.
@NathanFreeman
I echoed third parameter like below:
$this->server->on('Close', function($server, $fd, $reactorId) {
echo "client {$fd} closed\n ReactorId:{$reactorId}";
});
and got:
client 1 closed
ReactorId:2
In order to keep a connection opened, do i need to push() (stream) some data continously from server-side (or from the client-side) by puting push() (or send() ) inside a loop ? If the answer is "Yes" then what i will take from this is that push() only sends the data (from the server to the client) and then websocket connection is closed after every push.
from swoole-src.
$reactorId greater than 0 indicates that the client has actively closed the connection. You need to check the client's code.
from swoole-src.
@NathanFreeman
I think that the issue is in int $flags of the Server::pack() function.
For the (WebSocket's) client-side, I am using this gist (with swoole version 5.1.2)
https://gist.github.com/NHZEX/946275e9b32a832d579a36c5d3d0b7fc
If you just look at this code, the $this->socket->send() function is passing four parameters to Server::pack() (as shown in function below at the end, so initially it was not working). I removed the fourth parameter in order to make the code work (by conforming to the definition of Server::pack(), as per documentation), however i found two things that;
A) That, the third parameter (which is int $flags) work only for value "true", or non-zero value, but not for "false" or 0 ) which means i can not set $flag a value other than "WEBSOCKET_FLAG_FIN" (same is also on server-side) and ...
B) That, the client connection is closed after push() (as reported in this Issue)
So it looks like that the problem lies in how you deal internally with the value of the argument int $flags
(as passed in the function Server::pack() to make a Frame object).
//Original Function
public function send($data, $type = 'text', $masked = false)
{
switch ($type) {
case 'text':
$_type = WEBSOCKET_OPCODE_TEXT;
break;
case 'binary':
case 'bin':
$_type = WEBSOCKET_OPCODE_BINARY;
break;
case 'ping':
$_type = WEBSOCKET_OPCODE_PING;
break;
default:
return false;
}
return $this->socket->send(Server::pack($data, $_type, true, $masked));
}
In code above, i changed ...
// see, four parameters (may be because it is code targeting older version of swoole)
return $this->socket->send(Server::pack($data, $_type, true, $masked));
to ...
// Three parameters instead of four
return $this->socket->send(Server::pack($data, $_type, true));
It works with only "true" and "non-zero" values not with "false" and 0. (and parhaps passing the "true" and "non-zero" value also disconnects the connection, just as you say that disconnection is coming from client-side of code; May be this is because when we pass "true" or "non-zero" value then it is same as the default value of int $flags (which is "WEBSOCKET_FLAG_FIN"), whereas if i try to pass a Zero value or a boolean false, i get error as it takes the flow of code into ....
if ($data === false) { }
, condition inside function recv()
of this gist.
So, again the question is:
How to use server::pack(), and push() (with new type of paramater "int $flags") such that the WebSocker connection do not auto disconnect ?
from swoole-src.
Any update on this issue ? Please, also see my last reply.
@NathanFreeman @matyhtf @twose
from swoole-src.
I have found that there comes an issue in your client-side recv() function of socket object ...
$this->socket->recv();
When server-side pushes all the messages, the function recv() returns not only "false" (false is just fine, as per documentation) but it also issues a warning as below:
PHP Warning: Swoole\Client::recv(): recv() failed, Error: Resource temporarily unavailable[11] in /var/www/html/swoole-prac/websocketclient/wbsocketclient.php on line 87"
And, it also gives a warning that there is no property $errMsg on socket object.
Reference to Client-side Code:
Please, see on line 85 of this gist which i am using now.
from swoole-src.
Same problem after update from v4.8.12 to 5.1.3, push($fd, $data) works every other time.
from swoole-src.
Way around:
-
I think
push()
on server-side is working fine (i am using 5.1.2, i have not tested on 5.1.3). The issue of this PHP Warning and Error Code 11 occurs both on Swoole and OpenSwoole (i tested on both). -
In order to avoid disconnection, we need to put the function
recv()
which contains the command$this->socket->recv();
inside awhile(true) { ... }
loop (in its consumer side code). -
In order to ignore / suppress the PHP Warning on your TCP-Client code use the sign
@
just before$this->socket->recv();
to make it as@$this->socket->recv();
Note: This PHP warning which comes only when there is no data pushed in socket.
here $this->socket
is $this->socket = Swoole\Client(SWOOLE_SOCK_TCP); // may be different variable in your code
- Now from server-side if you send the data using
push()
repeatedly (like, by putingpush()
inside a loop), the client-side will show the data. As a test, i calledpush()
twice with a co:sleep(10) in the middle.
FYI.
@NathanFreeman @matyhtf @twose
from swoole-src.
Related Issues (20)
- Cancel Coroutine ! HOT 1
- WARNING Server::check_worker_exit_status(): worker(pid=8162, id=13) abnormal exit, status=0, signal=11 HOT 2
- Manage connections at 1 second intervals from within the websocket task, close connections etc. HOT 2
- PHP Fatal error: Cannot redeclare Swoole\Coroutine\run() (previously declared in @swoole/library/core/Coroutine/functions.php:16) in [my_project_folder]/vendor/swoole/library/src/core/Coroutine/functions.php on line 24 HOT 3
- https://swoole.com/download 页面的文件均无法下载 HOT 1
- problem with variable value HOT 4
- 6.0开启线程,打开文件过多 HOT 5
- v6.0线程的Swoole\Thread\Map的bug HOT 6
- 开启 HTTP2 时,$request->server['request_method'] 与 $request->getMethod() 的结果不一致。 HOT 3
- swoole5.1.3 运行时异常退出 HOT 4
- Critically Urgent: Cloud Deployment Architecture for Swoole in Multi-process / Multi-threaded Mode HOT 1
- swoole v6.0.0-dev 版本的swoole-cli 异常 HOT 2
- V6.0 配置了bootstrap后 没有执行$server->on和$server->start 事件就会失效 HOT 2
- Server deadlock when using max_concurrency HOT 3
- Swoole Tracker3.1发布,支持完善的内存泄漏检测!请问现在找不到Swoole Tracker 下载链接,是不提供该扩展了么? HOT 1
- v6.0 lock 多线程锁性能提升的一个方案 HOT 1
- php8.3 swoole5.1.3 kernel: php[1048884]: segfault at 48 ip 00007f82db25f197 sp 00007ffc466aa9c0 error 4 in swoole.so[7f82db1df000+1a2000] HOT 14
- undefined method Swoole\Thread::exec() HOT 4
- websocket服务器为什么在onclose事件中不提供closeCode HOT 2
- php8.3 swoole5.1.3 TaskWorker_onPipeReceive(): read(1056, 8192) failed,ReactorEpoll::wait(): EPOLLIN handle failed HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swoole-src.