zoukankan      html  css  js  c++  java
  • Centrifugo  语言无关的实时消息服务

    Centrifugo 语言无关的实时消息服务,基于golang编写,提供了websocket 以及sockjs 的兼容处理,使用上很简单
    同时也支持基于redis的扩展,以下是一个简单的运行测试

    环境准备

    • docker-compose 文件
     
    version: "3"
    services: 
      centrifugal:
        image: centrifugo/centrifugo
        command: centrifugo -c config.json --engine=redis --redis_host=redis --redis_port=6379
        ports: 
        - "8000:8000"
        volumes: 
        - "./config:/centrifugo"
        ulimits:
          nproc: 65536
          nofile:
            soft: 65536
            hard: 65536
      redis:
        image: redis
        ports: 
        - "6379:6379"
     
    • 配置文件

      主要是关于运行时的参数,测试使用,实际需要调整

    {
      "secret": "05f0842d-c302-4036-a19f-6ac263b9f620",
      "admin_password": "ca0e58bb-5fde-43b6-adce-b62392420ffc",
      "admin_secret": "b10b2ab3-8e29-428b-85cb-42a32ba6ea57",
      "api_key": "cbf46e80-3e00-4642-8f3a-369b8707304d",
      "anonymous": true,
      "publish": true,
      "subscribe_to_publish": true,
      "presence": true,
      "debug":true,
      "client_anonymous":true,
      "join_leave": true,
      "history_size": 10,
      "history_lifetime": 300,
      "history_recover": true,
      "prometheus": true
    }

    nodejs web 集成

    包含了jwt 生成以及以及简单的基于官方nodejs sdk 的demo(集成sockjs)

    • package.json
    {
      "name": "web",
      "version": "1.0.0",
      "main": "index.js",
      "license": "MIT",
      "dependencies": {
        "centrifuge": "^2.2.1",
        "jsonwebtoken": "^8.5.1",
        "sockjs-client": "^1.3.0"
      },
      "scripts": {
        "s":"node app.js"
      }
    }
    • app.js
    var Centrifuge = require("centrifuge")
    var SockJS = require('sockjs-client');
    var jwt = require('jsonwebtoken');
    var token = jwt.sign({ sub: 'dalongdemo'}, '05f0842d-c302-4036-a19f-6ac263b9f620');
    var centrifuge = new Centrifuge("http://localhost:8000/connection/sockjs", {
      sockjs: SockJS
    })
    centrifuge.setToken(token)
    centrifuge.subscribe("news", function(message) {
        console.log(message);
    });
    centrifuge.connect();
     

    运行&&测试

    • 启动服务
    docker-compose up -d
    • 启动nodejs demo 应用
    cd web
    yarn 
    yarn s
    • 通过api 发布消息
    curl -X POST 
      http://localhost:8000/api 
      -H 'Authorization: apikey cbf46e80-3e00-4642-8f3a-369b8707304d' 
      -d '{
        "method": "publish",
        "params": {
            "channel": "news",
            "data": {
                "text": "dalongrong"
            }
        }
    }'
     
    • 效果

    • redis key

    说明

    Centrifugo 还是比较方便的,使用起来也比较简单,实际上类似的工具还是很多的,nchan。。。 都挺不错的,同时官方文档还是不错的
    内容比较详细

    参考资料

    https://github.com/centrifugal/centrifugo
    https://nchan.io/
    https://github.com/rongfengliang/centrifugo-docker-compose

  • 相关阅读:
    1036 商务旅行
    4165 ​高精度求阶乘
    3117 高精度练习之乘法
    封装代理
    freemarker写select组件(三)
    VC双缓冲画图技术介绍
    JS学习十七天----工厂方法模式
    Struts2 全局结果集
    HDU 1730 Northcott Game
    C++ STL:vector
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/11367765.html
Copyright © 2011-2022 走看看