Code Monkey home page Code Monkey logo

yii2-websocket's Introduction

yii2-websocket

在 yii2 下运行 WebSocket 服务。

Latest Stable Version Total Downloads License

驱动支持

安装

安装此扩展程序的首选方法是通过 composer.

编辑运行

php composer.phar require --prefer-dist yiiplus/yii2-websocket "^1.0.0"

或添加配置到项目目录下的composer.json文件的 require 部分

"yiiplus/yii2-websocket": "^1.0.0"

基本使用

每个 channel 的功能都需要定义一个单独的类。例如,如果你需要为指定客户端推送一条消息,则该类可能如下所示:

namespace xxx\channels;

class PushMessageChannel extends BaseObject implements \yiiplus\websocket\ChannelInterface
{
	public function execute($fd, $data)
	{
		return [
			$fd, // 第一个参数返回客户端ID,多个以数组形式返回
			$data // 第二个参数返回需要返回给客户端的消息
		];
	}

	public function close($fd)
	{
		return;
	}
}

以下是从客户端发送消息的方法:

Yii::$app->websocket->send(['channel' => 'push-message', 'message' => '用户 xxx 送了一台飞机!']);

执行任务的确切方式取决于使用的驱动程序。 大多数驱动程序可以使用控制台命令运行,组件需要在应用程序中注册。

此命令启动一个守护进程,该守护进程维护一个 WebSocket Server,根据客户端发来的数据,处理相关 channel 的任务:

yii websocket/start

有关驱动程序特定控制台命令及其选项的更多详细信息,请参阅 文档

yii2-websocket's People

Contributors

gengxiankun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yii2-websocket's Issues

fd关闭或不存在时会报错

if($server->exist($fd)) {
if (!$server->push($fd, $data)) {
echo '[error] client_id ' . $fd . ' send failure.' . PHP_EOL;
return false;
}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.