server.php

<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set("fd", "[]"); //每次第一次执行都需要先清空reids里面的标识 $server = new swoole_websocket_server("0.0.0.0", 9502); $server->on('open', function (swoole_websocket_server $server, $request) use($redis) {
echo "访客{$request->fd}进来了\n";
$server->push($request->fd, "{$request->fd}客户进来了");
$str = json_decode($redis->get("fd"), true);
if($str == "") $str = [];
if(!in_array($request->fd, $str)){
array_push($str, $request->fd);
$str = json_encode($str);
$redis->set("fd", $str);
echo "目前在线访客:";
print_r($redis->get("fd"));
}
}); $server->on('message', function (swoole_websocket_server $server, $frame) use($redis) {
echo "系统消息说:{$frame->data}\n";
$str = json_decode($redis->get("fd"), true);
foreach ($str as $key => $value) {
if($frame->fd != $value){
$server->push($value, "系统消息说:".$frame->data);
}
}
}); $server->on('close', function ($ser, $fd) use($redis) {
echo "client {$fd} closed\n";
$str = json_decode($redis->get("fd"), true);
$point = array_keys($str, $fd, true); //search key
array_splice($str, $point['0'],1); //delete array
$str = json_encode($str);
$redis->set("fd", $str);
echo "删除后在线访客:";
print_r($redis->get("fd"));
}); $server->start();

Cli命令行执行:/usr/local/php/bin/php server.php

client.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="main">
<div class="msgs">
消息通知:<span id="push_content" style="color:red"></span>
</div>
</div>
</body>
<script>
var socket = new WebSocket('ws://23.27.127.32:9502'); //监听端口
socket.onopen = function () { //监听是否连接服务器成功触发
console.log('Connected!');
//socket.send("这条信息会返回给服务器看"); //重要!!客户端返回服务器
};
socket.onmessage = function (event) { // **接收到服务器数据**触发
console.log(event.data);
// alert('Received data: ' + event.data);
document.getElementById("push_content").innerHTML = event.data;
// socket.close();
};
socket.onclose = function () { //与服务器连接断开触发
console.log('Lost connection!');
};
socket.onerror = function () { //与服务器连接出现错误触发
console.log('Error!');
};
// socket.send('hello, world!'); </script>
</html>

admin.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>后台推送界面</title>
</head>
<body>
<div>
<input type="text" name="content" placeholder="请输入需要推送的信息">
<button id="push_button">推送</button>
</div>
</body>
<script>
window.onload = function () {
var socket = new WebSocket('ws://23.27.127.32:9502'); //监听端口
var push_button = document.getElementById("push_button");
var push_content = document.getElementsByName("content"); push_button.onclick = function () {
socket.send(push_content['0'].value);
}
socket.onmessage = function (event) { // **接收到服务器数据**触发
// alert('Received data: ' + event.data); //这里应该返回发送成功的额标识
};
socket.onopen = function () { //监听是否连接服务器成功触发
console.log('Connected!');
// socket.send(push_content['0'].value); //重要!!客户端返回服务器
}; socket.onclose = function () { //与服务器连接断开触发
console.log('Lost connection!');
};
socket.onerror = function () { //与服务器连接出现错误触发
console.log('Error!');
};
};
</script>
</html>

源码下载

