的nginx的websocket的helloworld
先装openresty
配置文件为
worker_processes 1; events { worker_connections 1024; } http { charset utf-8,gbk; include mime.types; default_type text/plain; lua_package_path "/data/www/lua/?.lua;;"; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; lua_code_cache off; location /lua_webc { content_by_lua_file /data/www/lua/webs_client.lua; } location /webs { content_by_lua_file /data/www/lua/webs.lua; } location / { root html; index index.html index.htm; } # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
关键点是
location /webs { content_by_lua_file /data/www/lua/webs.lua; }
websocket的lua代码为
[root@VM_192_107_centos lua]# cat webs.lua 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 local data, typ, err = wb:recv_frame() if not data then ngx.log(ngx.ERR, "failed to receive a frame: ", err) return ngx.exit(444) end if 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) return end if 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 else ngx.log(ngx.INFO, "received a frame of type ", typ, " and payload ", data) end wb:set_timeout(1000) -- change the network timeout to 1 second bytes, err = wb:send_text("Hello world") if not bytes then ngx.log(ngx.ERR, "failed to send a text frame: ", err) return ngx.exit(444) end bytes, err = wb:send_binary("blah blah blah...") if not bytes then ngx.log(ngx.ERR, "failed to send a binary frame: ", err) return ngx.exit(444) end 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 [root@VM_192_107_centos lua]#
客户端html代码
<html> <head> <script> var ws = null; function connect() { if (ws !== null) return log('already connected'); ws = new WebSocket('ws://haoning.net/webs'); 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>