In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The method of real-time push back-end messages to the front end in workerman. In view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
In the process of development, we often encounter the following situations. The front-end list shows the data in the background database, but inserts a piece of data into the database in one of the backend APIs. At this time, the database has been updated, but the front-end display data is not updated and needs to be refreshed manually. But it's too troublesome to update it manually every time, so you can use workerman to solve the problem.
Workerman framework is a pure PHP development of open source high-performance PHP socket server framework. It is widely used in mobile app, mobile communication, WeChat Mini Programs, mobile game server, online game, PHP chat room, hardware communication, smart home, car Internet, Internet of things and other fields of development. Support TCP persistent connection, support Websocket, HTTP and other protocols, support custom protocols. It has many high performance components, such as asynchronous Mysql, asynchronous Redis, asynchronous Http, asynchronous message queue and so on.
So how can we use it to solve the above problems?
1. The front and back ends establish a long connection to websocket, which is used to push messages to each other.
two。 A listening process is being set up inside the backend (unlimited by protocol)
3. After the interface successfully inserts data into the database, I want to push the data to the internal listening port.
4. After receiving the push message from the internal listening port, the back end is pushing the message to the front end through websocket to refresh.
After downloading the source code of the workerman framework, let's implement the above process.
Implementation code:
Server.php
Clien.html
Var ws = new WebSocket ('ws://127.0.0.1:1234'); ws.onopen = function () {var uid =' uid1'; ws.send (uid);}; ws.onmessage = function (e) {/ / alert (e.data); console.log (e.data) / / _ window.location.reload ();}
Run the process:
Open cmd and run server.php
Open the front page and console
After opening a cmd, run push.php
At this time, when you look at the front-end page, console receives the message.
This is the answer to the question about the real-time push of back-end messages to the front end in workerman. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.