定制 hannesvdvreken/socketio 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hannesvdvreken/socketio

最新稳定版本:1.0.0-alpha.1

Composer 安装命令:

composer require hannesvdvreken/socketio

包简介

Pushing Socket.io messages to Redis for socket.io's Redis socket manager.

README 文档

README

What does it do?

This package allows you to transfer socket message to a Node.js process via Redis. Socket.io has a redis adapter to allow scaling a Node.js cluster to send socket messages to any client connected to any process. We can use this to send messages straight from PHP.

This package provides a Socketio\Socketio class with similar methods as the socket.io server in Node.js such as emit, in, to, of and adapter.

Usage

First we can create a Socketio\Socketio object.

$io = new Socketio\Socketio;

It has some of the same methods as in Node.js:

Namespace

$chats = $io->of('/chats');

$io->emit('general', $message);
$chats->emit('user.connected', $user);

Setup a server object with a different namespace. The $io object keeps the same namespace. Or use a callback:

$io->of('/chats', function ($chats) {
    $chats->emit('user.connected', $user);
});

$io->emit('general', $message);

Rooms

Send to specific room(s):

$io->to('room-a')->emit('event', $message);
$io->to('room-a')->to('room-b')->emit('event', $message);
$io->in('room-c')->emit('event', $message);

Where in is an alias of to. You can send to several rooms at the same time by chaining to calls. The rooms are reset after every emit call. This is similar behaviour as in Node.js.

Adapters

By default, the Socketio\Socketio object configures a Memory adapter. This adapter fakes socket message delivery and can be useful for testing/stubbing.

To setup the Redis adapter you need to configure some dependencies first.

$predis = new Predis\Client;
$msgpack = new Socketio\Utils\Msgpack;

$adapter = new Socketio\Adapters\Redis($predis, $msgpack);
$io->adapter($adapter);

If you have ext-msgpack installed, the Socketio\Utils\Msgpack will use the native msgpack_pack and msgpack_unpack methods which are supposed to be the fastest around.

If not, you can pass it Msgpack\Encoder and Msgpack\Decoder objects to do the encoding/decoding. These come pre-installed with composer if you require this package.

$msgpack = new Socketio\Utils\Msgpack(new Msgpack\Encoder, new Msgpack\Decoder);

The Node.js process

var app = require('express')();
var socketio = require('socket.io');
var adapter = require('socket.io-redis');

var server = app.listen(process.env.NODE_PORT || 9000);

io = socketio(server);
io.adapter(adapter());

Right now, you're good to go!

Contributing

Stick to PSR-2 Coding Standards and be nice in communication.

License

MIT

统计信息

  • 总下载量: 27
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固