votong/websocket-async 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

votong/websocket-async

Composer 安装命令:

composer require votong/websocket-async

包简介

Laravel Websocket based on Ratchet, support pub/sub feature.

README 文档

README

WebSocket server based on Ratchet. Built to be completely event driven so it can used in several different project without having to manipulate the base code.

Installation

Add require and repositories information in the projects composer.json file:

"require": {
        ...
        "freestream/websocket": "1.*"
        ...
    },
    "repositories": [
        ...
        {
            "type": "vcs",
            "url": "git@github.com:freestream/laravel-websocket.git"
        }
        ...
    ],

Now its time to run composer update in your terminal.

After the update is complete the service provider needs to be registered in app/config/app.php inside the providers array:

'Freestream\WebSocket\WebSocketServiceProvider',

Server side configuration

Run the following command in the projects root folder to startup the WebSocket server. By default the server will be run on port 8080 but by adding --port=[number] at the end of the command it is possible to change to any desired port.

php artisan websocket:start

This will startup a deamon service that will estsblish the WebSocket server. To make sure that command is constantly running it is recommended to use Supervisord to supervice the deamon.

Client side listener

This service comes included with the nessesary JavaScripts. To include these into the projects assets folder run the following command.

php artisan asset:publish freestream/web-socket

After that add thins line into the template file.

<script type="text/javascript" src="{{ URL::asset('packages/freestream/web-socket/js/WebSocketClient.js') }}"></script>

To estalish a WebSocket client add the following code.

<script type="text/javascript">
    var webSocketClient = new WebSocketClient({
        prefix: 'Custom.Event.Prefix',
        server: 'localhost',
        port: '8080',
    });
</script>

The avilible configurations are:

debug       boolean     Enabled debug messages in browser console. Default is false.
prefix      string      Event firing prefix. Default 'Laravel.Freestream.WebSocket'
server      string      WebSocket server address. Default 'localhost'
port        integer     WebSocket server port. Default 8080
sessionId   string      Session ID for the opened WebSocket. Default random integer.
reconnect   boolean     Should reconnect automatically if losing connection. Default true.

Messages can be sent through the socket as soon as the connection is established. The first parameter is a event name that will be sent to the backend as a tracing event for easyer filtering. The second parameter is the message and can contain a string or a JSON.

<script type="text/javascript">
    webSocketClient.message('event-name', 'This is my message');
</script>

Messages that are sent back from the server to the client contains a JSON with two elements, origData and data. OrigData contains any data that have been sent as a message and the server has responded to and data contains the reponse data from the server.

{
    origData: {
        ...
    },
    data: {
        ...
    },
}

Add Event Listensers to responed/listen to anything that happneds in the WebSocket.

document.addEventListener('Laravel.Freestream.WebSocket.Message.Received', function(event) {});

Events that will be fired is:

[PREFIX].Error
[PREFIX].Message.Received
[PREFIX].Connection.Established
[PREFIX].Connection.Closed

Server side listener

The server needs to be able to respond to any new connections or messages that are sent by the client. This is done by Laravels event listener. This can be setup in different ways but to recommented way is to use events.php.

If events.php is not already in the 'app/' folder create the file and after that open up 'app/start/global.php' and make sure the follwoing line is in the end of the file.

require app_path().'/events.php';

All events that will be handled by the server should now be placed inside events.php:

<?php
Event::listen('Laravel.Freestream.WebSocket.Listener.Open', function($connection, $clients, $listener){
    ...
});

Event::listen('Laravel.Freestream.WebSocket.Listener.Open.After', function($connection, $clients, $listener){
    ...
});

Event::listen('Laravel.Freestream.WebSocket.Listener.Message', function($from, $raw, $clients, $listener) {
    ...
});

Event::listen('Laravel.Freestream.WebSocket.Listener.Close', function($connection, $clients, $listener) {
    ...
});

$connection or $from (depending on the event) has the following functions.

setSessionId('id')
setEvent('name')
send('string')

The send function will send the message to the client. If an event or sessionId needs to be specified this must be done before the message is sent.

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-10-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固