openresty websocket
程序员文章站
2022-04-10 20:08:49
...
mac安装openresty
openssl还是有问题 shared等
参考
https://blog.csdn.net/csdncqmyg/article/details/73835354
bundle/nginx-x-x-x(版本号)/auto/lib/openssl/conf
把.openssl删掉 ,只删这个单词
几年前调过的
http://haoningabc.iteye.com/blog/2168717
参考教程如下
openresty
http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
https://www.openresty.net.cn/
https://github.com/openresty/lua-nginx-module
http://wiki.jikexueyuan.com/project/openresty/openresty/get_url_param.html
https://www.cnblogs.com/scotoma/p/3330190.html
nginx.conf添加
客户端
调用url的例子
简单点的
websocket 的
nginx.conf配置执行lua
brew install openssl ./configure --prefix=/usr/local/openresty --with-openssl=/usr/local/Cellar/openssl/1.0.2o_1 make make install
openssl还是有问题 shared等
参考
https://blog.csdn.net/csdncqmyg/article/details/73835354
bundle/nginx-x-x-x(版本号)/auto/lib/openssl/conf
把.openssl删掉 ,只删这个单词
几年前调过的
http://haoningabc.iteye.com/blog/2168717
参考教程如下
openresty
http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
https://www.openresty.net.cn/
https://github.com/openresty/lua-nginx-module
http://wiki.jikexueyuan.com/project/openresty/openresty/get_url_param.html
https://www.cnblogs.com/scotoma/p/3330190.html
nginx.conf添加
location /1.0/websocket { lua_socket_log_errors off; lua_check_client_abort on; content_by_lua_block { local server = require "resty.websocket.server" local wb, err = server:new{ timeout = 5000, -- in milliseconds max_payload_len = 65535, } if not wb then ngx.log(ngx.ERR, "failed to new websocket: ", err) return ngx.exit(444) end while true do local data, typ, err = wb:recv_frame() if wb.fatal then ngx.log(ngx.ERR, "failed to receive frame: ", err) return ngx.exit(444) end if not data then local bytes, err = wb:send_ping() if not bytes then ngx.log(ngx.ERR, "failed to send ping: ", err) return ngx.exit(444) end elseif typ == "close" then break elseif typ == "ping" then local bytes, err = wb:send_pong() if not bytes then ngx.log(ngx.ERR, "failed to send pong: ", err) return ngx.exit(444) end elseif typ == "pong" then ngx.log(ngx.INFO, "client ponged") elseif typ == "text" then local bytes, err = wb:send_text("from nginx:"..data) if not bytes then ngx.log(ngx.ERR, "failed to send text: ", err) return ngx.exit(444) end end end wb:send_close() } }
客户端
<html> <head> <script> var ws = null; function connect() { if (ws !== null) return log('already connected'); ws = new WebSocket('ws://localhost/1.0/websocket'); 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> <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>
调用url的例子
function max(mystr) local sock = ngx.socket.tcp() --ngx.say("hello I am in /data/www/lua/ ") ngx.say(mystr) -- local ai_url = "http://10.22.57.168:8080/interaction" local ok, err = sock:connect("www.baidu.com", 80) --local ok, err = sock:connect(ai_url, 80) if not ok then ngx.say("failed to connect to baidu: ", err) return end local req_data = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\n\r\n" -- local req_data = '{"misid":"12345","content":{"type":"manual","text":"附近有什么外卖"},"tm":1524130703583}' local bytes, err = sock:send(req_data) if err then ngx.say("failed to send to baidu: ", err) return end local data, err, partial = sock:receive() if err then ngx.say("failed to recieve to baidu: ", err) return end sock:close() ngx.say("successfully talk to baidu! response first line: ", data) end max("hello world ya")
简单点的
local res = ngx.location.capture( 'http://10.22.57.168:8080/interaction', { method = ngx.HTTP_POST, -- args = ngx.encode_args({a = 1, b = '2&'}), --body = ngx.encode_args({c = 3, d = '4&'}) body = '{"misid":"12345","content":{"type":"manual","text":"附近有什么外卖"},"tm":1524130703583}' } ) ngx.say(res.body)
websocket 的
local server = require "resty.websocket.server" local wb, err = server:new{ timeout = 5000, -- in milliseconds max_payload_len = 65535, } if not wb then ngx.log(ngx.ERR, "failed to new websocket: ", err) return ngx.exit(444) end while true do local data, typ, err = wb:recv_frame() if wb.fatal then ngx.log(ngx.ERR, "failed to receive frame: ", err) return ngx.exit(444) end if not data then local bytes, err = wb:send_ping() if not bytes then ngx.log(ngx.ERR, "failed to send ping: ", err) return ngx.exit(444) end elseif typ == "close" then break elseif typ == "ping" then local bytes, err = wb:send_pong() if not bytes then ngx.log(ngx.ERR, "failed to send pong: ", err) return ngx.exit(444) end elseif typ == "pong" then ngx.log(ngx.INFO, "client ponged") elseif typ == "text" then local cjson = require "cjson" --local json_str = '{"name": "hao.ning", "age": 25}' --local json = cjson.decode(json_str) local json = cjson.decode(data) if not json then ngx.log(ngx.ERR, "failed json: ", err) return ngx.exit(444) end -- local bytes, err = wb:send_text("from nginx /data/www/lua: "..data) ngx.ctx.wb_list={} ngx.ctx.wb_list["hao"]="abc" ngx.ctx.wb_list[json['name']]="aaaa" --local bytes, err = wb:send_text("from nginx save wb:"..json['name']) --local bytes, err = wb:send_text("from nginx save wb:"..table.getn(ngx.ctx.wb_list)) local bytes, err = wb:send_text("from nginx save wb:"..ngx.ctx.wb_list["Bruce.Lin"]) if not bytes then ngx.log(ngx.ERR, "failed to send text: ", err) return ngx.exit(444) end end end wb:send_close()
nginx.conf配置执行lua
location /hellolua { content_by_lua_file /data/www/lua/luatest.lua; }
上一篇: Oracle 9i的数据类型
下一篇: Java 中 解压缩文件
推荐阅读
-
swoole的webSocket类没有connections属性吗?出错了
-
java WebSocket客户端断线重连的实现方法
-
客户端websocket 无法连接上PHP socket问题
-
使用Go基于WebSocket构建千万级视频直播弹幕系统的代码详解
-
JS如何实现基于websocket的多端桥接平台
-
swoole+websocket如何搭建一个点对点的聊天功能
-
openresty+websocket+redis simple chat
-
我做的一个websocket的demophp server
-
Spring4.0系列9-websocket简单应用
-
javascript - 怎么配置WebSocket服务器,用的SegmentFault