swoole WebSocket 消息推送的更多相关文章

  1. node.js Websocket消息推送---GoEasy

    Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送 速度快,代码简单易懂上手快 浏览器兼容性:GoEasy推送 支持websocket ...

  2. C(++) Websocket消息推送---GoEasy

    Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送 速度快,代码简单易懂上手快 浏览器兼容性:GoEasy推送 支持websocket ...

  3. 【WebSocket】WebSocket消息推送

    准备使用WebSocket实现Java与Vue或者安卓间的实时通信,实现私密聊天.群聊.查询下资料备用. WebSocket客户端 websocket允许通过JavaScript建立与远程服务器的连接 ...

  4. swoole websocket服务推送

    用过workerman, 两个字"好用",对于swoole最近有时间也研究研究 swoole的websocket 很好实现 如官网 https://wiki.swoole.com/ ...

  5. 使用swoole进行消息推送通知,配合vb.net进行客户端开发一样爽[开发篇]

    在以前的项目中,就曾听说过swoole的大名,想用来进行消息推送,但是当时只是有了初步的了解,并不敢大胆的运用到线上产品.所谓 识不足则多虑,威不足则多怒.所以就是怕,只能跟领导说了运用极光的推送功能 ...

  6. websocket消息推送实现

    一.服务层 package com.demo.websocket; import java.io.IOException; import java.util.Iterator; import java ...

  7. WebSocket消息推送

    WebSocket协议是基于TCP的一种新的网络协议,应用层,是TCP/IP协议的子集. 它实现了浏览器与服务器全双工(full-duplex)通信,客户端和服务器都可以向对方主动发送和接收数据.在J ...

  8. spring boot下WebSocket消息推送(转)

    原文地址:https://www.cnblogs.com/betterboyz/p/8669879.html WebSocket协议 WebSocket是一种在单个TCP连接上进行全双工通讯的协议.W ...

  9. spring+rabbitmq+stomp搭建websocket消息推送(非spring boot方式)

    前言: 两年前做过spring+activemq+stomp的ws推送,那个做起来很简单,但现在公司用的mq中间件是rabbitmq,因此需要通过rabbitmq去做ws通信.仔细搜了搜百度/谷歌,网 ...

随机推荐

  1. centos中如何安装php-bcmath扩展?

    talk is cheap,show me the code: [root@LAMP1 lib]# php -v PHP (cli) (built: Oct ::) Copyright (c) - T ...

  2. php在循环内外实例化类占用内存比较

    关于php类的实例化和内存的关系,可以这么说:只要有一个new 关键字就是创建一个对象,创建一个对象就是在内存中分配了一个空间. 代码1: 在循环外实例化类:class ABC{ public $nu ...

  3. column count of mysql.proc is wrong. expected 20,found 16. the table is probably corruptd.

    1558 1547 column count of mysql.proc is wrong. expected 20,found 16. the table is probably corruptd. ...

  4. 【android】SDK环境变量配置

    Android SDK: Android SDK提供了你的API库和开发工具构建,测试和调试应用程序,Android.简单来讲,Android SDK 可以看做用于开发和运行Android应用的一个软 ...

  5. 关系型数据库与Key-value型数据库Mongodb模式设计对比

    MongoDb 相比于传统的 SQL 关系型数据库,最大的不同在于它们的模式设计( Schema Design )上的差别,正是由于这一层次的差别衍生出其它各方面的不同. 我们可以简单的认为关系型数据 ...

  6. 源文件封装为IP的步骤

    因为模块的交接,最好将写好的源文件和生成的IP封装一个IP,然后再转交给其他的同事使用,这是一种好的习惯.但是对于,封装的过程还是需要注意一下.实际的看看步骤吧.1)将源文件和使用到的IP生成工程. ...

  7. Eutils用法总结

    好久没更新了,这里都长草了... 总结下Eutils的用法,参考<E-utilities Quick Start>,没时间看英文的可以参考下. 简介 Eutils全称是The Entrez ...

  8. CNN网络参数

    卷积神经网络 LeNet-5各层参数详解 LeNet论文阅读:LeNet结构以及参数个数计算     LeNet-5共有7层,不包含输入,每层都包含可训练参数:每个层有多个Feature Map,每个 ...

  9. probably another instance of uWSGI is running on the same address (127.0.0.1:9090). bind(): Address already in use

    probably another instance of uWSGI is running on the same address (127.0.0.1:9090). bind(): Address ...

  10. Python/Java读取TXT文件

    JAVA: public static void readTextFile(String filePath) { try { String encoding = "GBK"; Fi ...