亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Backend Development PHP Tutorial 一個(gè)基于WebSocket的WEB消息推送框架

一個(gè)基于WebSocket的WEB消息推送框架

Mar 25, 2017 am 09:57 AM
Push message

一個(gè)基于WebSocket的WEB消息推送框架

web-msg-sender是一款web長(zhǎng)連接推送框架,采用PHPSocket.IO開(kāi)發(fā),基于WebSocket長(zhǎng)連接通訊,如果瀏覽器不支持WebSocket則自動(dòng)轉(zhuǎn)用comet推送。 通過(guò)后臺(tái)推送消息,消息可以即時(shí)推送到客戶端,非輪詢,實(shí)時(shí)性非常好,性能很高。

特點(diǎn):

多瀏覽器支持

支持針對(duì)單個(gè)用戶推送消息

支持向所有用戶推送消息

長(zhǎng)連接推送(websocket或者comet),消息即時(shí)到達(dá)

支持在線用戶數(shù)實(shí)時(shí)統(tǒng)計(jì)展示

支持在線頁(yè)面數(shù)實(shí)時(shí)統(tǒng)計(jì)展示

支持跨域推送

下載

https://github.com/walkor/web-msg-sender

WEB消息推送框架

接收消息頁(yè)面:http://www.workerman.net:2123/

推送接口url:http://www.workerman.net:2121/?type=publish&to=uid&content=content
其中to為接收消息的uid,如果傳空則向所有人推送消息 content 為消息內(nèi)容

Linux環(huán)境安裝啟動(dòng)

1、下載web-msg-sender,并解壓縮到任意目錄

2、啟動(dòng)workermanphp start.php start -d如下圖

3、瀏覽器訪問(wèn)端口http://ip:2123或者h(yuǎn)ttp://域名:2123,例如?http://workerman.net:2123如圖:(如果無(wú)法訪問(wèn)請(qǐng)檢查服務(wù)器防火墻)

前端測(cè)試:

支持跨域推送,開(kāi)發(fā)者可以不用建立服務(wù)端,直接使用線上的推送服務(wù)測(cè)試,只要引入js文件并設(shè)置下端口及回調(diào)即可,例如在任意站點(diǎn)中加入如下代碼即可收到消息并統(tǒng)計(jì)數(shù)據(jù):

<script src=&#39;http://cdn.bootcss.com/socket.io/1.3.7/socket.io.js&#39;></script>
<script>
    // 連接服務(wù)端
    var socket = io(&#39;http://workerman.net:2120&#39;);
    // uid可以是自己網(wǎng)站的用戶id,以便針對(duì)uid推送以及統(tǒng)計(jì)在線人數(shù)
    uid = 123;
    // socket連接后以u(píng)id登錄
    socket.on(&#39;connect&#39;, function(){
    socket.emit(&#39;login&#39;, uid);
    });
    // 后端推送來(lái)消息時(shí)
    socket.on(&#39;new_msg&#39;, function(msg){
        console.log("收到消息:"+msg);
    });
    // 后端推送來(lái)在線數(shù)據(jù)時(shí)
    socket.on(&#39;update_online_count&#39;, function(online_stat){
        console.log(online_stat);
    });
</script>

后端調(diào)用api向任意用戶推送:

