2017 © Pedro Peláez
 

yii2-extension yii2-websocket

yii2 websocket

image

jianyan74/yii2-websocket

yii2 websocket

  • Monday, May 28, 2018
  • by jianyan74
  • Repository
  • 2 Watchers
  • 7 Stars
  • 443 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 8 Versions
  • 37 % Grown

The README.md

Yii2 WebSocket

即时通讯Demo, (*1)

问题反馈

在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流, (*2)

QQ群:655084090, (*3)

前提

服务器安装swoole, (*4)

 git clone https://github.com/swoole/swoole-src.git
 cd swoole-src
 phpize
 ./configure --enable-openssl -with-php-config=[PATH] #注意[PATH]为你的php地址 开启ssl用
 make && make install
 ```
### 安装

composer执行

composer require "jianyan74/yii2-websocket", (*5)


或者在 `composer.json` 加入

"jianyan74/yii2-websocket": "^1.0", (*6)

### 配置

 在 `common/config/main.php` 加入以下配置
  ```
     'redis' => [
         'class' => 'yii\redis\Connection',
         'hostname' => 'localhost',
         'port' => 6379,
         'database' => 0,
     ],
  ```
 在 `console/config/main.php` 加入以下配置。(注意:配置在controllerMap里面)

 ```
// webSocket
'websocket' => [
    'class' => 'jianyan\websocket\console\WebSocketController',
    'server' => 'jianyan\websocket\server\WebSocketServer', // 可替换为自己的业务类继承该类即可
    'host' => '0.0.0.0',// 监听地址
    'port' => 9501,// 监听端口
    'type' => 'ws', // 默认为ws连接,可修改为wss
    'config' => [// 标准的swoole配置项都可以再此加入
        'daemonize' => false,// 守护进程执行
        'task_worker_num' => 4,//task进程的数量
        // 'ssl_cert_file' => '',
        // 'ssl_key_file' => '',
        'pid_file' => __DIR__ . '/../../backend/runtime/logs/server.pid',
        'log_file' => __DIR__ . '/../../backend/runtime/logs/swoole.log',
        'log_level' => 0,
    ],
],
 ```

 ### 使用

  ```
  # 启动 
  php ./yii websocket/start
  # 停止 
  php ./yii websocket/stop
  # 重启 
  php ./yii websocket/restart
   ```

### 测试

, (*7)

// 聊天
function say(msg){
    if(msg){
        var data = '{"type":"say","to_client_id":"all","content":'+ msg +'}';
        ws.send(data);
    }
}

, (*8)


### 接口文档 > 发送消息的格式全部以json字符串发过来 ### 心跳 请求地址

ws://[to/you/url]:9501, (*9)


参数 参数名 | 说明 ---|--- type | pong 无返回 ### 进入房间 请求地址

ws://[to/you/url]:9501, (*10)


参数 参数名 | 说明 ---|--- type | login room_id | 房间id user_id | 用户id nickname | 用户昵称 head_portrait | 用户头像 返回(1)

{   "type":"login",   "from_client_id":2,   "to_client_id":"all",   "time":"2018-04-10 16:41:29",   "count":"1",   "member":{     "fd":2,     "room_id":10001,     "user_id":1,     "nickname":"隔壁老王",     "head_portrait":"123"   } }, (*11)


返回(2) > 当前登录的人还会返回一个在线列表

{   "type":"list",   "from_client_id":2,   "to_client_id":2,   "time":"2018-04-10 16:41:29",   "list":[{   "fd":2,   "room_id":10001,   "user_id":1,   "nickname":"隔壁老王",   "head_portrait":"123" }] }, (*12)


### 发言 请求地址

ws://[to/you/url]:9501, (*13)


参数 参数名 | 说明 ---|--- type | say to_client_id | 对谁说话:默认 all content | 内容 返回

{   "type":"say",   "from_client_id":2,   "to_client_id":"all",   "time":"2018-04-10 16:43:00",   "content":"123" }, (*14)


### 送礼物 请求地址

ws://[to/you/url]:9501, (*15)


参数 参数名 | 说明 ---|--- type | gift gift_id | 礼物id 返回

{ "type": "gift", "from_client_id": 4, "to_client_id": "all", "gift_id": "礼物id", "time": "2018-03-06 11:27:15" }, (*16)


### 离开房间 请求地址

ws://[to/you/url]:9501, (*17)


参数 参数名 | 说明 ---|--- type | leave 返回

{ "type": "leave", "from_client_id": 1, "to_client_id": "all", "count": 2, "time": "2018-03-06 11:27:15" } ```, (*18)

The Versions

28/05 2018

dev-master

9999999-dev https://github.com/jianyan74/yii2-websocket

yii2 websocket

  Sources   Download

Apache-2.0

The Requires

 

by 简言

php yii2 yii websocket swoole jianyan74

11/04 2018

1.1.1

1.1.1.0 https://github.com/jianyan74/yii2-websocket

yii2 websocket

  Sources   Download

Apache-2.0

The Requires

 

by 简言

php yii2 yii websocket swoole jianyan74

20/03 2018

1.0.5

1.0.5.0 https://github.com/jianyan74/yii2-websocket

yii2 websocket

  Sources   Download

Apache-2.0

The Requires

 

by 简言

php yii2 yii websocket swoole jianyan74

07/03 2018

1.0.4

1.0.4.0 https://github.com/jianyan74/yii2-websocket

yii2 websocket

  Sources   Download

Apache-2.0

The Requires

 

by 简言

php yii2 yii websocket swoole jianyan74

03/03 2018

1.0.3

1.0.3.0 https://github.com/jianyan74/yii2-websocket

yii2 websocket

  Sources   Download

Apache-2.0

The Requires

 

by 简言

php yii2 yii websocket swoole jianyan74

03/03 2018

1.0.2

1.0.2.0 https://github.com/jianyan74/yii2-websocket

yii2 websocket

  Sources   Download

Apache-2.0

The Requires

 

by 简言

php yii2 yii websocket swoole jianyan74

03/03 2018

1.0.1

1.0.1.0 https://github.com/jianyan74/yii2-websocket

yii2 websocket

  Sources   Download

Apache-2.0

The Requires

 

by 简言

php yii2 yii websocket swoole jianyan74

03/03 2018

1.0.0

1.0.0.0 https://github.com/jianyan74/yii2-websocket

yii2 websocket

  Sources   Download

Apache-2.0

The Requires

 

by 简言

php yii2 yii websocket swoole jianyan74