1. Question
Among them, C establishes tcp connection and http request is implemented by a class, and the process is asynchronous. There is a callback function on_read() that will get the response information from the server and process it.
C After getting the json request of js, we start the network work.
The problem is:
a. I don’t know when the response information will be returned, and I can’t wait for the response like synchronization.
b. The v8 api cannot be used outside the main thread of js, which means that the response information cannot be directly returned to js in the on_read() callback function
2. Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
Methods to try:
a. Set up a while loop to wait in the main thread. After on_read() returns data to js, the sub-thread sets a flag to indicate the end. This can It ends correctly, but C's asynchronous request becomes a synchronization, which is meaningless.
b. I saw the libuv method on the Internet, but there is still a problem that the libuv working function cannot get the C asynchronous response message. The main issue is how to handle the asynchronous message and C return to the node, and how to handle it between threads.
Related articles:
NodeJS and Mysql interaction sample code_javascript skills
Detailed introduction to C# code and javaScript functions Mutual calls
Related videos:
Nodejs mongoDB practical development Weibo system video tutorial