Hi! I have an issue with Swoole WebSocket Server. I created an object to use Swoole Server. Here is the constructor:
`public function __construct()
{
$this->serverWS = new SwooleWebSocketServer("192.168.56.2", 9502);
$this->serverWS->on('open',
function(Server $serverWS, Request $request)
{
$this->onConnection($request);
}
);
$this->serverWS->on('message',
function(Server $serverWS, Frame $frame)
{
$this->onMessage($frame);
}
);
$this->serverWS->on('close',
function(Server $serverWS, int $fd)
{
$this->onClose($fd);
}
);
$this->serverWS->on('workerStart',
function(Server $serverWS)
{
$this->onWorkerStart($this->serverWS);
}
);
$this->serverWS->start();
}`
Also, I have a desktop client written in Python (I start it with Linux terminal). Connection is established by the following example:
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://192.168.56.2:9502",
on_message = on_message,
on_error = on_error,
on_close = on_close,
header = {
'User-Agent: RaspberryPi/3B+' + ' ' + str(os.uname()),
'Access-Token: ' + generate_token()
}
)
ws.on_open = on_open
ws.run_forever()
What I do on server side is set current Python connection ID (I'm defining a Python client by the certain request header) to private property of my custom WebsocketServer object (this property is equal 0 by default). And by triggering onClose event I set this property to 0 again. If this property is not 0 and the same client tries to establish the connection, server closes it.
I do this because i need to identify this client and have only one instance of it's connection.
And the problem is that I can have two connections at the same time (two terminal windows with started clients). If there's three or more clients - all works fine: server closes connections.
I tried to debug and what I noticed is when the first PythonClient connection triggers, WebsocketServer sets ID property from 0 to 1 (and I can easily read this property and it will surely be equal 1). Then I'm running the second PythonClient and what i got on server is 0 ID property again, which will be setted to 2...
The third connection (and following ones) get ID that is equal 1, untill I'm keeping first connection opened. (As it should be)
This is so strange. I have no any idea why this happens. Please, tell me what I'm doing wrong!
My Swoole version is:
`php --ri swoole
swoole
Swoole => enabled
Author => Swoole Team team@swoole.com
Version => 4.3.1
Built => Mar 22 2019 11:50:29
coroutine => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 1.1.0g 2 Nov 2017
http2 => enabled
pcre => enabled
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled
Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608
`
My Linux kernel is:
Linux elephant 4.15.0-47-generic #50-Ubuntu SMP Wed Mar 13 10:44:52 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
My PHP version is:
PHP 7.2.17-0ubuntu0.18.04.1 (cli) (built: Apr 18 2019 14:12:38) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.17-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
I can only tell you the following points due to my poor English language proficiency:
new Swoole\WebSocket\Server("192.168.56.2", 9502, SWOOLE_BASE); to make it be a single process ServerI can only tell you the following points due to my poor English language proficiency:
- The server is asynchronous, multiple requests may be processed at the same time
- Server use multi-process mode by default, you can try
new Swoole\WebSocket\Server("192.168.56.2", 9502, SWOOLE_BASE);to make it be a single process Server
Thanks, it's working now!
Most helpful comment
I can only tell you the following points due to my poor English language proficiency:
new Swoole\WebSocket\Server("192.168.56.2", 9502, SWOOLE_BASE);to make it be a single process Server