leobenoist/socket-bundle

此包已被废弃,不再维护。未建议替代包。

Symfony LeobenoistSocketBundle

dev-master 2014-07-04 14:44 UTC

This package is auto-updated.

Last update: 2020-02-08 13:39:20 UTC


README

这是一个用于管理 Symfony 2 中 Socket.IO 的包,尽可能地减少 JavaScript 的使用。

请注意,此包处于开发中,不建议在生产环境中使用。

先决条件

Symfony 2.1+ 
FOSUserBundle
NodeJS (no knowledge required)

支持的浏览器

桌面

Internet Explorer 5.5+
Safari 3+
Google Chrome 4+
Firefox 3+
Opera 10.61+

移动

iPhone Safari
iPad Safari
Android WebKit
WebOs WebKit

安装

安装是一个简单的3步骤过程

  1. 使用 composer 下载 LeoBenoistSocketBundle
  2. 启用 Bundle
  3. 配置您的 config.yml

步骤 1:使用 composer 下载 SocketBundle

在您的 composer.json 中添加 SocketBundle

{
    "require": {
        "leobenoist/socketbundle": "dev-master"
    }
}

现在运行以下命令让 composer 下载该包

$ php composer.phar update leobenoist/socketbundle

步骤 2:启用 Bundle

在 kernel 中启用 Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new LeoBenoist\SocketBundle\LeoBenoistSocketBundle(),
    );
}

步骤 3:配置您的 config.yml

# app/config/config.yml

leobenoist_socket:
    client:
        hostname: localhost
        port: 1337
    server:
        hostname: localhost
        port: 1337

使用 SocketBundle

客户端

第一步是在您的客户端与 socket.io(我们的实时服务器)之间建立连接。

为此,只需在每个您希望具有实时功能的页面上添加此代码,或者直接在 base.html.twig 中添加。

# app/Ressources/views/base.html.twig

{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
  {{ leobenoistSocketConnect()|raw }}
{% endif %}

将客户端订阅到一个标签(从客户端接收更新)

您发送的每个更新都会被标记。您需要说明在哪个页面上需要什么更新,以及收到后如何处理。您还可以指定生成的 twig 格式。例如,html-ajax 将生成带有脚本 HTML 代码的代码。

# src/SomeRandomBundle/Ressources/views/base.html.twig

{{ leobenoistSocketRegisterLabel('yourLabel', 'yourJavacriptFunction', 'html-ajax')|raw }}

# src/SomeRandomBundle/Ressources/views/base.html.twig

{{ leobenoistSocketRegisterLabel('yourLabel' ~ app.user.id, 'yourJavacriptFunction', 'html-ajax')|raw }}
关于安全性的说明

当您使用此代码时,Symfony 2 后端会将用户订阅到特定标签的信息发送到 Node.js,然后生成允许客户端连接到实时服务器的相应 JavaScript 代码。当客户端浏览器收到此 JavaScript 代码时,它会请求将此标签注册到实时服务器。实时服务器会检查 Symfony 是否先前授予了此用户权限,如果一切正常,则用户将被授权访问该标签。

服务器端(发送更新)

非常简单 :)

// in a controller or a service

//Get the service 
$socket = $this->container->get('leobenoist_socket.service');

// Send a raw basic update
$socket->sendResponseForLabel('yourLabel', '{your data}');


// Send a symfony response object update
$response = $this->render(
    'YourRandomBundle:YourFolde:yourView.html.twig',
    array(
        'data' => $data,
    )
);

$socket->sendResponseForLabel('yourLabel', $response);

这很简单,不是吗?

启动实时服务器

node server.js

使用 https 和 nginx 进行配置的示例。

server {
    listen      80;
    server_name yourdomain.com www.yourdomain.com;
    rewrite     ^   https://www.yourdomain.com$request_uri? permanent;
}

server {
    listen 443 ssl;
    server_name www.yourdomain.com;
    root /var/www/yourdomain.com/www/web;

    ssl                   on;
    ssl_certificate      /etc/nginx/ssl/yourdomain.com.chained.crt;
    ssl_certificate_key  /etc/nginx/ssl/www.yourdomain.com.key;

    if ($host !~* ^www\.){
        rewrite ^(.*)$ https://www.yourdomain.com$1;
    }

    location / {
        # try to serve file directly, fallback to rewrite
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        # rewrite all to app.php
        rewrite ^(.*)$ /app.php/$1 last;
    }

    location /socket.io {
        proxy_pass http://localhost:1337;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;

        proxy_redirect off;
    }

    location ~ ^/(app)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS on;
    }

    error_log /var/log/nginx/www.yourdomain.com_error.log;
    access_log /var/log/nginx/www.yourdomain.com_access.log;
}

待办事项

更好的文档,纠正改进,更好的英文

Symfony和Node之间的套接字通信

通过Composer使包可用