nginx push_upstream模块的websocket

阅读更多
参考
https://www.rails365.net/articles/websocket-wai-pian-nginx-push-stream-module-mo
安装
git clone https://github.com/wandenberg/nginx-push-stream-module
./configure --add-module=/Users/haoning/tool/nginx/nginx-push-stream-module --prefix=/usr/local/nginx_push_stream
make
make  install

nginx配置加入
	push_stream_shared_memory_size 32M;
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

		location /channels-stats {
            # activate channels statistics mode for this location
            push_stream_channels_statistics;

            # query string based channel id
            push_stream_channels_path               $arg_id;
        }

        location /pub {
            # activate publisher (admin) mode for this location
            push_stream_publisher admin;

            # query string based channel id
            push_stream_channels_path               $arg_id;
        }

        location ~ /ws/(.*) {
            # activate websocket mode for this location
            push_stream_subscriber websocket;

            # positional channel path
            push_stream_channels_path                   $1;
            # message template
            push_stream_message_template                "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\"}";

            push_stream_websocket_allow_publish         on;

            # ping frequency
            push_stream_ping_message_interval           10s;
        }


启动nginx
测试查看状态
curl -s -v 'http://localhost/channels-stats'

测试写入
curl http://localhost/pub\?id\=ch1 -d "Some Text"  {"channel": "ch1", "published_messages": 1, "stored_messages": 0, "subscribers": 1}

h5的客户端代码





  {"name": "Bruce.Lin", "age": 25}
  

    ruby的客户端代码
    require 'net/http'
    
    uri = URI("http://localhost/pub\?id\=ch1")
    http = Net::HTTP.new(uri.host, uri.port)
    
    req = Net::HTTP::Post.new(uri.to_s)
    req.body = 'Some Text'
    
    http.request(req)
    

    你可能感兴趣的:(nginx)