phpsocket.io
https://github.com/walkor/phpsocket.io
phpsocket.io
A server side alternative implementation of socket.io in PHP based on Workerman.
Notice
Only support socket.io v1.3.0 or greater.
This project is just translate socket.io by workerman.
More api just see http://socket.io/docs/server-api/
Install
composer require workerman/phpsocket.io
Examples
Simple chat
start.php
use Workerman\Worker;
use PHPSocketIO\SocketIO;
require_once __DIR__ . '/vendor/autoload.php'; // Listen port 2021 for socket.io client
$io = new SocketIO(2021);
$io->on('connection', function ($socket) use ($io) {
$socket->on('chat message', function ($msg) use ($io) {
$io->emit('chat message', $msg);
});
}); Worker::runAll();
Another chat demo
https://github.com/walkor/phpsocket.io/blob/master/examples/chat/start_io.php
use Workerman\Worker;
use PHPSocketIO\SocketIO;
require_once __DIR__ . '/vendor/autoload.php'; // Listen port 2020 for socket.io client
$io = new SocketIO(2020);
$io->on('connection', function ($socket) {
$socket->addedUser = false; // When the client emits 'new message', this listens and executes
$socket->on('new message', function ($data) use ($socket) {
// We tell the client to execute 'new message'
$socket->broadcast->emit('new message', array(
'username' => $socket->username,
'message' => $data
));
}); // When the client emits 'add user', this listens and executes
$socket->on('add user', function ($username) use ($socket) {
global $usernames, $numUsers; // We store the username in the socket session for this client
$socket->username = $username;
// Add the client's username to the global list
$usernames[$username] = $username;
++$numUsers; $socket->addedUser = true;
$socket->emit('login', array(
'numUsers' => $numUsers
)); // echo globally (all clients) that a person has connected
$socket->broadcast->emit('user joined', array(
'username' => $socket->username,
'numUsers' => $numUsers
));
}); // When the client emits 'typing', we broadcast it to others
$socket->on('typing', function () use ($socket) {
$socket->broadcast->emit('typing', array(
'username' => $socket->username
));
}); // When the client emits 'stop typing', we broadcast it to others
$socket->on('stop typing', function () use ($socket) {
$socket->broadcast->emit('stop typing', array(
'username' => $socket->username
));
}); // When the user disconnects, perform this
$socket->on('disconnect', function () use ($socket) {
global $usernames, $numUsers; // Remove the username from global usernames list
if ($socket->addedUser) {
unset($usernames[$socket->username]);
--$numUsers; // echo globally that this client has left
$socket->broadcast->emit('user left', array(
'username' => $socket->username,
'numUsers' => $numUsers
));
}
});
}); Worker::runAll();
Enable SSL for https
(phpsocket.io>=1.1.1 && workerman>=3.3.7 required)
start.php
<?php use Workerman\Worker;
use PHPSocketIO\SocketIO; require_once __DIR__ . '/vendor/autoload.php'; // SSL context
$context = array(
'ssl' => array(
'local_cert' => '/your/path/of/server.pem',
'local_pk' => '/your/path/of/server.key',
'verify_peer' => false
)
);
$io = new SocketIO(2021, $context); $io->on('connection', function ($connection) use ($io) {
echo "New connection coming\n";
}); Worker::runAll();
手册
phpsocket.io的更多相关文章
- PHPSocket.IO知识学习整理
一.服务端和客户端连接 1.创建一个SocketIO服务端 <?php require_once __DIR__ . '/vendor/autoload.php'; use Workerman\ ...
- web socket (记录下来方便观看)
Web Sockets HTML5 WebSocket 设计出来的目的就是要取代轮询和 Comet 技术,使客户端浏览器具备像 C/S 架构下桌面系统的实时通讯能力. 浏览器通过 JavaScript ...
- web推送
WEB消息推送框架 web-msg-sender是一款web长连接推送框架,采用PHPSocket.IO开发,基于WebSocket长连接通讯,如果浏览器不支持WebSocket则自动转用comet推 ...
- web-msg-send 学习 http://www.workerman.net/web-sender
WEB消息推送框架 web-msg-sender是一款web长连接推送框架,采用PHPSocket.IO开发,基于WebSocket长连接通讯,如果浏览器不支持WebSocket则自动转用comet推 ...
- Notadd 2.0 全新 Node.js 版本~ (开发中) [从 PHP 到 node 的踩坑记]
对于 Notadd 我们本来期望它实现更多... 尽管我们也尝试做了很多努力,但是由于 PHP 本身的局限,以及考虑到开发环境配置的复杂程度,最终使用了折中方案.接下来,我们谈谈整个技术选型历程,也供 ...
- 后台接口平台 基于Laravel 开发 快速开发数据接口
laravelPCMS V1.5.0 项目地址:https://github.com/q1082121/laravelcms 喜欢的朋友可以支持下 点点星标 百牛信息技术bainiu.ltd整理发布于 ...
- thinkphp 5.0整合phpsocketio完整攻略,绕坑
使用环境: thinkphp5.0 项目需求 前端下单,后台接受,并立即做出提示.例如:美团外卖,客户端下单成功后,商家端就会立即有接单语音提示. 开发环境 thinkphp5.0 phpsocket ...
- 必学PHP类库/常用PHP类库大全,php 类库分类-收集
依赖管理( Dependency Management ) 用于依赖管理的包和框架 Composer / Packagist - 一个包和依赖管理器. Composer Installers - 一个 ...
- workman即时推送
https://www.workerman.net/web-sender 下载源码解压后运行 start_for_win.bat 如果提示不成功,就把php路径配置到环境变量中去即可 运行后打开浏 ...
随机推荐
- idea运行gradle项目报错,找不到符号符号,方向xxxx类未知
报错: 解决:把build和run设置为idea
- Python常见数据结构-字符串
字符串基本特点 用引号括起来,单引号双引号均可,使用三个引号创建多行字符串. 字符串不可变. Python3直接支持Unicode编码. Python允许空字符串存在,不含任何字符且长度为0. 字符串 ...
- 33.2 案例:输出指定目录下的所有java文件名(包含子目录)
package day32_file_文件和目录操作; import java.io.File; public class test_输出指定目录下所有的java文件名 { public static ...
- Struts2-学习笔记系列(13)-类型转换异常和校验器
Struts2框架有默认的类型转换错误拦截机制,该配置在struts-default.xml中,名叫conversionError,但是想使用需要继承ActionSupport. 默认的错误提示信息是 ...
- 奥卡姆剃刀原则在ERP项目的应用
一向崇信“奥卡姆剃刀原则”,如非必要,绝不新增. 在我所实施的项目中,自定义字段.自定义报表非常少.很极端的一个例子是,曾经有一家工厂,生产打印机的部件,产品百分之百外销. 在项目实施完成,成功上线后 ...
- vueCli 运行报错
error 如下: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! shopping@0.1.0 serve: `vue-cli-service ...
- dubbo(三):负载均衡实现解析
dubbo作为分布式远程调用框架,要保证的点很多,比如:服务注册与发现.故障转移.高性能通信.负载均衡等等! 负载均衡的目的是为了特定场景下,能够将请求合理地平分到各服务实例上,以便发挥所有机器的叠加 ...
- Labyrinth 树的直径加DFS
The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is d ...
- SpringBoot集成Shiro实现权限控制
Shiro简介 Apache Shiro是一个功能强大且易于使用的Java安全框架,用于执行身份验证,授权,加密和会话管理.使用Shiro易于理解的API,您可以快速轻松地保护任何应用程序-从最小的移 ...
- [PHP][mysql] 需要知道的那些事
就是想总结一下自己不会的! sql: 1.在SQL语句中出现AS,是起别名的意思! 例子:select a.* from table_1 as a就是给table_1起个别名叫a,因此前面就可以使用a ...