<?php
// 指明給誰(shuí)推送,為空表示向所有在線用戶推送
$to_uid = "";
// 推送的url地址,上線時(shí)改成自己的服務(wù)器地址
$push_api_url = "http://workerman.net:2121/";
$post_data = array(
   "type" => "publish",
   "content" => "這個(gè)是推送的測(cè)試數(shù)據(jù)",
   "to" => $to_uid, 
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $push_api_url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
$return = curl_exec ( $ch );
curl_close ( $ch );
var_export($return);

相關(guān)文章:

微信開(kāi)發(fā)消息推送實(shí)現(xiàn)技巧(附代碼)

.NET 微信開(kāi)發(fā)自動(dòng)內(nèi)容回復(fù)實(shí)例代碼

在Java中通過(guò)websocket實(shí)現(xiàn)消息推送的實(shí)現(xiàn)代碼詳解

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
Use Firebase Cloud Messaging (FCM) to implement message push functionality in PHP applications Use Firebase Cloud Messaging (FCM) to implement message push functionality in PHP applications Jul 24, 2023 pm 12:37 PM

Use Firebase Cloud Messaging (FCM) to implement message push function in PHP applications. With the rapid development of mobile applications, real-time message push has become one of the indispensable functions of modern applications. Firebase Cloud Messaging (FCM) is a cross-platform messaging service that helps developers push real-time messages to Android and iOS devices. This article will introduce how to use FCM to implement message push function in PHP applications.

How to implement message push and notification reminder in uniapp How to implement message push and notification reminder in uniapp Oct 20, 2023 am 11:03 AM

How to implement message push and notification reminders in uniapp With the rapid development of mobile Internet, message push and notification reminders have become indispensable functions in mobile applications. In uniapp, we can implement message push and notification reminders through some plug-ins and interfaces. This article will introduce a method to implement message push and notification reminder in uniapp, and provide specific code examples. 1. Message Push The premise for implementing message push is that we need a background service to send push messages. Here I recommend using Aurora Push.

How to turn off the message push on the Amap map_How to turn off the message push on the Amap map How to turn off the message push on the Amap map_How to turn off the message push on the Amap map Apr 01, 2024 pm 03:06 PM

1. Open the phone settings, click Applications, and click Application Management. 2. Find and click to enter the Amap. 3. Click Notification Management and turn off the Allow Notifications switch to turn off message push notifications. This article takes Honor magic3 as an example and is applicable to Amap v11.10 version of MagicUI5.0 system.

How to use the PHP framework Lumen to develop an efficient message push system and provide timely push services How to use the PHP framework Lumen to develop an efficient message push system and provide timely push services Jun 27, 2023 am 11:43 AM

With the rapid development of mobile Internet and changes in user needs, the message push system has become an indispensable part of modern applications. It can realize instant notification, reminder, promotion, social networking and other functions to provide users and business customers with better services. experience and service. In order to meet this demand, this article will introduce how to use the PHP framework Lumen to develop an efficient message push system to provide timely push services. 1. Introduction to Lumen Lumen is a micro-framework developed by the Laravel framework development team. It is a

Message push service selection for developing real-time chat function in PHP Message push service selection for developing real-time chat function in PHP Aug 26, 2023 am 11:21 AM

Selection of message push service for developing real-time chat function in PHP Introduction: With the rapid development of the Internet, real-time communication has become an indispensable function for many websites and applications. In order to achieve real-time message push and real-time chat functions, it is crucial to choose the appropriate message push service. This article will introduce some commonly used message push services that are suitable for PHP development and provide relevant code examples. 1. Implementation of WebSocket protocol The WebSocket protocol is a TCP-based protocol specifically used to achieve real-time two-way communication.

UniApp's design and development skills for implementing message push and push services UniApp's design and development skills for implementing message push and push services Jul 04, 2023 pm 12:57 PM

UniApp is a framework for developing cross-platform applications that can run on iOS, Android and Web platforms at the same time. When implementing the message push function, UniApp can cooperate with the back-end push service to realize the design and development of message push. 1. Design overview of message push To implement the message push function in UniApp, you need to design a push service to send push messages to the App. The push service needs to implement the following functions: establish a connection with the App and send messages.

Analysis of the relationship between PHP real-time communication function and message push middleware Analysis of the relationship between PHP real-time communication function and message push middleware Aug 10, 2023 pm 12:42 PM

Analysis of the relationship between PHP real-time communication function and message push middleware With the development of the Internet, the importance of real-time communication function in Web applications has become increasingly prominent. Real-time communication allows users to send and receive messages in real-time in applications, and can be applied to a variety of scenarios, such as real-time chat, instant notification, etc. In the field of PHP, there are many ways to implement real-time communication functions, and one of the common ways is to use message push middleware. This article will introduce the relationship between PHP real-time communication function and message push middleware, and how to use message push

Quick Start: Use Go language functions to implement simple message push functions Quick Start: Use Go language functions to implement simple message push functions Jul 31, 2023 pm 02:09 PM

Quick Start: Use Go language functions to implement simple message push functions In today's mobile Internet era, message push has become a standard feature of various APPs. Go language is a fast and efficient programming language, which is very suitable for developing message push functions. This article will introduce how to use Go language functions to implement a simple message push function, and provide corresponding code examples to help readers get started quickly. Before we begin, we need to understand the basic principles of message push. Generally, message push functionality requires two main components: push server

See all articles