websocket.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. char inbuf[4];
  11. const char *server_options[] = {
  12. /* document_root: The path to the test function websock.htm */
  13. "document_root", "../../examples/websocket",
  14. /* port: use http standard to match websocket url in websock.htm: ws://127.0.0.1/MyWebSock */
  15. /* if the port is changed here, it needs to be changed in websock.htm as well */
  16. "listening_ports", "80",
  17. NULL
  18. };
  19. callback_funcs.init_context = websock_init_lib;
  20. callback_funcs.exit_context = websock_exit_lib;
  21. callback_funcs.websocket_ready = websocket_ready_handler;
  22. callback_funcs.websocket_data = websocket_data_handler;
  23. callback_funcs.connection_close = connection_close_handler;
  24. ctx = mg_start(&callback_funcs, NULL, server_options);
  25. printf("Connect to localhost:%s/websock.htm\n", mg_get_option(ctx, "listening_ports"));
  26. puts("Enter an (ASCII) character or * to exit:");
  27. for (;;) {
  28. fgets(inbuf, sizeof(inbuf), stdin);
  29. if (inbuf[0]=='*') {
  30. break;
  31. }
  32. inbuf[0] = toupper(inbuf[0]);
  33. websock_send_broadcast(ctx, inbuf, 1);
  34. }
  35. mg_stop(ctx);
  36. return 0;
  37. }