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的更多相关文章

  1. PHPSocket.IO知识学习整理

    一.服务端和客户端连接 1.创建一个SocketIO服务端 <?php require_once __DIR__ . '/vendor/autoload.php'; use Workerman\ ...

  2. web socket (记录下来方便观看)

    Web Sockets HTML5 WebSocket 设计出来的目的就是要取代轮询和 Comet 技术,使客户端浏览器具备像 C/S 架构下桌面系统的实时通讯能力. 浏览器通过 JavaScript ...

  3. web推送

    WEB消息推送框架 web-msg-sender是一款web长连接推送框架,采用PHPSocket.IO开发,基于WebSocket长连接通讯,如果浏览器不支持WebSocket则自动转用comet推 ...

  4. web-msg-send 学习 http://www.workerman.net/web-sender

    WEB消息推送框架 web-msg-sender是一款web长连接推送框架,采用PHPSocket.IO开发,基于WebSocket长连接通讯,如果浏览器不支持WebSocket则自动转用comet推 ...

  5. Notadd 2.0 全新 Node.js 版本~ (开发中) [从 PHP 到 node 的踩坑记]

    对于 Notadd 我们本来期望它实现更多... 尽管我们也尝试做了很多努力,但是由于 PHP 本身的局限,以及考虑到开发环境配置的复杂程度,最终使用了折中方案.接下来,我们谈谈整个技术选型历程,也供 ...

  6. 后台接口平台 基于Laravel 开发 快速开发数据接口

    laravelPCMS V1.5.0 项目地址:https://github.com/q1082121/laravelcms 喜欢的朋友可以支持下 点点星标 百牛信息技术bainiu.ltd整理发布于 ...

  7. thinkphp 5.0整合phpsocketio完整攻略,绕坑

    使用环境: thinkphp5.0 项目需求 前端下单,后台接受,并立即做出提示.例如:美团外卖,客户端下单成功后,商家端就会立即有接单语音提示. 开发环境 thinkphp5.0 phpsocket ...

  8. 必学PHP类库/常用PHP类库大全,php 类库分类-收集

    依赖管理( Dependency Management ) 用于依赖管理的包和框架 Composer / Packagist - 一个包和依赖管理器. Composer Installers - 一个 ...

  9. workman即时推送

    https://www.workerman.net/web-sender 下载源码解压后运行  start_for_win.bat  如果提示不成功,就把php路径配置到环境变量中去即可 运行后打开浏 ...

随机推荐

  1. (js描述的)数据结构[哈希表1.2](9)

    一. 优秀的哈希函数 1.快速的计算: 需要快速的计算来获得对应的hashCode(霍纳法则来减少乘除次数) 2.均匀的分布: 尽可能将元素映射到不同的位置,让元素在哈希表中均匀分布 二.哈希表的扩容 ...

  2. Vue+Mock.js模拟登录和表格的增删改查

    有三类人不适合此篇文章: "喜欢站在道德制高点的圣母婊" -- 适合去教堂 "无理取闹的键盘侠" -- 国际新闻版块欢迎你去 "有一定基础但又喜欢逼逼 ...

  3. Java课程设计之——爬虫篇

    主要使用的技术 Httplcient Jsoup 多线程 dao模式 网络爬虫简介 网络爬虫(又称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取 ...

  4. PDF阅读器

    1.SumatraPDF 非常小巧,打开很轻快 2.PDF Reader by Xodo window商店中可以找到,很好用 3. PDFXChange Editor这是我迄今为止见过的最好的PDF编 ...

  5. Python语言-selenium webdriver操作记录汇总

    1.控制浏览器大小 set_window_size() 设置浏览器大小 该方法有两个参数,第一个参数是宽,第二个是高 maximize_window() 设置浏览器全屏显示,无参数 chrome谷歌浏 ...

  6. [apue] getopt 可能重排参数

    看第21章时,介绍到了解析命令行的神器 getopt,了解了 linux 下处理通用命令行的方法. 命令行可分为参数与选项,其中不带 - 或 -- 前缀的为参数,对一个命令而言数量是固定的,多个参数之 ...

  7. vue组件之间值传递四种方法汇总

    1.父组件获取子组件的数据和方法 $refs 子组件: <template> <div class="header"> <h3>{{ zz }} ...

  8. [YII2] 自带分页调整

    在search Model的search()方法里有一个$dataProvider 属性 ,在这个属性数组里添加 'pagination' => ['pageSize' => 10,],设 ...

  9. HTTP Request和Response

    一.Servlet 1:实现Servlet接口 servlet生命周期: init方法:tomcat启动时 调用此方法 service方法:访问servlet时默认执行此方法 destroy方法:to ...

  10. Mysql列属性

    列属性又称之为字段属性在mysql中一共有6个属性:null,默认值(default),列描述(comment),主键(primary key),唯一键(unique key)和自动增长 修改数据库字 ...