1

I have a php socket server that I use to handle messages from a modem. I want to create a php websocket server and whenever I receive a message from the modem I send it to a webclient. I'm a couple days trying to do it without success.

How is the best way to do it? Thanks

.

Here is the way I want to do:

Modem ------> PHP Socket Server ------> Web Clients
                    v
                 database

But if you have another best way to do it, tell me.

Thankyou again.

 

 

EDITED: This is an alternative for socket_read errors in non-blocking socket: http://php.net/manual/pt_BR/function.socket-read.php#73509

1 Answer 1

2

You can do it with non-blocking socket and long polling technique.

The workflow is something like this.

  1. PHP connects to Modem with a non-blocking socket.
  2. Web client sends a long polling http request which times out after 5 or 10 minutes.
  3. PHP made the incoming client connection as non-blocking. This way PHP adds every client to a queue.
  4. PHP iterates over each of the clients and modem and check if something could be written or read.
  5. if there is something to read from Modem it reads and process. If necessary message is written to Webclient socket.
  6. Same as step 5 but goes from web client socket to modem socket.
  7. Do any additional work.
  8. GO to step 3.

I had a similar situation months ago. I had to keep web sessions in PHP and there was another server which PHP was connected too. Couldn't finished it. Later I had to move to Python for this.

Sign up to request clarification or add additional context in comments.

4 Comments

In step 3: I don't need to care about queue, right? Do I only have to connect like this? -> stackoverflow.com/a/9761659/1791115
You need to maintain a queue or a list. Otherwise in the next iteration you can not find the sockets! Its just used for storage purpose
Sorry, I'm starting to use sockets now and I really need to do it. Is this the way to maintain the queue list? groups.drupal.org/node/184984
@GiovanneAfonso read this in php manual. Oh you have to use socket_select.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.