nginx push_upstream模块的websocket

参考
[url]https://www.rails365.net/articles/websocket-wai-pian-nginx-push-stream-module-mo[/url]
安装
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的客户端代码

<html>
<head>
<script>
var ws = null;
function connect() {
if (ws !== null) return log('already connected');
ws = new WebSocket('ws://localhost/ws/ch1');
ws.onopen = function () {
log('connected');
};
ws.onerror = function (error) {
log(error);
};
ws.onmessage = function (e) {
log('recv: ' + e.data);
};
ws.onclose = function () {
log('disconnected');
ws = null;
};
return false;
}
function disconnect() {
if (ws === null) return log('already disconnected');
ws.close();
return false;
}
function send() {
if (ws === null) return log('please connect first');
var text = document.getElementById('text').value;
document.getElementById('text').value = "";
log('send: ' + text);
ws.send(text);
return false;
}
function log(text) {
var li = document.createElement('li');
li.appendChild(document.createTextNode(text));
document.getElementById('log').appendChild(li);
return false;
}
</script>
</head>
<body>
{"name": "Bruce.Lin", "age": 25}
<form onsubmit="return send();">
<button type="button" onclick="return connect();">
Connect
</button>
<button type="button" onclick="return disconnect();">
Disconnect
</button>
<input id="text" type="text">
<button type="submit">Send</button>
</form>
<ol id="log"></ol>
</body>
</html>


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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值