websocat icon indicating copy to clipboard operation
websocat copied to clipboard

HTTP handling on server mode

Open tgotwig opened this issue 7 years ago • 1 comments

Hello there 🤗​

is it possible to handle HTTP-Request on server mode? HTTP and WebSockets can run on the same port: https://stackoverflow.com/questions/29497725/can-websockets-and-http-server-both-run-on-the-same-port-number I have a Application which works in that way, I receive following Error each time I send a HTTP request:

websocat: WebSocketError: I/O failure

tgotwig avatar Sep 24 '18 12:09 tgotwig

Yes. There is even special option in websocat to serve specified static files in addition to the websocket connection:

websocat -Et ws-l:127.0.0.1:8080 mirror: -F /:text/html:index.html

It means "if it is not a WebSocket connection and URI is /, serve index.html as content-type text/html instead of handling request as usual".


For trickier things you should you should use a web server on your port and redirect WebSocket traffic to websocat, which would be running on a separate port.

Here is example of config file for nginx (some non-relevant parts removed):

http {
    server {
        listen 0.0.0.0:3380;
        index index.html;
        root  /path/to/your/site;
        
        
        location /websocket {
            proxy_read_timeout 1d;
            proxy_send_timeout 1d;
            proxy_pass http://localhost:3381;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }

}

Nginx will serve content as usual and redirect requests to /websocket location to websocat, which should be started like this:

websocat -E --text ws-l:127.0.0.1:3381 exec:./my_script.sh

vi avatar Sep 24 '18 12:09 vi