rws_example.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // The MIT License (MIT)
  2. // Copyright (c) 2018 liu2guang <liuguang@rt-thread.com>
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  14. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  17. // OR OTHER DEALINGS IN THE SOFTWARE.
  18. #include <rtthread.h>
  19. #include <librws.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #if (RTTHREAD_VERSION < 30100)
  23. #define DBG_SECTION_NAME "[LIBRWS.Test] "
  24. #else
  25. #define DBG_SECTION_NAME "LIBRWS.Test"
  26. #endif
  27. #define DBG_ENABLE
  28. #define DBG_LEVEL DBG_LOG
  29. #define DBG_COLOR
  30. #include <rtdbg.h>
  31. struct librws_app
  32. {
  33. rt_bool_t init;
  34. rws_socket *socket;
  35. };
  36. typedef struct librws_app *librws_app_t;
  37. static librws_app_t app;
  38. static void onopen(rws_socket socket)
  39. {
  40. LOG_D("websocket connected. ");
  41. }
  42. static void onclose(rws_socket socket)
  43. {
  44. rws_error error = rws_socket_get_error(socket);
  45. if (error)
  46. {
  47. LOG_E("websocket disconnect, error: %i, %s ", rws_error_get_code(error), rws_error_get_description(error));
  48. }
  49. else
  50. {
  51. LOG_D("websocket disconnect! ");
  52. }
  53. rws_socket_disconnect_and_release(app->socket);
  54. app->init = RT_FALSE;
  55. }
  56. static void onmessage_text(rws_socket socket, const char *text, const unsigned int len)
  57. {
  58. char *buff = RT_NULL;
  59. buff = (char *)rt_malloc(2048);
  60. rt_memset(buff, 0x00, 2048);
  61. rt_memcpy(buff, text, len);
  62. LOG_D("message(txt), %d(byte): %s ", len, buff);
  63. if (buff != RT_NULL)
  64. {
  65. rt_free(buff);
  66. }
  67. }
  68. static void onmessage_bin(rws_socket socket, const void *data, const unsigned int len)
  69. {
  70. char *buff = RT_NULL;
  71. buff = (char *)rt_malloc(2048);
  72. rt_memset(buff, 0x00, 2048);
  73. rt_memcpy(buff, data, len);
  74. LOG_D("message(bin), %d(byte): %s ", len, buff);
  75. if (buff != RT_NULL)
  76. {
  77. rt_free(buff);
  78. }
  79. }
  80. // rws_connect ws echo.websocket.org 80
  81. // rws_connect wss echo.websocket.org 443
  82. // rws_send "hello tls!"
  83. static int _rws_connect(int argc, char *argv[])
  84. {
  85. int port = -1;
  86. rws_bool ret = rws_false;
  87. if (argc < 3)
  88. {
  89. LOG_E("the msh cmd format: rws_conn ws/wss host [port]. ");
  90. return RT_EOK;
  91. }
  92. if (app->init == RT_TRUE)
  93. {
  94. LOG_E("the websocket connection has been opened. ");
  95. return (-RT_EBUSY);
  96. }
  97. app = (librws_app_t)rt_malloc_align(sizeof(struct librws_app), 4);
  98. if (app == RT_NULL)
  99. {
  100. LOG_E("librws_conn cmd memory malloc failed. ");
  101. return (-RT_ENOMEM);
  102. }
  103. rt_memset(app, 0x00, sizeof(struct librws_app));
  104. app->socket = rws_socket_create();
  105. if (app->socket == RT_NULL)
  106. {
  107. LOG_E("librws socket create failed. ");
  108. return (-RT_ERROR);
  109. }
  110. if (strcmp(argv[1], "ws") == 0)
  111. {
  112. port = ((argv[3] == RT_NULL) ? (80) : (atoi(argv[3])));
  113. rws_socket_set_url(app->socket, "ws", argv[2], port, "/");
  114. }
  115. else if (strcmp(argv[1], "wss") == 0)
  116. {
  117. port = ((argv[3] == RT_NULL) ? (443) : (atoi(argv[3])));
  118. rws_socket_set_url(app->socket, "wss", argv[2], port, "/");
  119. }
  120. else
  121. {
  122. LOG_E("protocol types are not supported, only support ws/wss. ");
  123. return (-RT_EINVAL);
  124. }
  125. rws_socket_set_on_connected(app->socket, &onopen);
  126. rws_socket_set_on_disconnected(app->socket, &onclose);
  127. rws_socket_set_on_received_text(app->socket, &onmessage_text);
  128. rws_socket_set_on_received_bin(app->socket, &onmessage_bin);
  129. /* set custom mode */
  130. rws_socket_set_custom_mode(app->socket);
  131. ret = rws_socket_connect(app->socket);
  132. if (ret == rws_false)
  133. {
  134. if (strcmp(argv[1], "ws") == 0)
  135. {
  136. LOG_E("connect %s://%s:%d/ failed. ", argv[1], argv[2], port);
  137. }
  138. else if (strcmp(argv[1], "wss") == 0)
  139. {
  140. LOG_E("connect %s://%s:%d/ failed. ", argv[1], argv[2], port);
  141. }
  142. }
  143. else
  144. {
  145. if (strcmp(argv[1], "ws") == 0)
  146. {
  147. LOG_D("try connect %s://%s:%d/ ", argv[1], argv[2], port);
  148. }
  149. else if (strcmp(argv[1], "wss") == 0)
  150. {
  151. LOG_D("try connect %s://%s:%d/ ", argv[1], argv[2], port);
  152. }
  153. }
  154. app->init = RT_TRUE;
  155. return RT_EOK;
  156. }
  157. MSH_CMD_EXPORT_ALIAS(_rws_connect, rws_connect, websocket connect);
  158. static int _rws_disconnect(int argc, char *argv[])
  159. {
  160. if (app->init == RT_FALSE)
  161. {
  162. LOG_W("no websocket connection. ");
  163. return (-RT_ERROR);
  164. }
  165. rws_socket_disconnect_and_release(app->socket);
  166. LOG_I("try disconnect websocket connection. ");
  167. return RT_EOK;
  168. }
  169. MSH_CMD_EXPORT_ALIAS(_rws_disconnect, rws_disconnect, websocket disconnect);
  170. static int _rws_send(int argc, char *argv[])
  171. {
  172. if (argc == 1)
  173. {
  174. LOG_E("The command format: rws_send \"content\". ");
  175. return RT_EOK;
  176. }
  177. if (app->init == RT_FALSE)
  178. {
  179. LOG_W("no websocket connection. ");
  180. return (-RT_ERROR);
  181. }
  182. LOG_W("string = %s, len = %d", argv[1], rt_strlen(argv[1]));
  183. rws_socket_send_text(app->socket, argv[1]);
  184. return RT_EOK;
  185. }
  186. MSH_CMD_EXPORT_ALIAS(_rws_send, rws_send, websocket send text);