Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: websocket support. #213

Open
xopxe opened this issue Dec 4, 2018 · 6 comments
Open

Feature request: websocket support. #213

xopxe opened this issue Dec 4, 2018 · 6 comments

Comments

@xopxe
Copy link
Contributor

xopxe commented Dec 4, 2018

It would be great if we could use websockets. That would allow to have complex client side UI, portably, and some light API on the microcontroller side. Do you have any plans?
As alternatives I suppose we could deploy a websocket server in parallel on a separate port (like this one https://github.com/Molorius/esp32-websocket). But the ideal solution is to use an http server with websocket support, or integrate support into httpsrv.c

@the0ne
Copy link
Collaborator

the0ne commented Apr 19, 2019

The integration of https://github.com/Molorius/esp32-websocket into the httpsrv isn't easy as the esp32-websocket module works on netconn only.

But I made progress by adding an interface in sockets.h to get the socket's netconn and providing that to ws_server_add_client. netconn_write doesn't seem to work on that but using netconn_write_partial it seems to work.

I'm not yet sure whether to implement as:

  1. one lua callback for all websocket or
  2. separate lua files for websocket request depending on the request url like for "normal" http(s) requests

Probably 1. will be easier to implement and provide faster runtime...

@xopxe
Copy link
Contributor Author

xopxe commented Apr 19, 2019

Yes, probably having a single Lua callback will be good enough. You can pass the URL to the handler so it can discriminate between services if needed from inside the Lua code. The handler will also need the socket so it can stream data into the connection with a thread.

@the0ne
Copy link
Collaborator

the0ne commented Apr 20, 2019

the callback will have the following globals set:

http_method
http_uri
http_request
http_cookies
http_language
http_agent
http_port
http_secure
http_remote_addr

the following values will be given as parameters:
length of the message
the message
the message type (which can be text or bin)

sample callback:

	function websocket_callback(len, msg, type)
		-- os.syslog("lua got msg: "..msg)
		local print = net.service.http.print_websocket
		print("you sent: "..msg) --this is sent directly to the client
	end
	net.service.http.start(80, 0, nil, nil, nil, websocket_callback)

also, it will be possible to send a message to all connected websocket clients from any lua script on the system by using
net.service.http.print_websockets("your_message_here")

@xopxe
Copy link
Contributor Author

xopxe commented Apr 20, 2019

Nice!

@the0ne
Copy link
Collaborator

the0ne commented Sep 24, 2019

Good news here.
I recently managed to rip-out-again some functionality (serving static files in separate threads) that I had added for speed testing and optimization. Still, some more cleanup will be done before creating a PR.

@bazooka07
Copy link

Hello,
Are websockets working now with the http server ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants