websocket.c 1.2 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 wenn */
  16. "listening_ports", "80",
  17. NULL
  18. };
  19. websock_init_lib();
  20. callback_funcs.websocket_ready = websocket_ready_handler;
  21. callback_funcs.websocket_data = websocket_data_handler;
  22. callback_funcs.connection_close = connection_close_handler;
  23. ctx = mg_start(&callback_funcs, NULL, server_options);
  24. puts("Enter an (ASCII) character or * to exit:");
  25. for (;;) {
  26. fgets(inbuf, sizeof(inbuf), stdin);
  27. if (inbuf[0]=='*') {
  28. break;
  29. }
  30. inbuf[0] = toupper(inbuf[0]);
  31. websock_send_broadcast(inbuf, 1);
  32. }
  33. mg_stop(ctx);
  34. websock_exit_lib();
  35. return 0;
  36. }