Home >Backend Development >PHP Tutorial >javascript - I want to create a web chat function similar to QQ, how to implement it? ?

javascript - I want to create a web chat function similar to QQ, how to implement it? ?

WBOY
WBOYOriginal
2016-09-19 09:16:241892browse

When making a web-based chat tool, what method do you usually use for real-time messaging? ?

What I know:

  1. ajax polling (they say it’s a waste of resources, not good...)

  2. EventSource (poor support, IE doesn’t support it at all..)

  3. WebSoket (don’t know much about it...)

Moreover, after searching on Baidu, I learned that when the browser does not support WebSocket, there are actually third-party libraries that can implement WebSocket. Is the so-called WebSocket the same as EventSource? The core principle is to use ajax polling to achieve instant communication. ? ?

What is the currently commonly used web real-time communication technology? ? Xiaobai asks for advice, asks the great god to come by air...

Reply content:

When making a web-based chat tool, what method do you usually use for real-time messaging? ?

What I know:

  1. ajax polling (they say it’s a waste of resources, not good...)

  2. EventSource (poor support, IE doesn’t support it at all..)

  3. WebSoket (don’t know much about it...)

Moreover, after searching on Baidu, I learned that when the browser does not support WebSocket, there are actually third-party libraries that can implement WebSocket. Is the so-called WebSocket the same as EventSource? The core principle is to use ajax polling to achieve instant communication. ? ?

What is the currently commonly used web real-time communication technology? ? Xiaobai asks for advice, asks the great god to come by air...

Workerman is an open source high-performance PHP socket server framework developed purely in PHP. It is widely used in the development of mobile apps, mobile game servers, online game servers, chat room servers, hardware communication servers, smart homes, Internet of Vehicles, Internet of Things and other fields. Supports TCP long connections, supports Websocket, HTTP and other protocols, and supports custom protocols. Based on Workerman, developers can focus more on business logic development and no longer have to worry about the underlying development of PHP Socket.
git repository: https://github.com/walkor/Wor...
Chinese homepage: http://www.workerman.net/
Chinese documentation: http://doc3.workerman.net/

The questioner can try this.

Talking about two PHP solutions, one of them, WorkerMan, has been mentioned by someone, and the other is PHPWebIM developed by Fengge based on Swoole.

PECL extension Swoole supports using PHP to write high-performance socket applications:

<code>apt-get install php-pear php5-dev
yum install php-pear php-devel
pecl remote-info swoole
pecl install swoole</code>

PHPWebIM is an official WebSocket web instant chat tool developed by Swoole based on PHP Swoole extension and Swoole Framework.
PHPWebIM supports WebSocket+Comet protocols and can be used in all types of browsers including IE, please see Demo.

If you only want to implement relatively real-time message notifications, you can also use AJAX polling:
1) Single page (only poll on one page)
2) SetInterval interval passive polling (keep-alive persistent connection, such as 30 seconds)
3) window.onfocus window gets focus and is actively triggered (event-driven)
The polling interval is too short and users open too many pages, which may cause excessive pressure on the server.
For businesses that do not have high real-time requirements, you can Increase the interval, such as polling once every 2 minutes.

<code>window.onblur  = function() {document.title = '失去焦点';};
window.onfocus = function() {document.title = '获得焦点';};</code>

I have written one using nodejs socket.io before. If you search directly, you will find many examples written by others: http://www.open-open.com/lib/...

Correct answer upstairs.

Case address: http://www.workerman.net/

Websocket is definitely not implemented through ajax.
Using websocket is definitely the most perfect, and can be implemented through frameworks such as workerman, react, and swoole.
You can also use long polling, but it consumes more resources. But the implementation cost is lower than websocket .
There is also real-time polling, which has the lowest implementation cost. However, the efficiency is definitely the lowest. SF message reminders use scheduled polling

javascript - I want to create a web chat function similar to QQ, how to implement it? ?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn