简体   繁体   中英

Connecting libwebsockets client to rails server with Action Cable

I have a web server running on ruby on rails 5 with action cable and want to use a libwebsockets client to connect to the server. I am unable to get the client to connect successfully to the server with action cable. The libwebsockets client never receives the callback for a successful connection and always times out. My rails server is running on nginx and thin on a Ubuntu machine, while the client is a C++ console application running in Visual Studio 2013. I have the actioncable/thin compability settings enabled as described in https://github.com/macournoyer/thin/issues/298 . I am also using redis for action cable.

I have tried logging and debugging everything I can think of. Can someone please point me in the right direction for getting the libwebsockets client to connect?


More details:

I originally had my rails server set up to use websocket-rails, and this worked with the sample client provided in the official libwebsockets github repository: https://github.com/warmcat/libwebsockets/tree/master/test-server . However, this same client times out when trying to connect to the server running with action cable. I verified I have action cable set up properly on my rails server by implementing the javascript client-side action cable code, and I can successfully connect to the action cable server and subscribe to a channel. Based on the answer https://stackoverflow.com/a/36120630/3298688 , I need the libwebsockets client to send a subscribe message right after connecting, but the client never connects so I am unable to send this message.

My rails log shows that the server successfully received the request from the client. This log entry block was triggered twice within the same second so there is an unknown duplicate request; this did not happen when connecting to websocket-rails. I can see the server accepts the connection from the client since my custom logger message appeared. This repeats each time the client times out and attempts to reconnect.

Started GET "/websocket" for 127.0.0.1 at 2017-03-29 11:33:51 -0700
Started GET "/websocket/" [WebSocket] for 127.0.0.1 at 3/29/2017 11:33 AM
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
****** My custom logger message: ApplicationCable::Connection#connect was called!
Registered connection (Z2lkOi8vcHJvcGVydHktbWF0cml4L0xvZ2luLzk2)

I logged all the callback reasons that get triggered in the libwebsockets client and I never see the callback for LWS_CALLBACK_CLIENT_ESTABLISHED . All of the callbacks are related to polling (lws_callback_reasons enum 29+). I attempted to send a message from within those callbacks, but no data actually gets sent, probably because the client has not triggered the connected callback yet.

When I close the client application, the following message appears twice in the rails log, so it looks like the rails server does not have any issues.

Finished "/websocket/" [WebSocket] for 127.0.0.1 at 3/29/2017 11:57 AM

This isn't a direct answer to the original question, but I just gave up on libwebsockets and instead used Chromium Embedded Framework to nest an invisible browser inside my application, which allowed me to just load a web page from my Rails app and transfer data that way. It was still a pain to set up but I didn't run into any weird connection issues since I was able to use the built-in javascript for action cable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM