I am trying to make a simple webapp which uses websocket for communication with the lighttpd server. Unfortunately my setup does not work and i do not know why. Search in the internet does not offer clear example how to use mod_wstunnel( May be someone has experience with lighttpd.
Below you can find my sandbox:
lighttpd.conf
server.modules += ("mod_wstunnel", "mod_setenv")
server.indexfiles = ("index.html")
server.document-root = "/var/www/html/websocket"
$HTTP["url"] =~ "^/websockify" {
wstunnel.server = ( "" => ( ( "host" => "127.0.0.1", "port" => "5900" ) ) )
wstunnel.frame-type = "binary"
server.stream-request-body = 2
server.stream-response-body = 2
}
index.html
<h1>Real Time Messaging</h1>
<pre id="messages" style="height: 400px; overflow: scroll"></pre>
<input type="text" id="messageBox" placeholder="Type your message here" style="display: block; width: 100%; margin-bottom: 10px; padding: 10px;" />
<button id="send" title="Send Message!" style="width: 100%; height: 30px;">Send Message</button>
<script>
(function() {
const sendBtn = document.querySelector('#send');
const messages = document.querySelector('#messages');
const messageBox = document.querySelector('#messageBox');
let ws;
function showMessage(message) {
messages.textContent += `
${message}`;
messages.scrollTop = messages.scrollHeight;
messageBox.value = '';
}
function init() {
if (ws) {
ws.onerror = ws.onopen = ws.onclose = null;
ws.close();
}
ws = new WebSocket("ws://127.0.0.1:5900/websockify")
ws.onopen = () => {
console.log('Connection opened!');
}
ws.onmessage = ({ data }) => showMessage(data);
ws.onclose = function() {
ws = null;
}
}
sendBtn.onclick = function() {
if (!ws) {
showMessage("No WebSocket connection :(");
return ;
}
ws.send(messageBox.value);
showMessage(messageBox.value);
}
init();
})();
</script>
Thanks in advance for the help.
question from:
https://stackoverflow.com/questions/65862891/lighttpd-with-websocket 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…