安装并使用 websocket 统计 sym 在线人数

前言

首先,对 websocket 要有一个简要的了解与认识

websocket 是 HTML5 开始提供的一种浏览器与服务器进行全双工通讯的网络技术,属于应用层协议。

它基于 TCP 传输协议,并复用 HTTP 的握手通道。

但是如果对于我们来说可以很简单的这样来理解:

1、它可以被用于浏览器

2、它可以实现双向沟通

3、它操作很简单

本次安装使用 centos7 版本系统

一、安装 nodejs

在 Linux 系统安装 Nodejs

二、安装 redis

建站不啰嗦,上手跟我做(八)redis 下载和安装

三、安装 websocket

用 npm 安装 laravel-echo-server

[root@iz2ze70zrtwzm9p0l3p86nz ~]# npm install -g laravel-echo-server

创建一个目录用于存放安装后的文件

[root@iz2ze70zrtwzm9p0l3p86nz ~]# cd /opt
[root@iz2ze70zrtwzm9p0l3p86nz opt]# mkdir echo-server
[root@iz2ze70zrtwzm9p0l3p86nz opt]# cd echo-server/
[root@iz2ze70zrtwzm9p0l3p86nz echo-server]# 

开始安装

[root@iz2ze70zrtwzm9p0l3p86nz echo-server]# /opt/nodejs/bin/laravel-echo-server init

接下来你会看到如下信息,我会告诉你怎么选择

? Do you want to run this server in development mode? Yes
? Which port would you like to serve from? 6001 ? Which database would you like to use to store presence channel members? redis
? Enter the host of your Laravel authentication server. http://localhost
? Will you be serving on http or https? http
? Do you want to generate a client ID/Key for HTTP API? Yes
? Do you want to setup cross domain access to the API? Yes
? Specify the URI that may access the API: http://localhost:80 ? Enter the HTTP methods that are allowed for CORS: GET, POST
? Enter the HTTP headers that are allowed for CORS: Origin, Content-Type, X-Auth-Token, X-Requested-W
ith, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id
从上到下分别为
运行服务的时候是否在开发模式?
该服务的访问端口?
希望连接的数据库?
私人频道验证的url域名
希望使用http还是htpps来通过浏览器访问该服务现有的频道信息?
是否生成客户端访问该服务API的ID/key?
是否允许跨域访问API?
通过浏览器访问该服务现有的频道信息的URL
允许使用哪些方法跨域访问?(一般是GET和POST)
允许使用哪些头部跨域访问?
[root@iz2ze70zrtwzm9p0l3p86nz echo-server]# vim laravel-echo-server.json

        "authEndpoint": "/broadcasting/auth",
        "clients": [
                {
                        "appId": "3668ee3eb205a821",
                        "key": "a50117cdb843faa6a74c230e6fe406b4"
                }
        ],
        "database": "redis",
        "databaseConfig": {
                "redis": {},
                "sqlite": {
                        "databasePath": "/database/laravel-echo-server.sqlite"
                }
        },
        "devMode": false,
        "host": null,
        "port": "6001",
        "protocol": "http",
        "socketio": {},
        "sslCertPath": "",
        "sslKeyPath": "",
        "sslCertChainPath": "",
        "sslPassphrase": "",
        "subscribers": {
                "http": true,
                "redis": true
        },
        "apiOriginAllow": {
                "allowCors": true,
                "allowOrigin": "https://qiankunpingtai.cn",
                "allowMethods": "GET, POST",
                "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
        }
}

开始尝试使用 websocket

[root@iz2ze70zrtwzm9p0l3p86nz echo-server]# /opt/nodejs/bin/laravel-echo-server start

会出现如下信息:

[root@iz2ze70zrtwzm9p0l3p86nz echo-server]# /opt/nodejs/bin/laravel-echo-server start

L A R A V E L  E C H O  S E R V E R

version 1.5.0

Starting server...

✔  Running at localhost on port 6001
✔  Listening for http events...
✔  Listening for redis events...

Server ready!

即证明你的 websocket 已经安装成功!

nginx 反向代理 websocket

user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
        map $http_upgrade $connection_upgrade {
                default upgrade;
                '' close;
        }

    include       mime.types;
    default_type  application/octet-stream;
    client_max_body_size 20M;
    client_body_buffer_size 128k;

    sendfile        on;
    keepalive_timeout  65;
    server {
        listen 443 ssl;
        server_name qiankunpingtai.cn; # 你的域名
        ssl_certificate  /usr/local/nginx/cert/200543_qiankunpingtai.cn.pem; # 改成你的证书的名字
        ssl_certificate_key /usr/local/nginx/cert/200543_qiankunpingtai.cn.key; # 你的证书的名字
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        location /symphony {
                proxy_pass        http://localhost:8081/symphony;
                proxy_redirect default;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;
                proxy_set_header Host $host:$server_port;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }
        location / {
                proxy_pass        https://qiankunpingtai.cn/symphony;
                proxy_redirect default;
                proxy_set_header Host $host:$server_port;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
 server {
           listen 80;
           server_name qiankunpingtai.cn; #你的域名
            rewrite ^(.*)$ https://$host$1 permanent; # 把http的域名请求转成https
        }

}

查看在线人数统计效果
图片.png