websocket.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2004-2012 Sergey Lyubka
  2. // This file is a part of mongoose project, http://github.com/valenok/mongoose
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "mongoose.h"
  6. static void websocket_ready_handler(struct mg_connection *conn) {
  7. unsigned char buf[40];
  8. buf[0] = 0x81;
  9. buf[1] = snprintf((char *) buf + 2, sizeof(buf) - 2, "%s", "server ready");
  10. mg_write(conn, buf, 2 + buf[1]);
  11. }
  12. // Arguments:
  13. // flags: first byte of websocket frame, see websocket RFC,
  14. // http://tools.ietf.org/html/rfc6455, section 5.2
  15. // data, data_len: payload data. Mask, if any, is already applied.
  16. static int websocket_data_handler(struct mg_connection *conn, int flags,
  17. char *data, size_t data_len) {
  18. unsigned char reply[200];
  19. size_t i;
  20. (void) flags;
  21. printf("rcv: [%.*s]\n", (int) data_len, data);
  22. // Truncate echoed message, to simplify output code.
  23. if (data_len > 125) {
  24. data_len = 125;
  25. }
  26. // Prepare frame
  27. reply[0] = 0x81; // text, FIN set
  28. reply[1] = data_len;
  29. // Copy message from request to reply, applying the mask if required.
  30. for (i = 0; i < data_len; i++) {
  31. reply[i + 2] = data[i];
  32. }
  33. // Echo the message back to the client
  34. mg_write(conn, reply, 2 + data_len);
  35. // Returning zero means stoping websocket conversation.
  36. // Close the conversation if client has sent us "exit" string.
  37. return memcmp(reply + 2, "exit", 4);
  38. }
  39. int main(void) {
  40. struct mg_context *ctx;
  41. struct mg_callbacks callbacks;
  42. const char *options[] = {
  43. "listening_ports", "8080",
  44. "document_root", "websocket_html_root",
  45. NULL
  46. };
  47. memset(&callbacks, 0, sizeof(callbacks));
  48. callbacks.websocket_ready = websocket_ready_handler;
  49. callbacks.websocket_data = websocket_data_handler;
  50. ctx = mg_start(&callbacks, NULL, options);
  51. getchar(); // Wait until user hits "enter"
  52. mg_stop(ctx);
  53. return 0;
  54. }