lua_package_path "/home/username/lib/lua/lib/?.lua;;"; server { # , , nginx location ~ ^/ws/?(.*)$ { default_type 'plain/text'; # - , content_by_lua_file /home/username/www/wsexample.local/ws.lua; } # , php # POST , json payload location ~ ^/lua_fastcgi_connection(/?.*)$ { internal; # nginx fastcgi_pass_request_body on; fastcgi_pass_request_headers off; # never never use it for lua handler #include snippets/fastcgi-php.conf; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD "POST"; # $request_method; fastcgi_param CONTENT_TYPE "application/x-www-form-urlencoded"; # $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param DOCUMENT_URI "$1"; # $document_uri fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REQUEST_SCHEME $scheme; fastcgi_param HTTPS $https if_not_empty; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; fastcgi_param SCRIPT_FILENAME "$document_root/mywebsockethandler.php"; fastcgi_param SCRIPT_NAME "/mywebsockethandler.php"; fastcgi_param REQUEST_URI "$1"; # . lua - php . fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; fastcgi_keep_conn on; }
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 return elseif not data then ngx.log(ngx.DEBUG, "Sending Websocket ping") wb:send_ping() elseif typ == "close" then -- send a close frame back: local bytes, err = wb:send_close(1000, "enough, enough!") if not bytes then ngx.log(ngx.ERR, "failed to send the close frame: ", err) return end local code = err ngx.log(ngx.INFO, "closing with status code ", code, " and message ", data) break; elseif typ == "ping" then -- send a pong frame back: local bytes, err = wb:send_pong(data) if not bytes then ngx.log(ngx.ERR, "failed to send frame: ", err) return end elseif typ == "pong" then -- just discard the incoming pong frame elseif data then -- uri, json payload body local res = ngx.location.capture("/lua_fastcgi_connection"..ngx.var.request_uri,{method=ngx.HTTP_POST,body=data}) if wb == nil then ngx.log(ngx.ERR, "WebSocket instaince is NIL"); return ngx.exit(444) end wb:send_text(res.body) else ngx.log(ngx.INFO, "received a frame of type ", typ, " and payload ", data) end end
<!DOCTYPE> <html> <head> <meta charset="utf-8" /> <script type="text/javascript"> "use strict"; let socket; function tryWebSocket() { socket = new WebSocket("ws://try6.local/ws/"); socket.onopen = function() { console.log(" ."); }; socket.onclose = function(event) { if (event.wasClean) { console.log(' '); } else { console.log(' '); // , "" } console.log(': ' + event.code + ' : ' + event.reason); }; socket.onmessage = function(event) { console.log(" " + event.data); }; socket.onerror = function(error) { console.log(" " + error.message); }; } function tryWSSend(event) { let msg = document.getElementById('msg'); socket.send(msg.value); event.stopPropagation(); event.preventDefault(); return false; } function closeWebSocket(event) { socket.close(); } </script> </head> <body onLoad="tryWebSocket(event);return false;"> <form onsubmit="tryWSSend(event); return false;"> <button onclick="tryWebSocket(event); return false;">Try WebSocket</button> <fieldset> Message: <input value="Test message 4444" type="text" size="10" id="msg"/><input type="submit"/> </fieldset> <fieldset> <button onclick="closeWebSocket(event); return false;">Close Websocket</button><br/> </fieldset> </form> </body> </html>
<?php header("Content-Type: application/json; charset=utf-8"); echo json_encode(["status"=>"ok","response"=>"php websocket json @ ".time(), "payload"=>[$_REQUEST,$_SERVER]]); exit;
Source: https://habr.com/ru/post/338614/
All Articles