php websocket类库,一个简单的PHP WebSocket实现

Wrench

Simple WebSocket Client/Server for PHP

Formerly known as php-websocket

Version: 2.0.0

Build Status: b724f0041d7f6a41010440bdabb58c13.png

A simple websocket server and client package for PHP 5.3/5.4, using streams. Protocol support is based around RFC 6455, targeting the latest stable versions of Chrome and Firefox. (Suggest other clients here)

Backward compatibility

Public API

The new vendor namespace is Wrench. This namespace begins in the /lib directory, rather than server/lib.

Apart from the new namespace, the public API of this new major version is fairly compatible with that of php-websocket 1.0.0.

Protected API

The protected API has changed, a lot. Many method have been broken up into simple protected methods. This makes the Server class much easier to extend. In fact, almost all of the classes involved in your typical daemon can now be replaced or extended, including the socket handling and protocol handling.

What happened to the client dir?

The client-side libraries are no longer supported: some libraries are included but are packaged only as examples. You're free to use whatever client-side libraries you'd like with the server. If you're still using them, see the 1.0 branch.

Installation

The library is PSR-0 compatible, with a vendor name of Wrench. An SplClassLoader is bundled for convenience.

Usage

This creates a server on 127.0.0.1:8000 with one Application that listens for WebSocket requests to ws://localhost:8000/echo and ws://localhost:8000/chat:

$server = new \Wrench\BasicServer('ws://localhost:8000', array(

'allowed_origins' => array(

'mysite.com',

'mysite.dev.localdomain'

)

));

$server->registerApplication('echo', new \Wrench\Examples\EchoApplication());

$server->registerApplication('chat', new \My\ChatApplication());

$server->run();

Authors

The original maintainer and author was @nicokaiser. Plentiful improvements were contributed by @lemmingzshadow and @mazhack. Parts of the Socket class were written by Moritz Wutz. The server is licensed under the WTFPL, a free software compatible license.

Bugs/Todos/Hints

Add tests around fragmented payloads (split into many frames).

To report issues, see the issue tracker.

Examples

See server.php in the examples directory and Wrench\Application\EchoApplication

Jitt.li, a Twitter API sample project.

For Symfony2, VarspoolWebsocketBundle extends this library for use with the Service Container.

实现基于Websocket 协议的 PHP类库 和 javascript类库实现事件 回调函数的架构开发,以一个简单聊天室实例介绍其使用方法: 不同浏览器,不同电脑,不同地域, 实时通讯。实现的详细 请研究源码。 /***服务端****/ /**  * 实例化  */ $io = new SocketIO('127.0.0.1',8000); /**  * 监听连接  */ $io->on('connect',function($ws,$uid){ $msg = "任意数据类型,结构需要和前端协议,便于通信";     $ws->broadcast(evet, $msg);     //$ws->emit(evet, $uid, $msg); }); /**  * 任意事件,与前端协议好,// evet 由开发者定义  */ $io->on('event',function($ws,$uid,$msg){     $msg = "任意数据类型,结构需要和前端协议,便于通信";     $ws->broadcast(evet, $msg); }); /* * 关闭 */ $io->on('close',function($ws,$uid,$err){     // evet 由开发者定义     $msg = "任意数据类型,结构需要和前端协议,便于通信";     $ws->broadcast('close', $msg); }); /**  * 启动  */ $io->run();   /*** 客户端 ***/ var io = new SocketIO('127.0.0.1',8000); io.on('connect',function(){      console.log('open');      // 发出请求, event 由开发者定义      io.emit(event, user, function(ok){           if(ok){                        }else{                           }      });      // 收到消息, event 由开发者定义      io.on(event, function(msg){                        console.log(msg);      });      //关闭事件  io.on('close', function(){ }); }); 标签:phpws
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值