Due to work needs, some simple code started with websocket does not know how to run, so there is always a layer of mystery, but there is not much interest in studying it. However, it is inevitable that you need to know something to avoid any impact on subsequent work. I learned from the ground up and finally completed the coding test for less than a week. I encountered a lot of problems. Fortunately, I solved them through my own thinking and help.
About go and websocket programming, GitHub has a lot of open-source code, such as https://github.com/garyburd/go-websocket. I found that the websocket programming about Go is basically similar on the Internet. Of course, websocket is just like that. Let's talk about the troubles I have encountered for such a simple problem.
1. How can I run the server after it is started? Because at the beginning, my understanding of websocket is completely unknown, and I don't even know how to run the program. At the beginning, HTML files are directly run, but static pages do not seem to interact with the server at all. After thinking about it, simply enter the IP address in the browser. This will help you understand the meaning of the URL specified on the server.
2. Because of work requirements, our development is carried out in virtual machines, but the virtual machines cannot connect to the Internet. I think it is better to use a virtual machine instead of a virtual machine. Therefore, I have to work hard to deploy the environment early every morning to work in the Internet environment that day. What a fearless spirit is this, hey. To put it bluntly, you can run a program on the Internet, but it cannot run when you migrate it to the Intranet. This is also very easy to find, because there is only one difference between the Intranet and the Internet, one can go to the Internet, one can not. After reading the code, there is src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", so these JS resources should be downloaded online, I downloaded this library and saved it to the file, you cannot associate resources with SRC. In the same operation, I made it again on the Internet and started the operation department. I have learned some browser debugging methods before and found that these source files are not found at all. The directories are all good. Why can't I find them? It's really depressing. After calling master, he said that he could not find the resources, and then told me the principles of websocket again. These protocols were rarely used before, in many cases, you may not be able to understand it all at once. When the IP address is entered in the browser, it will be found from this directory. Of course, it cannot be found. Then I said that I didn't understand how to enable nginx or anything. Since we can only find this HTML file, why can't I directly write these JS files into HTML? Facts have proved that this idea is good because JavaScript code is concentrated and will not affect the readability of the Code.
3. It is important to unify the data format at both ends of the protocol for information transmission. My idea is to get the original data from the peer end and encapsulate it as needed. In this way, both sides are transmitted using Byte arrays. However, after the server receives the data, in order to verify the correctness, I converted it into a string and printed it out, which is strange as long as it is a conversion, only the data received for the first time can be printed, and the following data may be lost. Without conversion, you can print out the binary stream of information in a lot of ways. I scanned all the errors and found no problems. I called master again. After describing my situation, he suggested that you try to use functions that have been encapsulated in the websocket package. This guarantees the performance and reduces the chance of errors. At that time, I was still very confused about why such a result would appear. So even if I replaced my function with a function in the function library, I always did not forget to try whether there was a problem during conversion. For debugging convenience, all input is redirected to the file, and then the results are viewed in a text editor. This time, the same results show that my code may not be the source of the problem. I tried to reduce the output and run it directly in the compiler. I saw the correct result. I confirmed that the text editor was faulty. It was really unreliable, it took me one afternoon to find the problem. Later, I encountered a similar problem. All the output in the text editor became a string of numbers. it was normal to print the content using cat. The open source is really unreliable. Of course, there will be no more dependency on the text editor. In fact, there have been three changes, and there will always be annoying bugs from time to time.
4. In terms of the organizational structure of the code, I think it is impossible for the master to solve all the things in one sentence. I really admire it. Sometimes, encapsulating some functions into a function and then calling it can make the Code look neat and tidy, and I am always trying to encapsulate those functions, it is very convenient to call directly. I have seen and never used embedding functions internally. I have encountered problems and cannot think of solutions that I can solve. Therefore, this is an experience.
5. Chinese garbled characters. For Java learners, Chinese Garbled text seems to be a common problem. No matter how careful you are, this Chinese Garbled text will never go away. In the past, every time I encountered garbled code, I complained about myself. Why did I choose java? Isn't it okay? Now it's hard to get rid of Java, and I have encountered Chinese garbled characters. I barrier with my master. We don't support Chinese. Why do we have to work hard to solve this problem. He looked green at the time and said it was necessary. Now let's think about it. It's really funny. Why is it so easy to retreat. When using English, TCP and websocket communicate well with each other, and garbled characters only occur when the websocket browser client inputs Chinese characters. After tracking for a long time, I found that sending from the browser to sending from the server is correct Chinese, and it becomes garbled only after the browser receives the data. I just fixed the problem: Is there a problem with the character encoding in JavaScript code? At that time, I didn't feel that my server sending side was faulty, because even if websocket was used to communicate with each other, there was no problem with Chinese. After finding the old master, he asked me three more questions to confirm that it was normal when the server side sent the message. At first, I said "OK". Later, he asked me if it was okay. He began to look for front-end problems. It is reasonable to say that the front-end code is adaptive and character encoding should not be adjusted. Otherwise, the user experience will be poor. Who else will modify the character set when visiting a webpage. The final conclusion is that the browser had garbled characters when receiving the message. This is not fun. It fully demonstrates that the problem lies in the sending end. Why is it better to use a browser? I used the original demo program to run the program and found that Chinese characters are acceptable. I found that when I sent the program, I converted the message into a byte array, in fact, you can simply pass the data in string format to the function. Maybe the function has re-converted the data.
These are the longest problems that have been entangled in me. By solving these problems, I have learned a lot. You can calm down and analyze the problem.