[Libwebsockets] 回复:Re: websocket client on ESP8266

Andy Green andy at warmcat.com
Mon Nov 21 02:58:17 CET 2016

On November 21, 2016 9:33:04 AM GMT+08:00, lhl_nciae at sina.cn wrote:
>Hi, Mr Green	Thank for your reply.Now , I have some questions for how
>to make a websocket client on plat-esp8266, Could you help me to answer
>about it, thanks!         1)Use lws_client_connect_via_info to
>inititate the "websocket client handshake", is it correctly?

Yes... it initiates the whole network connection and host dns lookup before that.

>2)In the "lws_client_conncect_2"function, I use "espconn_connect"

Basically put all esp8266 code in a function in plat-esp8266.c, and call the function with #if defined (LWS_WITH_ESP8266) around it from the existing code.

>initiate the TCP Connection Requeset, meanwhile, I  regist the
>"esp8266_cb_connect" by espconn_regit_connectcb() function. In the

I did not implement any 8266 client connection, so I don't know the esp sdk flow for it with their weirdo apis.  There will be examples in the sdk and on the web.

You'll need to do the dns lookup, the sdk apis there also bear little resemblance to posix.

>"esp8266_cb_cx", I handle the response msg from websocket server. Is it

You should look in plat-esp8266.c and follow how it wired up rx and tx there.  Because in lws the same functions in output.c do network rx and tx for both client and server.  Since that already works on server, you should only need to do the same for your socket traffic and that part will just work I think.

>corrrectly?         3)Please explain about the role of lws_vhost,
>lws_connection, lws_connection_info, as well as the link between each

Read the docs and the test client sources, because these are generic questions.  For client, vhost is a stub, the one created by default when you create the context is enough.  Connection info is just a struct to cleanly pass many possible parameters to the function that creates the connection.

>other.         4) Please expain about the mechanism and role of libev 
>、libeu,and how to use them!

On esp8266 they are not supported.  a) neither lib supports esp8266, b) the sdk + lwip does not support poll in a way they understand, since there is no multitasking if not using rtos, c) there's no ****ing room for them on esp8266.

Instead the necessary adaptations can be found in lib/plat-esp8266.c, that create the equivalent of the event model libuv/ev would provide.  But much more compactly than either of those libraries.  This also should be shared between server and client and more or less 'just work'.

Just a reminder lws is LGPL, it'd be nice to get patches not least since I might be able to help once it's started.


>	  Mr Green, Would you give me some advice on how to implement 
>websocket client on ESP8266 plat?
>   	  Look forward to your reply!Thanks!----- 原始邮件 -----
>发件人:Andy Green <andy at warmcat.com>
>收件人:lhl_nciae <lhl_nciae at sina.cn>,Libwebsockets at ml.libwebsockets.org
>主题:Re: [Libwebsockets] websocket client on ESP8266
>日期:2016年11月20日 22点48分
>On November 20, 2016 9:33:22 PM GMT+08:00, lhl_nciae
><lhl_nciae at sina.cn> wrote:
>>As all konwn,libwebsocket had ported to esp8266 as an websocket
>>server.But,how to make a websocket client on esp8266?Who had do
>>it?Please tell me! Thanks!
>I only have a use for server (which is quite fully supported, eg, every
>lws test server demo function works out of the box; v2.x plugins are
>supported even by compile-time #including them).  So I did not do any
>work porting the client side, it's disabled at cmake currently.
>There are many common critical areas between client and server that
>will reuse the server port in plat-esp8266, like the pollfd emulation
>and interfacing to lwip.  But there will be enough unported areas (like
>esp8266 DNS lookup not using POSIX apis) someone will need to go
>through the client code fixing it up before it will work.
>>Libwebsockets mailing list
>>Libwebsockets at ml.libwebsockets.org

More information about the Libwebsockets mailing list