Comments (14)
Hello :-),
So let's start by question 2 (the first one, because you have two 2 😉). Each connection is represented by a node. Maybe this Section of the documentation might help you, http://hoa-project.net/En/Literature/Hack/Websocket.html#Customized_node.
Next, question 1. PHP is synchronous by nature. You cannot listen to incoming message while having another execution where you send messages. This is possible if you use pthread for instance or some tricks to get a pseudo-asynchronous execution with generators and co-routines.
Finally, question 3. Answered with the previous paragraph.
from websocket.
1) I think you may have misunderstood me, will try to explaine.
- when page is rendered I create connection with socketServer (send user id to server)
- SocketServer runs under Supervisord, with while(true){} loop
- SocketServer iterate each node, and checking message for each user by his id received at the first step
- I have 2500 clients that working at the same time
Thats what I need.
2) Yes, I think it will be help, need to try, thanks.
3) I understand, but how you can solved this problem, if you need answer each client every minute?
Run two process, one for read and one for send or will sending message from client each minute, or some another way?
from websocket.
So you want that:
- the server replies to the client when a new message is coming,
- but also sending spontaneously messages to every client each x ticks.
Is it correct?
from websocket.
Yes and yes.
I think, I can extend the loop circul, and call once in 3 seckonds my method that sending data to clients.
Whay You think abut this?
from websocket.
You cannot have 2 infinite loops inside the same thread. When calling the famous run
method, an infinite loop is starting. If you are starting another infinite loop inside a listener, this will not work.
I recommend 2 solutions: Using pthread, or using generators/co-routines libraries to simulate asynchronous execution. Most people uses pthread for this usages.
Or, you can use 2 servers.
from websocket.
Did you have your answer or should we keep this issue open?
from websocket.
Can we create something like this: https://github.com/Shkarbatov/WebSocketPHPWorkerman/blob/master/worker.php ?
from websocket.
This is almost the actual API of Hoa\Websocket\Server
or Hoa\Websocket\Client
. You add listeners, and you run the instance.
What do you mean?
from websocket.
Can I send data from server to client with Hoa like I do this with workerman?
Not sending every second request from web through WebSockets to server.
from websocket.
Hello @Shkarbatov, Yes, as long you save the client (fro source bucket) during client established connection to the server.
from websocket.
Thanks for answer, do you have any example?
from websocket.
Trying next:
Worker:
use Hoa\Websocket\Server as WebsocketServer;
use Hoa\Socket\Server as SocketServer;
use Hoa\Event\Bucket;
$subscribedTopics = array();
// =================================
$websocket_php = new WebsocketServer(
new SocketServer('ws://127.0.0.1:8009')
);
$websocket_php->on('open', function (Bucket $bucket) { });
$websocket_php->on('message', function (Bucket $bucket) use (&$subscribedTopics) {
$data = json_decode($bucket->getData()['message'], true);
if (isset($data['user']) and isset($subscribedTopics[$data['user']])) {
$subscribedTopics[$data['user']]->getSource()->send($data['command']);
}
});
// =================================
$websocket_web = new WebsocketServer(
new SocketServer('ws://127.0.0.1:8008')
);
$websocket_web->on('open', function (Bucket $bucket) use (&$subscribedTopics) {
$subscribedTopics[substr($bucket->getSource()->getRequest()->getUrl(), 7)] = $bucket;
});
$websocket_web->on('message', function (Bucket $bucket) {
$bucket->getSource()->send('Socket connected');
});
$websocket_web->on('close', function (Bucket $bucket) { });
// =================================
$group = new Hoa\Socket\Connection\Group();
$group[] = $websocket_web;
$group[] = $websocket_php;
$group->run();
Web Client:
<script>
var ws = new WebSocket('ws://site.ll:8008/?user=tester01');
ws.onmessage = function(evt) { console.log(evt); };
ws.onopen = function() {
console.log("connect");
ws.send("hello");
};
ws.onerror = function(error) {
console.log("Error " + error.message);
};
</script>
PHP Client:
$client = new Hoa\Websocket\Client(new Hoa\Socket\Client('tcp://127.0.0.1:8009'));
$client->setHost('127.0.0.1');
$client->connect();
$client->send(json_encode(['user' => 'tester01', 'command' => '111111']));
$client->close();
Web client connected to worker and save his backet, then I run php client and worker receive message, find backet needed web client and send message, but it not send and I don't have any errors.
Any idea what it can be?
from websocket.
Any idea, why it is not working?
from websocket.
Well, problem next:
I saved bucket next:
$subscribedTopics[substr($bucket->getSource()->getRequest()->getUrl(), 7)] = $bucket;
And then, when I want to send:
$subscribedTopics[$data['user']]->getSource()->send($data['command']);
if I will not put Socket\Node as the second parametr of the send function it will used current node:
if (null === $node) {
$node = $this->getConnection()->getCurrentNode();
}
This is ok, but if I will used Node from saved backed:
$subscribedTopics[$data['user']]->getSource()->send($data['command'],
$subscribedTopics[$data['user']]->getSource()->getConnection()->getCurrentNode());
It will return current node, not saved node.
So I need to save separate bucket and node:
$subscribedTopics[substr($bucket->getSource()->getRequest()->getUrl(), 7)] =
['bucket' => $bucket, 'node' => $bucket->getSource()->getConnection()->getCurrentNode()];
And when send message add saved node:
$subscribedTopics[$data['user']]['bucket']->getSource()->send(
$data['command'],
$subscribedTopics[$data['user']]['node']
);
Is this bug? Can I somehow receive node from saved bucked?
I can receive node id, but there no method receive node by id, only all with method getNodes()
from websocket.
Related Issues (20)
- Try to change the stream resource with an invalid one; given unknown type when trying to broadcastIf in on close. HOT 2
- Closing client from server closes other clients as well? HOT 8
- how can i get url query string value? HOT 2
- How can I do action every x seconds/ticks/whatever ? HOT 6
- Same connection used by two clients ?
- Close a given node by nodeId
- Always crash server when client disconnect.
- Dependabot can't resolve your PHP dependency files
- Dependabot can't resolve your PHP dependency files
- Operation timed out (nothing to accept)
- Cannot find how to send message to customized Node
- Dependabot can't resolve your PHP dependency files
- Dependabot can't resolve your PHP dependency files
- Connection is closed
- About audio streaming HOT 1
- Send message to a specific Client from another place than $server->on('message'..) HOT 4
- Vue and websocket HOT 3
- Data read from socket doesn't match expected length HOT 6
- Test failure HOT 1
- Bucket Send problem HOT 2
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 websocket.