PHP chat rooms were previously implemented through ajax and PHP. Today we will take a look at a PHP chat room WebSocket technical example. PHP chat rooms were previously implemented through ajax and PHP. Today we will take a look at a PHP chat room WebSocket technical example.
Script ec (2); script
Recently I am studying chat room technology. Because I am a php developer, I definitely want to start with php, even in a chat room ,:
Http://pan.baidu.com/s/1dDWsbip
Password: i2qz
Preparing for a php chat room system based on this, php is easy to understand, however, one problem I encountered was how to send information to the backend server in front-end files. Later I found that the Javascript WebSocket technology was used. The following are common operations for js WebSocket:
Var socket = new WebSocket ('ws: // localhost: 100 ');
// Open the Socket
Socket. onopen = function (event ){
}
// Send an initialization message
Socket. send ('I am the client and I \' m listening! ');
// Listen for messages
Socket. onmessage = function (event ){
Console. log ('client received ed a message', event );
};
// Disable the listening Socket
Socket. onclose = function (event ){
Console. log ('client notified socket has closed ', event );
};
// Close the Socket ....
Socket. close ()
Socket. onerror = function (evt) {console. log ("WebSocketError !");};
The version downloaded here is the php chat room of the window version. You need to configure how to run the php environment in cmd,
It's very simple. There are a lot of tutorials on the Internet. You can search for them. I only say that one of my doubts is that after configuring the environment variable path according to the tutorial, it should be because you can run php In cmd, but it just won't work. After a day, you will find that you can use it, and then you will understand that after the configuration is complete, you need to restart the computer to take effect.
I used node before. js has developed some small functions and I think the php Technology and node here. js functions are similar, and they all do not require the runtime environment. In fact, I think the biggest breakthrough point is that, the server can actively send information to users to control the browser of the user end. I think this is of great significance. In this way, you do not need to use ajax to constantly access the server to obtain the latest technology, which greatly reduces the pressure on the server and improves the user experience, however, it is found that this technology is still not widely used, so some video tutorials will be released in the future, so that children's shoes can learn from each other.
After downloading the source code, double-click the start_for_win.bat file under the directory to start the chat room service,
Access: http: // 127.0.0.1: 55151/to enter the chat room.
You can also use the following command to control the service:
Start
Start in debug mode
Php/var/www/workerman/start. php start
Start in daemon mode
Php/var/www/workerman/start. php start-d
Stop
Php/var/www/workerman/start. php stop
Restart
Php/var/www/workerman/start. php restart
Smooth restart
Php/var/www/workerman/start. php reload
View status
Php/var/www/workerman/start. php status
I only want to mention the important point. When the debug mode is enabled, the chat room service will be closed when you close the cmd window, And the daemon mode will be used for the startup. Even if the cmd window is closed, the chat room service is still running