My sample civetweb websocket code...

2,049 views
Skip to first unread message

Morgan McGuire

unread,
Sep 21, 2013, 8:25:23 PM9/21/13
to civetweb
Including holding per-socket data and asynchronous communication, at:

http://sourceforge.net/p/g3d/code/HEAD/tree/G3D10/samples/remoteRender/source/App.cpp

The javascript code for the client side is in ../data-files

-m

Prof. Morgan McGuire
Computer Science Department
Williams College
http://cs.williams.edu/~morgan

No Face Press

unread,
Sep 23, 2013, 9:33:08 PM9/23/13
to cive...@googlegroups.com
Is this something that you want put into the repository or is it just a reference for discussion?

Thomas

Morgan McGuire

unread,
Sep 23, 2013, 10:01:37 PM9/23/13
to No Face Press, civetweb
I think that the dependencies are too large to check in usefully...it shows how to use civetweb websockets and Javascript together very cleanly, but to actually build that you'd have to care a lot more about 3D graphics than most civetweb developers probably do and download the whole G3D Innovation Engine library to link against.

-m


Prof. Morgan McGuire
Computer Science Department
Williams College
http://cs.williams.edu/~morgan


--
Sourceforge
https://sourceforge.net/projects/civetweb/
---
You received this message because you are subscribed to the Google Groups "civetweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to civetweb+u...@googlegroups.com.
To post to this group, send email to cive...@googlegroups.com.
Visit this group at http://groups.google.com/group/civetweb.
To view this discussion on the web visit https://groups.google.com/d/msgid/civetweb/74461457-0fd7-4a7e-83b7-1f2d1011be34%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

bel

unread,
Sep 25, 2013, 6:20:18 PM9/25/13
to cive...@googlegroups.com

My example test code for websocks:
websock.htm .. a single HTML file with a minimal script (%document_root%/websock/websock.htm)
WebSockCallback.c .. a minimal websock callback implementation
WebSockCallback.h .. the header, used in main.c

Modify main:
  // Start Mongoose
  memset(&callbacks, 0, sizeof(callbacks));
  callbacks.log_message = log_message;
  callbacks.websocket_ready = websocket_ready_handler;
  callbacks.websocket_data = websocket_data_handler;
  callbacks.connection_close = connection_close_handler;

  websock_init_lib();
  ctx = mg_start(&callbacks, NULL, (const char **) options);

websock.htm
WebSockCallbacks.c
WebSockCallbacks.h

bel

unread,
Sep 25, 2013, 6:28:08 PM9/25/13
to cive...@googlegroups.com

The code above is pure C (no C++), has no further dependencies, and is tested at Win32. It could work for Linux as well, but it is not tested there yet.
You can do whatever you want with the code (but don't blame me if it does not work).

While creating a thread is available as a "Convenience function" in the webserver header, mutex functions are not. I think it would be a good idea to make them available as well.

The test example shows a clock at the title bar of the web page. Furthermore all connected clients are informed if any client pushes a button.

bel

unread,
Sep 29, 2013, 1:11:09 PM9/29/13
to cive...@googlegroups.com
Civetweb provides support for Lua server pages - one can use it as a standalone server with Lua as a server side scripting language and sqlite as a data base.
Furthermore it provides support for websockets, but only in C, so websockets are only available for those, that use the webserver embedded or at least add some C/C++ code - one can not use websockets in the standalone server.

Wouldn't it be nice to allow users of the standalone webserver to use websockets via Lua scripts?
It is actually also easy to implement - interested?

jmil

unread,
Oct 8, 2019, 11:21:51 AM10/8/19
to civetweb
@bel, I am trying to have multiple browsers open at one time with the ability to change something in one browser and push a  notification to the all open browsers. I believe a websocket is the way to go. I have opened one on /ws and can see a change when it is made. However, I am just re-running a get which is a constant refresh until I close the socket. Instead, I would like to just send the notification. If I have to refresh the page, to see the data update than that is fine but the primary goal is to receive a notification on another browser. I am not sure if there is an example already where I can see how to go about this.

I have tried civetweb/example/_obsolete/websocket/WebSockCallbacks.c there is a websock_send_broadcast. I get a segmentation fault using this API.

I have tried using the mg_set_user_connection_data(conn,data); But when I use the mg_get_user_data(conn) I get a "invalid conversion from ‘void*’ to ‘t_ws_client*".

Any direction would be greatly appreciated.


Best Regards,
JMil

bel

unread,
Oct 11, 2019, 7:24:44 PM10/11/19
to civetweb
This thread is more than 6 years old, and most of the discussion moved into GitHub issues ... even if it is not really an "issue" with the code, but just used as a channel to ask for help.

You are right, websockets would be the way to go.
The examples in the _obsolete folder are really obsolete ... the new example is in embedded_c (https://github.com/civetweb/civetweb/blob/master/examples/embedded_c/embedded_c.c).
The "InformWebsockets" function will send some message to all connected websockets.
Reply all
Reply to author
Forward
0 new messages