Real-Time communication between PHP and Flex appli

2019-03-04 16:42发布

问题:

I have a Flex/AIR application communicating with a PHP server application. Recently my client requested real-time-like connectivity between the applications to replace what we currently have; refresh buttons which have to be clicked to refresh data.

So effectively the server needs to push data to the client when something changes. I can write the code that detects when something changes, but I'm not sure about the push side of things.

What's my best option here?

回答1:

Actually you can push data from the server to the client, when using Flash sockets (which means bypassing the http protocol). If you cannot use sockets you can use things like polling, long polling or http streaming. However it will take a while to do everything by hand, so I suggest looking at a product which already has this features. WebOrb for PHP is one of them, and from what I know is free.



回答2:

I'm no flex developer so I'm not going to provide you some code to help with this situation but I can provide you some ideas of how you would go about aproaching this.

Instead of looking for PHP to push to Flex I would setup your Flex code to constantly probe the PHP for new happenings. You can probe every second if you can be sure that the server will respond quick enough. Alternatively something that probes ever 5 seconds would still be real time.

If PHP is sending DATA back to your flex app then I would make sure it sends IDs with the data so that you can use that ID in your flex app to know what was the last data returned. Each time you probe PHP send the last ID you recieved that way telling PHP only to send new DATA after that ID. Many a time you will probe and there wont be anything comming back but its better than recieving the same data over and over again and wasting bandwidth and proccesses figuring out if the data is already shown on screen.



回答3:

You cannot push data from the server to the client. That's the nature of the web :(

What you have to do is continuously poll the server from the client and check if something has changed.

If something has changed then you can take the necessary actions. This is how google / facebook and everyone else does this. To the user it looks like the server pushed the data but internally the client keeps polling the server.



回答4:

This article may be a starting point for you. Communicating with Flex and PHP over Sockets