I need to use NGINX as a WebSocket Proxy, and I've been able to run this tutorial without any problem. In this case, a simple WebSocket application is done using ws and Node.js. However, I need to substitute the code in javascript (server.js) with an application developed in C.
To do so, I've tried with the last example shown in this tutorial , which is very simple and easy to understand.
However, using the same port and configuration as in server.js, when running the compiled C code, it seems that the communication is not properly established. In this case, when I run wscat in the client's terminal, it seems it is waiting for something since the word 'connected' is never seen, but the communication is not finished either (this is why I say it seems it is waiting for something; in other cases, if I try with a different port, for instance, the communication is closed and it appears a sentence as 'error: unexpected server response').
On the other hand, on the server side, I can see there is a new connection. I suspect that the answer from the server is not arriving at the client, but I don't know why. Nginx is properly configured since the example done in javascript works properly, and I'm using the same ports in both javascript and C (in server.js I have: 'wss = new WebSocketServer({port: 8010});' and in server.c: 'server.sin_port = htons( 8010 );' from my point of view it should work...)
I've tried with other examples in C with a similar behavior. Does anyone know which may be the cause for this? Do you recommend other particular C codes for WebSocket servers?
Your problem is that there's a very marked difference between websockets and sockets. Sockets are more low level network interfaces whereas websockets run on a web server of some kind.
I would recommend using a websocket library for C. :) Here's one I found after doing a quick google search. The documentation for said library can be found here as well.
The answer given by @MustacheMoses is very much to the point (I upvoted) - there's a huge difference between sockets and Websockets.
Websockets is a protocol that starts with an HTTP "upgrade" request and continues with a multiplexed message exchange protocol using a specific data packaging scheme.
Sockets refer to the lower level communication channels (ie, Stream Sockets, such as TCP/IP sockets or Unix Sockets, or even Raw Sockets).
I would also point out that, much like node.js, websocket libraries / servers in C come in different flavors.
Some libraries provide an almost Web Framework approach (for example, kore.io or my own facil.io micro-framework ) and others are more narrow in scope .
Much like node.js packages / servers (which are often written in C or C++), they all differ in performance and features (I would test this before I commit to a specific approach).
As @tadman rightfully pointed out, it might be better to stick with your existing code base and add any C libraries you need using FFI .
It's somewhat rare for high level services, such as web applications, to be authored using a lower level language.
The inverse is also true, it's often less desirable to author the lower level web server using a higher level language.
This is just my humble opinion, but consider that I'm writing this even though I'm the author of a websocket application framework written in C. When the time comes to write an actual production Websocket app, I often prefer to use facil.io as a Ruby C extension and write the app in Ruby (unless Ruby isn't available).
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.