daurnimator / lua-http

HTTP Library for Lua. Supports HTTP(S) 1.0, 1.1 and 2.0; client and server.
https://daurnimator.github.io/lua-http/
MIT License
795 stars 81 forks source link

example for http.websocket server #152

Closed stephenkgu closed 5 years ago

stephenkgu commented 5 years ago

cannot find an example for websocket server, anyone gives one like server_hello?

thanks

RussellHaley commented 5 years ago

Here was my first attempt. It's a websocket server and a client. I haven't run this in a long time so I don't remember if it works or not: https://github.com/RussellHaley/lua-http-endpoints

Here is a cqueues application that runs minecraft in a pty and then serves out the console output via a (lua-http) websocket: https://github.com/RussellHaley/minecraft-runner

Here is the same codebase as the minecraft-runner project only with the minecraft stuff stripped out: https://github.com/RussellHaley/we-client

Caveat emptor...

daurnimator commented 5 years ago

an example for websocket server, anyone gives one like server_hello?

In your on_stream callback (after doing any logging/routing/etc you feel like) you would write:

local ws = assert(websocket.new_from_stream(stream, headers))
assert(ws:accept()) -- can be passed options for advanced use cases.

and then just use ws:send() and ws:receive()