Online chat based on web sockets and ratchet php
![Demo] (doc/demo.gif)
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist joni-jones/yii2-wschat
or add
"joni-jones/yii2-wschat": "*"
to the require section of your composer.json
file.
The chat extension can use any database storage supported by yii.
If mongodb
extension specified the chat will be try to use it as message history storage, otherwise extensionwill be use specified in application config db component.
The simple example how to use mongodb storage is listed below.Install MongoDB and yii2-mongodbextension to store messages history and you need just specify connection in console
config:
'components' => [
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://username:password@localhost:27017/dbname'
]
]
In created mongodb database you need to create collection named as history
;
IMPORTANT: if you use db component - you need to create table history
in your database.The simple examples postgresql and mysql you can see in tests/codeception
directory.
To start chat server need to create console command and setup it as demon:
Create controller which extends yii\console\Controller
:
ServerController extends \yii\console\Controller
Create action to start server:
namespace app\commands;
use jones\wschat\components\Chat;
use jones\wschat\components\ChatManager;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
class ServerController extends \yii\console\Controller
{
public function actionRun()
{
$server = IoServer::factory(new HttpServer(new WsServer(new Chat(new ChatManager()))), 8080);
$server->run();
}
}
If you want to use chat for auth users, you must to specify userClassName
property for ChatManager
instance.For example:
$manager = Yii::configure(new ChatManager(), [
'userClassName' => '\yii\db\ActiveRecord' //allow to get users from MySQL or PostgreSQL
]);
Now, you can run chat server with yii
console command:
yii server/run
To add chat on page just call:
<?php echo ChatWidget::widget();?>
or if you want to use chat for auth users just add config as parameter:
<?php echo ChatWidget::widget([
'auth' => true,
'user_id' => '' // setup id of current logged user
]);?>
List of available options:
auth - boolean, default: false
user_id - mixed, default: null
port - integer, default: 8080
chatList - array (allow to set list of preloaded chats), default: [
id => 1,
title => 'All'
],
add_room - boolean, default: true (allow to user create new chat rooms)
You can also store added chat, just specify js callback for vent events:
Chat.vent('chat:add', function(chatModel) {
console.log(chatModel);
});
This code snipped may be added in your code, but after chat widget loading. In the callback you will get access to Chat.Models.ChatRoom
backbone model. Now, you need add your code to save chat room instead console.log()
.
If
YII_DEBUG
is enabled - all js scripts will be loaded separately.
Also by default chat will try to load two images:/avatar_16.png
and /avatar_32.png
from assets folder.
If you don't see any messages in console log, check flushInterval
and exportInterval
of your log configuration component. The simple configuration may looks like this:
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'flushInterval' => 1,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning', 'info'],
'logVars' => [],
'exportInterval' => 1
],
],
],
If you use https
protocol chat will try to connect to wss
instead ws
. But Ratchet PHP does not support work via SSL, soyou need to use some proxy like stunnel.
MIT
创建测试文件 ./vendor/bin/codecept g:test unit Topic #创建一个名为TopicTest的单元测试类 ./vendor/bin/codecept g:cest functional TopicCest #创建名为TopicCest的功能测试类 运行测试 vendor/bin/codecept run #运行所有测试 vendor/bin/codecept
<?= DetailView::widget([ 'model' => $model, 'attributes' => [ 'id', 'name', //'status', //要显示状态名称,不是数字,改成下面这样: [ 'label
数据模型层 models/ZsDynasty.php [php] view plain copy <?php namespace app\models; use yii\db\ActiveRecord; class ZsDynasty extends ActiveRecord { } ZsDynasty 对应数据库表 zs_dynasty 控制器
Yii2-workerman框架是基于 Yii2 和 PHPSocket.IO 做的二次开发 gitgub地址:https://github.com/zhengpeiqiang/Yii2-workerman
YII2日常所用的教程 作者:Terry,2014年开始使用Yii2,用yii2做了几个线上项目,包括:外贸B2C电商,ERP,营销系统,统计系统等。本博客致力于研究Yii2的源码,研究他的内在机制,以及一些扩展的经验。将YII2日常所用的教程,日常的学习经验,与大家分享,如果您有想要了解yii2方面的一些知识,但是本文没有,您可以发送邮件到2358269014@qq.com或者zqy234@12
安装composer,并执行: # composer global require "fxp/composer-asset-plugin:~1.1.0" 根据模板创建项目 # composer create-project --prefer-dist yiisoft/yii2-app-basic basic # composer create-project --prefer-dist --sta
$query->where([ 'status' => 10, 'type' => null, 'id' => [4, 8, 15], ]); 相当于sql 中的 ...WHERE (`status` = 10) AND (`type` IS NULL) AND (`id` IN (4, 8, 15)) 。 $query->filterWhere([ 'status' => $A, 'type'
Html::img(); Url::to(); Json::encode(); 转载于:https://www.cnblogs.com/meetuj/p/11319898.html
Yii2里 model在使用load方法加载浏览器的值的时候,会进行rules验证。这时候可以使用场景,让model对不同场景使用不同验证方式: model中: //设定在不同场景下面验证的规则 public function scenarios() { $scenarios = parent::scenarios(); $scenarios
https://github.com/yiisoft/yii2-smarty php composer.phar require –prefer-dist yiisoft/yii2-smarty return [ //.... 'components' => [ 'view' => [ 'renderers' => [
Yii 2 完全根据 Yii 1.1 版本重写,后者是最流行的 PHP 框架之一。Yii 2 继承了 Yii 的简洁、快速、和高扩展性。Yii 2 需要 PHP 5.4,并且拥有现代 Web 应用开发中最好的实例和协议。
问题内容: 我想以以下方式实现sql查询: 我该如何使用或上面提到的方法来做到这一点? 问题答案: 是一个方法从查询类。 您可以尝试这样的事情。
openadm/yii2-admin 基于yii2-extension包管理和adminlte2主题构建的admin项目,包括了基础的用户管理,RBAC管理,扩展管理等核心功能。 去OpenADM 查看更多 Installation The preferred way to install this extension is through composer. Either run php com
YII2接口文档工具: 使用注释自动生成基于YII2的接口文档,自动获取模型中注释。 安装方式 composer require chatfeed/yii2-doc "*" 配置 'module'=>[ 'doc' => [ 'class' => 'cfd\doc\Module', 'modelDescriptions'=>requ
yii2-apistore 是适用于 Yii2 的 ApiStore SDK 扩展。 该扩展为 Yii framework 添加 ApiStore 客户端。
Hau 是用Yii2框架搭建的API模板 后台使用 vue + ElementUI 后台前端项目:hau-vue-admin-template 后台 用户:13112345678 密码:admin.len168 效果图: