websocket.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include "civetweb.h"
  5. #include "WebSockCallbacks.h"
  6. int main(void)
  7. {
  8. struct mg_context *ctx = 0;
  9. struct mg_callbacks callback_funcs = {0};
  10. tWebSockContext ws_ctx;
  11. char inbuf[4];
  12. const char *server_options[] = {
  13. /* document_root: The path to the test function websock.htm */
  14. "document_root", "../../examples/websocket",
  15. /* port: use http standard to match websocket url in websock.htm: ws://127.0.0.1/MyWebSock */
  16. /* if the port is changed here, it needs to be changed in websock.htm as well */
  17. "listening_ports", "80",
  18. NULL
  19. };
  20. callback_funcs.init_context = websock_init_lib;
  21. callback_funcs.exit_context = websock_exit_lib;
  22. callback_funcs.websocket_ready = websocket_ready_handler;
  23. callback_funcs.websocket_data = websocket_data_handler;
  24. callback_funcs.connection_close = connection_close_handler;
  25. ctx = mg_start(&callback_funcs, &ws_ctx, server_options);
  26. printf("Connect to localhost:%s/websock.htm\n", mg_get_option(ctx, "listening_ports"));
  27. puts("Enter an (ASCII) character or * to exit:");
  28. for (;;) {
  29. fgets(inbuf, sizeof(inbuf), stdin);
  30. if (inbuf[0]=='*') {
  31. break;
  32. }
  33. inbuf[0] = toupper(inbuf[0]);
  34. websock_send_broadcast(ctx, inbuf, 1);
  35. }
  36. mg_stop(ctx);
  37. return 0;
  38. }