ghedipunk / PHP-Websockets

A Websockets server written in PHP.
BSD 3-Clause "New" or "Revised" License
913 stars 376 forks source link

multiple instances of websocketserver #29

Closed mortenko closed 9 years ago

mortenko commented 9 years ago

Hi, I have question. Can I create multiple instances of server like: $server1=new server("127.0.0.2","9001"); $server1=new server("127.0.0.3","9002"); $server1=new server("127.0.0.4","9003"); and connect via web browsers to each server? And also is it possible to connect via this web servers to mysql database ?

Xaraknid commented 9 years ago

Currently you can only load one server per app you run. If you load multiple intance of server in same app only one will respond.

If you run multiple app you will be able to connect on each server via web browsers.

you can connect to a dataside inside this app and send info back to client like you do for php page via (apache,nginx,...). But I did not recommend to use websocket to admin the database unless you use wss:// secure protocol same as https;//

ghedipunk commented 9 years ago

Closing: This is questions, not bugs.

In order to have multiple instances of the server running, they have to be started as different processes. (The $port parameter on socket_bind() only accepts one port... There is no way to specify a range of ports, and the server really isn't designed to ever allow multiple listening sockets.)

Also, yes, it is possible to connect to any database that you could otherwise connect to in any other PHP script. This server has absolutely nothing to do with being able to connect to any database.

To set up an analogy as to why this script has nothing to do with any database... It's like saying "I can use my car to go to the store and buy milk. If I use this truck instead, will that store still sell me milk?"

mortenko commented 9 years ago

If I use f.e. haproxy then I can get response from all three servers ?Sorry no connection to DB but I mean I can execute queries using this servers ?

Xaraknid commented 9 years ago

I'm not sure why you want multiple instance of server ? As side note. The revision I working on will handle multiple virtualhost. So one instance of WS server will dispatch message accordingly.

For DB question : you can do it the same way you do with SAPI environnement.

mortenko commented 9 years ago

Here is my problem http://stackoverflow.com/questions/29983805/executing-queries-using-haproxy-and-php-websocket

Xaraknid commented 9 years ago

"Can php websocket communicate with DB? If you know some articles about it... Let me know."

Definately yes! I run both memcache and mysql with my websocket server. Work perfectly!

Just to be sure you know. The core function you will use to process message from client is : process() from testwebsock.php. If you want to know what exactly the message you receive add in process() this line :

$this->stdout($message);

In other word the connection must be establish first you only need one and can keep it alive for the time websocket server run.

Within process() will look something like that : (an exemple where you thrust the client and $message are the complete query you want to send to DB)

DB->query($message);