使用nginx-push-stream-module

xiaoxiao2021-02-28  26

首先需要下载或者克隆插件,地址在https://github.com/wandenberg/nginx-push-stream-module。如果是和nginx一起安装只需要在configure时加上--add-module=模块路径即可,下面说一下配置方式。

  安装后需要在nginx的配置文件nginx.conf或者其他包含的配置文件中做如下配置:

在http段加入

#最大共享内存 push_stream_shared_memory_size 100m; #频道最大长度 push_stream_max_channel_id_length 200; #每个频道缓存的最大消息数量 push_stream_max_messages_stored_per_channel 20; #消息生命周期(分钟) push_stream_message_ttl 5m;

添加一个虚拟主机(server)配置如下

server { listen 8080; server_name 127.0.0.1; location /pub { push_stream_publisher admin; push_stream_channels_path $arg_id; push_stream_store_messages on; } location ~ /sub/(.*) { push_stream_subscriber; push_stream_channels_path $1; push_stream_last_received_message_time "$arg_time"; push_stream_last_received_message_tag "$arg_tag"; push_stream_header_template "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta http-equiv=\"Cache-Control\" content=\"no-store\">\r\n<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\r\n<meta http-equiv=\"Pragma\" content=\"no-cache\">\r\n<meta http-equiv=\"Expires\" content=\"Thu, 1 Jan 1970 00:00:00 GMT\">\r\n<script type=\"text/javascript\">\r\nwindow.onError = null;\r\ntry{ document.domain = (window.location.hostname.match(/^(\d{1,3}\.){3}\d{1,3}$/)) ? window.location.hostname : window.location.hostname.split('.').slice(-1 * Math.max(window.location.hostname.split('.').length - 1, (window.location.hostname.match(/(\w{4,}\.\w{2}|\.\w{3,})$/) ? 2 : 3))).join('.');}catch(e){}\r\nparent.PushStream.register(this);\r\n</script>\r\n</head>\r\n<body>"; push_stream_message_template "<script>p(~id~,'~channel~','~text~');</script>"; push_stream_footer_template "</body></html>"; default_type "text/html; charset=utf-8"; push_stream_ping_message_interval 10s; } location ~ /ev/(.*) { push_stream_subscriber eventsource; push_stream_channels_path $1; push_stream_last_received_message_time "$arg_time"; push_stream_last_received_message_tag "$arg_tag"; push_stream_message_template "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\",\"tag\":\"~tag~\",\"time\":\"~time~\"}"; push_stream_ping_message_interval 10s; } location ~ /ws/(.*) { push_stream_subscriber websocket; push_stream_channels_path $1; push_stream_last_received_message_time "$arg_time"; push_stream_last_received_message_tag "$arg_tag"; push_stream_message_template "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\",\"tag\":\"~tag~\",\"time\":\"~time~\"}"; push_stream_ping_message_interval 10s; } location ~ /lp/(.*) { push_stream_subscriber long-polling; push_stream_channels_path $1; push_stream_last_received_message_time "$arg_time"; push_stream_last_received_message_tag "$arg_tag"; push_stream_message_template "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\",\"tag\":\"~tag~\",\"time\":\"~time~\"}"; push_stream_longpolling_connection_ttl 30s; } }

然后重新载入nginx的配置,可以在Linux命令下使用curl做如下测试(其中ch1代表频道)

#发布信息: curl http://localhost:8080/pub?id=ch1 -d "test message" #订阅信息: curl http://localhost:8080/lp/ch1
转载请注明原文地址: https://www.6miu.com/read-2800149.html

最新回复(0)